-
Notifications
You must be signed in to change notification settings - Fork 594
/
HttpRequestMultiPart.java
208 lines (174 loc) · 6.09 KB
/
HttpRequestMultiPart.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package kong.unirest;
import org.apache.http.HttpHeaders;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.*;
class HttpRequestMultiPart extends BaseRequest<MultipartBody> implements MultipartBody {
private List<BodyPart> parameters = new ArrayList<>();
private MultipartMode mode = MultipartMode.BROWSER_COMPATIBLE;
private Charset charSet;
private boolean forceMulti = false;
private ProgressMonitor monitor;
HttpRequestMultiPart(HttpRequestBody httpRequest) {
super(httpRequest);
this.charSet = httpRequest.getCharset();
}
@Override
public MultipartBody field(String name, String value) {
addPart(new ParamPart(name, value));
return this;
}
@Override
public MultipartBody field(String name, String value, String contentType) {
addPart(name, value, contentType);
return this;
}
@Override
public MultipartBody field(String name, Collection<?> collection) {
for (Object current: collection) {
addPart(name, current, null);
}
return this;
}
@Override
public MultipartBody field(String name, InputStream value, ContentType contentType) {
addPart(new InputStreamPart(name, value, contentType.toString()));
return this;
}
@Override
public MultipartBody field(String name, File file) {
addPart(new FilePart(file, name));
return this;
}
@Override
public MultipartBody field(String name, File file, String contentType) {
addPart(new FilePart(file, name, contentType));
return this;
}
@Override
public MultipartBody field(String name, InputStream stream, ContentType contentType, String fileName) {
addPart(new InputStreamPart(name, stream, contentType.toString(), fileName));
return this;
}
@Override
public MultipartBody field(String name, InputStream stream, String fileName) {
addPart(new InputStreamPart(name, stream, ContentType.APPLICATION_OCTET_STREAM.toString(), fileName));
return this;
}
@Override
public MultipartBody field(String name, byte[] bytes, ContentType contentType, String fileName) {
addPart(new ByteArrayPart(name, bytes, contentType, fileName));
return this;
}
@Override
public MultipartBody field(String name, byte[] bytes, String fileName) {
addPart(new ByteArrayPart(name, bytes, ContentType.APPLICATION_OCTET_STREAM, fileName));
return this;
}
@Override
public MultipartBody charset(Charset charset) {
this.charSet = charset;
return this;
}
@Override
public MultipartBody contentType(String mimeType) {
header(HttpHeaders.CONTENT_TYPE, mimeType);
return this;
}
@Override
public MultipartBody mode(MultipartMode value) {
this.mode = value;
return this;
}
@Override
public MultipartBody uploadMonitor(ProgressMonitor uploadMonitor) {
this.monitor = uploadMonitor;
return this;
}
@Override
public Charset getCharset() {
return this.charSet;
}
public MultipartBody fields(Map<String, Object> fields) {
if (fields != null) {
for (Map.Entry<String, Object> param : fields.entrySet()) {
if (param.getValue() instanceof File) {
field(param.getKey(), (File) param.getValue());
} else {
field(param.getKey(), Util.nullToEmpty(param.getValue()));
}
}
}
return this;
}
public MultipartBody field(String name, Object value, String contentType) {
addPart(name, value, contentType);
return this;
}
private void addPart(String name, Object value, String contentType) {
if(value instanceof InputStream){
addPart(new InputStreamPart(name, (InputStream)value, contentType));
} else if (value instanceof File) {
addPart(new FilePart((File)value, name, contentType));
} else {
addPart(new ParamPart(name, Util.nullToEmpty(value), contentType));
}
}
private void addPart(BodyPart value) {
parameters.add(value);
Collections.sort(parameters);
}
@Override
public Optional<Body> getBody() {
return Optional.of(this);
}
@Override
public boolean isMultiPart() {
return forceMulti || multiParts().stream().anyMatch(BodyPart::isFile);
}
@Override
public boolean isEntityBody() {
return false;
}
@Override
public Collection<BodyPart> multiParts() {
return new ArrayList<>(parameters);
}
@Override
public MultipartMode getMode() {
return mode;
}
@Override
public ProgressMonitor getMonitor() {
return monitor;
}
MultipartBody forceMultiPart(boolean value) {
forceMulti = value;
return this;
}
}