-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormDataBody.java
166 lines (115 loc) · 5.05 KB
/
FormDataBody.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.*;
import java.io.*;
/**
* This class represent a form data body.
* It holds the given key values and convert them to a form data body.
* for use : first call {@link FormDataBody#build()} then call {@link FormDataBody#set()}.
* Use {@link FormDataBody#getContentType()} and {@link FormDataBody#getContentLength()} to set the Content headers
*
*
* @author Mohammad Mahdi Malmasi
* @version 0.2.5
*/
public class FormDataBody extends RequestBody
{
/* Fields */
// client given datas String
private String datasString;
// this hash map holds the key and values of the body
// if some of the values are file, the key of them will contains "(FILE)"
private HashMap<String, String> keyValues;
// a boundary for this form data body
private final String boundaryKey;
// final boundray for write on output stream
private final String boundary;
private static final long serialVersionUID = 3231995008582149988L;
/* Constructor */
/**
* Create a new FormData Body by given datas.
* The created FormData body will sent to given output stream
*
*
* @param datas : a {@code String} of datas that you want to sent them in form data.
* the given {@code String} should be in this form: "key=valu&key1=value1& ..."
*/
public FormDataBody(String datas)
{
this.datasString = datas;
this.keyValues = new HashMap<>();
boundaryKey = "Jurl-MmMmM-79--" + (((new Date()).getTime() + System.nanoTime())/3006009);
this.boundary = "--".concat(boundaryKey).concat("\r\n");
}
/* Methods */
/**
* This method build a {@code HashMap<String, String>} of given datas {@code String}.
* If the given {@code String} isn't in "key=value&key1=value1& ..." format, the current JVM will terminate
*/
public void build()
{
String[] holdDatas = datasString.split("&");
String key = null, value = null;
for (String data : holdDatas)
{
try
{
key = data.substring(0, data.indexOf('='));
value = data.substring(data.indexOf('=')+1);
if (value.length() == 0 || key.length() == 0)
error();
}
catch (IndexOutOfBoundsException e) { error(); }
keyValues.put(key, value);
}
}
/**
* This method set the form date of given key-values as connection body
*/
public void set(OutputStream connectionOutputStream) throws IOException
{
for (String key : keyValues.keySet())
{
// write boundry
connectionOutputStream.write(boundary.getBytes());
super.Content_Length += boundary.getBytes().length;
// for file cases
if (key.contains("(FILE)"))
{
connectionOutputStream.write(("Content-Disposition: form-data; filename=\"" +
(new File(keyValues.get(key))).getName() +
"\"\r\nContent-Type: Auto\r\n\r\n").getBytes());
super.Content_Length += ("Content-Disposition: form-data; filename=\"" +
(new File(keyValues.get(key))).getName() +
"\"\r\nContent-Type: Auto\r\n\r\n").getBytes().length;
try (BufferedInputStream tempBIS = new BufferedInputStream(new FileInputStream(new File(keyValues.get(key)))))
{
byte[] filesBytes = tempBIS.readAllBytes();
super.Content_Length += filesBytes.length;
connectionOutputStream.write(filesBytes);
super.Content_Length += "\r\n".getBytes().length;
connectionOutputStream.write("\r\n".getBytes());
}
catch (IOException e) { error(); }
}
// for text cases
else
{
super.Content_Length += ("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n").getBytes().length;
connectionOutputStream.write(("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n").getBytes());
super.Content_Length += (keyValues.get(key) + "\r\n").getBytes().length;
connectionOutputStream.write((keyValues.get(key) + "\r\n").getBytes());
}
}
super.Content_Length += boundary.getBytes().length;
connectionOutputStream.write(("--" + boundaryKey + "--\r\n").getBytes());
connectionOutputStream.flush();
connectionOutputStream.close();
}
/**
* This method return the boundary of this form data body.
* set a header to your request with key: "Content-Type", and value: 'output of this method'
*/
public String getContentType()
{
return "multipart/form-data; charset=utf-8; boundary=" + boundaryKey;
}
}