Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 760a911

Browse files
author
Stephane Landelle
committed
1 parent fbcc220 commit 760a911

File tree

11 files changed

+242
-238
lines changed

11 files changed

+242
-238
lines changed

api/src/main/java/com/ning/http/client/providers/jdk/JDKAsyncHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
608608
lenght = MAX_BUFFERED_BYTES;
609609
}
610610

611-
MultipartRequestEntity mre = AsyncHttpProviderUtils.createMultipartRequestEntity(request.getParts(), request.getParams());
611+
MultipartRequestEntity mre = AsyncHttpProviderUtils.createMultipartRequestEntity(request.getParts(), request.getHeaders());
612612

613613
urlConnection.setRequestProperty("Content-Type", mre.getContentType());
614614
urlConnection.setRequestProperty("Content-Length", String.valueOf(mre.getContentLength()));

api/src/main/java/com/ning/http/multipart/FilePart.java

Lines changed: 55 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* This class is an adaptation of the Apache HttpClient implementation
26-
*
26+
*
2727
* @link http://hc.apache.org/httpclient-3.x/
2828
*/
2929
public class FilePart extends PartBase {
@@ -51,43 +51,40 @@ public class FilePart extends PartBase {
5151
/**
5252
* Attachment's file name as a byte array
5353
*/
54-
private static final byte[] FILE_NAME_BYTES =
55-
MultipartEncodingUtil.getAsciiBytes(FILE_NAME);
54+
private static final byte[] FILE_NAME_BYTES = MultipartEncodingUtil.getAsciiBytes(FILE_NAME);
5655

5756
/**
5857
* Source of the file part.
5958
*/
60-
private PartSource source;
59+
private final PartSource source;
6160

6261
/**
6362
* FilePart Constructor.
64-
*
65-
* @param name the name for this part
66-
* @param partSource the source for this part
67-
* @param contentType the content type for this part, if <code>null</code> the
68-
* {@link #DEFAULT_CONTENT_TYPE default} is used
69-
* @param charset the charset encoding for this part, if <code>null</code> the
70-
* {@link #DEFAULT_CHARSET default} is used
63+
*
64+
* @param name the name for this part
65+
* @param partSource the source for this part
66+
* @param contentType the content type for this part, if <code>null</code> the {@link #DEFAULT_CONTENT_TYPE default} is used
67+
* @param charset the charset encoding for this part, if <code>null</code> the {@link #DEFAULT_CHARSET default} is used
68+
* @param contentId
7169
*/
72-
public FilePart(String name, PartSource partSource, String contentType, String charset) {
70+
public FilePart(String name, PartSource partSource, String contentType, String charset, String contentId) {
7371

74-
super(
75-
name,
76-
contentType == null ? DEFAULT_CONTENT_TYPE : contentType,
77-
charset == null ? "ISO-8859-1" : charset,
78-
DEFAULT_TRANSFER_ENCODING
79-
);
72+
super(name, contentType == null ? DEFAULT_CONTENT_TYPE : contentType, charset == null ? "ISO-8859-1" : charset, DEFAULT_TRANSFER_ENCODING, contentId);
8073

8174
if (partSource == null) {
8275
throw new IllegalArgumentException("Source may not be null");
8376
}
8477
this.source = partSource;
8578
}
79+
80+
public FilePart(String name, PartSource partSource, String contentType, String charset) {
81+
this(name, partSource, contentType, charset, null);
82+
}
8683

8784
/**
8885
* FilePart Constructor.
89-
*
90-
* @param name the name for this part
86+
*
87+
* @param name the name for this part
9188
* @param partSource the source for this part
9289
*/
9390
public FilePart(String name, PartSource partSource) {
@@ -96,77 +93,64 @@ public FilePart(String name, PartSource partSource) {
9693

9794
/**
9895
* FilePart Constructor.
99-
*
96+
*
10097
* @param name the name of the file part
10198
* @param file the file to post
102-
* @throws java.io.FileNotFoundException if the <i>file</i> is not a normal
103-
* file or if it is not readable.
99+
* @throws java.io.FileNotFoundException if the <i>file</i> is not a normal file or if it is not readable.
104100
*/
105-
public FilePart(String name, File file)
106-
throws FileNotFoundException {
101+
public FilePart(String name, File file) throws FileNotFoundException {
107102
this(name, new FilePartSource(file), null, null);
108103
}
109104

110105
/**
111106
* FilePart Constructor.
112-
*
113-
* @param name the name of the file part
114-
* @param file the file to post
115-
* @param contentType the content type for this part, if <code>null</code> the
116-
* {@link #DEFAULT_CONTENT_TYPE default} is used
117-
* @param charset the charset encoding for this part, if <code>null</code> the
118-
* {@link #DEFAULT_CHARSET default} is used
119-
* @throws FileNotFoundException if the <i>file</i> is not a normal
120-
* file or if it is not readable.
121-
*/
122-
public FilePart(String name, File file, String contentType, String charset)
123-
throws FileNotFoundException {
107+
*
108+
* @param name the name of the file part
109+
* @param file the file to post
110+
* @param contentType the content type for this part, if <code>null</code> the {@link #DEFAULT_CONTENT_TYPE default} is used
111+
* @param charset the charset encoding for this part, if <code>null</code> the {@link #DEFAULT_CHARSET default} is used
112+
* @throws FileNotFoundException if the <i>file</i> is not a normal file or if it is not readable.
113+
*/
114+
public FilePart(String name, File file, String contentType, String charset) throws FileNotFoundException {
124115
this(name, new FilePartSource(file), contentType, charset);
125116
}
126117

127118
/**
128119
* FilePart Constructor.
129-
*
130-
* @param name the name of the file part
120+
*
121+
* @param name the name of the file part
131122
* @param fileName the file name
132-
* @param file the file to post
133-
* @throws FileNotFoundException if the <i>file</i> is not a normal
134-
* file or if it is not readable.
123+
* @param file the file to post
124+
* @throws FileNotFoundException if the <i>file</i> is not a normal file or if it is not readable.
135125
*/
136-
public FilePart(String name, String fileName, File file)
137-
throws FileNotFoundException {
126+
public FilePart(String name, String fileName, File file) throws FileNotFoundException {
138127
this(name, new FilePartSource(fileName, file), null, null);
139128
}
140129

141130
/**
142131
* FilePart Constructor.
143-
*
144-
* @param name the name of the file part
145-
* @param fileName the file name
146-
* @param file the file to post
147-
* @param contentType the content type for this part, if <code>null</code> the
148-
* {@link #DEFAULT_CONTENT_TYPE default} is used
149-
* @param charset the charset encoding for this part, if <code>null</code> the
150-
* {@link #DEFAULT_CHARSET default} is used
151-
* @throws FileNotFoundException if the <i>file</i> is not a normal
152-
* file or if it is not readable.
153-
*/
154-
public FilePart(String name, String fileName, File file, String contentType, String charset)
155-
throws FileNotFoundException {
132+
*
133+
* @param name the name of the file part
134+
* @param fileName the file name
135+
* @param file the file to post
136+
* @param contentType the content type for this part, if <code>null</code> the {@link #DEFAULT_CONTENT_TYPE default} is used
137+
* @param charset the charset encoding for this part, if <code>null</code> the {@link #DEFAULT_CHARSET default} is used
138+
* @throws FileNotFoundException if the <i>file</i> is not a normal file or if it is not readable.
139+
*/
140+
public FilePart(String name, String fileName, File file, String contentType, String charset) throws FileNotFoundException {
156141
this(name, new FilePartSource(fileName, file), contentType, charset);
157142
}
158143

159144
/**
160145
* Write the disposition header to the output stream
161-
*
146+
*
162147
* @param out The output stream
163148
* @throws java.io.IOException If an IO problem occurs
164149
*/
165-
protected void sendDispositionHeader(OutputStream out)
166-
throws IOException {
167-
super.sendDispositionHeader(out);
150+
protected void sendDispositionHeader(OutputStream out) throws IOException {
168151
String filename = this.source.getFileName();
169152
if (filename != null) {
153+
super.sendDispositionHeader(out);
170154
out.write(FILE_NAME_BYTES);
171155
out.write(QUOTE_BYTES);
172156
out.write(MultipartEncodingUtil.getAsciiBytes(filename));
@@ -176,7 +160,7 @@ protected void sendDispositionHeader(OutputStream out)
176160

177161
/**
178162
* Write the data in "source" to the specified stream.
179-
*
163+
*
180164
* @param out The output stream.
181165
* @throws IOException if an IO problem occurs.
182166
*/
@@ -202,17 +186,17 @@ protected void sendData(OutputStream out) throws IOException {
202186
}
203187
}
204188

205-
public void setStalledTime(long ms) {
206-
_stalledTime = ms;
207-
}
189+
public void setStalledTime(long ms) {
190+
_stalledTime = ms;
191+
}
208192

209-
public long getStalledTime() {
210-
return _stalledTime;
211-
}
193+
public long getStalledTime() {
194+
return _stalledTime;
195+
}
212196

213197
/**
214198
* Returns the source of the file part.
215-
*
199+
*
216200
* @return The source.
217201
*/
218202
protected PartSource getSource() {
@@ -221,14 +205,14 @@ protected PartSource getSource() {
221205

222206
/**
223207
* Return the length of the data.
224-
*
208+
*
225209
* @return The length.
226210
* @throws IOException if an IO problem occurs
227211
*/
228212
protected long lengthOfData() throws IOException {
229213
return source.getLength();
230214
}
231215

232-
private long _stalledTime = -1;
216+
private long _stalledTime = -1;
233217

234218
}

api/src/main/java/com/ning/http/multipart/FilePartSource.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/**
2626
* This class is an adaptation of the Apache HttpClient implementation
27-
*
27+
*
2828
* @link http://hc.apache.org/httpclient-3.x/
2929
*/
3030
public class FilePartSource implements PartSource {
@@ -41,22 +41,19 @@ public class FilePartSource implements PartSource {
4141

4242
/**
4343
* Constructor for FilePartSource.
44-
*
44+
*
4545
* @param file the FilePart source File.
46-
* @throws java.io.FileNotFoundException if the file does not exist or
47-
* cannot be read
46+
* @throws java.io.FileNotFoundException if the file does not exist or cannot be read
4847
*/
4948
public FilePartSource(File file) throws FileNotFoundException {
5049
this.file = file;
5150
if (file != null) {
5251
if (!file.isFile()) {
53-
final String errorMessage =
54-
String.format("File is not a normal file (%s).", file.getAbsolutePath());
52+
final String errorMessage = String.format("File is not a normal file (%s).", file.getAbsolutePath());
5553
throw new FileNotFoundException(errorMessage);
5654
}
5755
if (!file.canRead()) {
58-
final String errorMessage =
59-
String.format("File is not readable (%s).", file.getAbsolutePath());
56+
final String errorMessage = String.format("File is not readable (%s).", file.getAbsolutePath());
6057
throw new FileNotFoundException(errorMessage);
6158
}
6259
this.fileName = file.getName();
@@ -65,23 +62,19 @@ public FilePartSource(File file) throws FileNotFoundException {
6562

6663
/**
6764
* Constructor for FilePartSource.
68-
*
65+
*
6966
* @param fileName the file name of the FilePart
70-
* @param file the source File for the FilePart
71-
* @throws FileNotFoundException if the file does not exist or
72-
* cannot be read
67+
* @param file the source File for the FilePart
68+
* @throws FileNotFoundException if the file does not exist or cannot be read
7369
*/
74-
public FilePartSource(String fileName, File file)
75-
throws FileNotFoundException {
70+
public FilePartSource(String fileName, File file) throws FileNotFoundException {
7671
this(file);
77-
if (fileName != null) {
78-
this.fileName = fileName;
79-
}
72+
this.fileName = fileName;
8073
}
8174

8275
/**
8376
* Return the length of the file
84-
*
77+
*
8578
* @return the length of the file.
8679
* @see PartSource#getLength()
8780
*/
@@ -95,17 +88,17 @@ public long getLength() {
9588

9689
/**
9790
* Return the current filename
98-
*
91+
*
9992
* @return the filename.
10093
* @see PartSource#getFileName()
10194
*/
10295
public String getFileName() {
103-
return (fileName == null) ? "noname" : fileName;
96+
return fileName;
10497
}
10598

10699
/**
107100
* Return a new {@link java.io.FileInputStream} for the current filename.
108-
*
101+
*
109102
* @return the new input stream.
110103
* @throws java.io.IOException If an IO problem occurs.
111104
* @see PartSource#createInputStream()
@@ -114,13 +107,12 @@ public InputStream createInputStream() throws IOException {
114107
if (this.file != null) {
115108
return new FileInputStream(this.file);
116109
} else {
117-
return new ByteArrayInputStream(new byte[]{});
110+
return new ByteArrayInputStream(new byte[] {});
118111
}
119112
}
120113

121114
public File getFile() {
122115
return file;
123116
}
124117

125-
126118
}

api/src/main/java/com/ning/http/multipart/MultipartBody.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public class MultipartBody implements RandomAccessBody {
4848

4949
enum FileLocation {NONE, START, MIDDLE, END}
5050

51-
public MultipartBody(List<com.ning.http.client.Part> parts, String boundary, String contentLength) {
52-
this.boundary = MultipartEncodingUtil.getAsciiBytes(boundary.substring("multipart/form-data; boundary=".length()));
51+
public MultipartBody(List<com.ning.http.client.Part> parts, String contentType, String contentLength) {
52+
this.boundary = MultipartEncodingUtil.getAsciiBytes(contentType.substring(contentType.indexOf("boundary=") + "boundary=".length()));
5353
this.contentLength = Long.parseLong(contentLength);
5454
this.parts = parts;
5555

@@ -430,6 +430,7 @@ private ByteArrayOutputStream generateFileStart(FilePart filePart)
430430
filePart.sendDispositionHeader(overhead);
431431
filePart.sendContentTypeHeader(overhead);
432432
filePart.sendTransferEncodingHeader(overhead);
433+
filePart.sendContentIdHeader(overhead);
433434
filePart.sendEndOfHeader(overhead);
434435
return overhead;
435436
}

0 commit comments

Comments
 (0)