-
Notifications
You must be signed in to change notification settings - Fork 444
/
MultipartContent.java
264 lines (234 loc) · 8.02 KB
/
MultipartContent.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.http;
import com.google.api.client.util.Preconditions;
import com.google.api.client.util.StreamingContent;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.UUID;
/**
* Serializes MIME multipart content as specified by <a
* href="http://tools.ietf.org/html/rfc2387">RFC 2387: The MIME Multipart/Related Content-type</a>
* and <a href="http://tools.ietf.org/html/rfc1521#section-7.2.2">RFC 2046: Multipurpose Internet
* Mail Extensions: The Multipart/mixed (primary) subtype</a>.
*
* <p>By default the media type is {@code "multipart/related; boundary=__END_OF_PART__<random
* UUID>__"}, but this may be customized by calling {@link #setMediaType(HttpMediaType)}, {@link
* #getMediaType()}, or {@link #setBoundary(String)}.
*
* <p>Implementation is not thread-safe.
*
* @since 1.14
* @author Yaniv Inbar
*/
public class MultipartContent extends AbstractHttpContent {
static final String NEWLINE = "\r\n";
private static final String TWO_DASHES = "--";
/** Parts of the HTTP multipart request. */
private ArrayList<Part> parts = new ArrayList<>();
public MultipartContent() {
this("__END_OF_PART__" + UUID.randomUUID().toString() + "__");
}
public MultipartContent(String boundary) {
super(new HttpMediaType("multipart/related").setParameter("boundary", boundary));
}
public void writeTo(OutputStream out) throws IOException {
Writer writer = new OutputStreamWriter(out, getCharset());
String boundary = getBoundary();
for (Part part : parts) {
HttpHeaders headers = new HttpHeaders().setAcceptEncoding(null);
if (part.headers != null) {
headers.fromHttpHeaders(part.headers);
}
headers
.setContentEncoding(null)
.setUserAgent(null)
.setContentType(null)
.setContentLength(null)
.set("Content-Transfer-Encoding", null);
// analyze the content
HttpContent content = part.content;
StreamingContent streamingContent = null;
if (content != null) {
headers.set("Content-Transfer-Encoding", Arrays.asList("binary"));
headers.setContentType(content.getType());
HttpEncoding encoding = part.encoding;
long contentLength;
if (encoding == null) {
contentLength = content.getLength();
streamingContent = content;
} else {
headers.setContentEncoding(encoding.getName());
streamingContent = new HttpEncodingStreamingContent(content, encoding);
contentLength = AbstractHttpContent.computeLength(content);
}
if (contentLength != -1) {
headers.setContentLength(contentLength);
}
}
// write multipart-body from RFC 1521 §7.2.1
// write encapsulation
// write delimiter
writer.write(TWO_DASHES);
writer.write(boundary);
writer.write(NEWLINE);
// write body-part; message from RFC 822 §4.1
// write message fields
HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
if (streamingContent != null) {
writer.write(NEWLINE);
writer.flush();
// write message text/body
streamingContent.writeTo(out);
}
// terminate encapsulation
writer.write(NEWLINE);
}
// write close-delimiter
writer.write(TWO_DASHES);
writer.write(boundary);
writer.write(TWO_DASHES);
writer.write(NEWLINE);
writer.flush();
}
@Override
public boolean retrySupported() {
for (Part part : parts) {
if (!part.content.retrySupported()) {
return false;
}
}
return true;
}
@Override
public MultipartContent setMediaType(HttpMediaType mediaType) {
super.setMediaType(mediaType);
return this;
}
/** Returns an unmodifiable view of the parts of the HTTP multipart request. */
public final Collection<Part> getParts() {
return Collections.unmodifiableCollection(parts);
}
/**
* Adds an HTTP multipart part.
*
* <p>Overriding is only supported for the purpose of calling the super implementation and
* changing the return type, but nothing else.
*/
public MultipartContent addPart(Part part) {
parts.add(Preconditions.checkNotNull(part));
return this;
}
/**
* Sets the parts of the HTTP multipart request.
*
* <p>Overriding is only supported for the purpose of calling the super implementation and
* changing the return type, but nothing else.
*/
public MultipartContent setParts(Collection<Part> parts) {
this.parts = new ArrayList<>(parts);
return this;
}
/**
* Sets the HTTP content parts of the HTTP multipart request, where each part is assumed to have
* no HTTP headers and no encoding.
*
* <p>Overriding is only supported for the purpose of calling the super implementation and
* changing the return type, but nothing else.
*/
public MultipartContent setContentParts(Collection<? extends HttpContent> contentParts) {
this.parts = new ArrayList<>(contentParts.size());
for (HttpContent contentPart : contentParts) {
addPart(new Part(contentPart));
}
return this;
}
/** Returns the boundary string to use. */
public final String getBoundary() {
return getMediaType().getParameter("boundary");
}
/**
* Sets the boundary string to use.
*
* <p>Defaults to {@code "END_OF_PART"}.
*
* <p>Overriding is only supported for the purpose of calling the super implementation and
* changing the return type, but nothing else.
*/
public MultipartContent setBoundary(String boundary) {
getMediaType().setParameter("boundary", Preconditions.checkNotNull(boundary));
return this;
}
/**
* Single part of a multi-part request.
*
* <p>Implementation is not thread-safe.
*/
public static final class Part {
/** HTTP content or {@code null} for none. */
HttpContent content;
/** HTTP headers or {@code null} for none. */
HttpHeaders headers;
/** HTTP encoding or {@code null} for none. */
HttpEncoding encoding;
public Part() {
this(null);
}
/** @param content HTTP content or {@code null} for none */
public Part(HttpContent content) {
this(null, content);
}
/**
* @param headers HTTP headers or {@code null} for none
* @param content HTTP content or {@code null} for none
*/
public Part(HttpHeaders headers, HttpContent content) {
setHeaders(headers);
setContent(content);
}
/** Sets the HTTP content or {@code null} for none. */
public Part setContent(HttpContent content) {
this.content = content;
return this;
}
/** Returns the HTTP content or {@code null} for none. */
public HttpContent getContent() {
return content;
}
/** Sets the HTTP headers or {@code null} for none. */
public Part setHeaders(HttpHeaders headers) {
this.headers = headers;
return this;
}
/** Returns the HTTP headers or {@code null} for none. */
public HttpHeaders getHeaders() {
return headers;
}
/** Sets the HTTP encoding or {@code null} for none. */
public Part setEncoding(HttpEncoding encoding) {
this.encoding = encoding;
return this;
}
/** Returns the HTTP encoding or {@code null} for none. */
public HttpEncoding getEncoding() {
return encoding;
}
}
}