You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2014/02/21 11:58:02 UTC
svn commit: r1570525 - in /httpcomponents/httpclient/trunk: RELEASE_NOTES.txt
httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java
httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
Author: olegk
Date: Fri Feb 21 10:58:02 2014
New Revision: 1570525
URL: http://svn.apache.org/r1570525
Log:
HTTPCLIENT-1466: FileBodyPart#generateContentType() ignores custom ContentType values
Modified:
httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java
httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1570525&r1=1570524&r2=1570525&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Fri Feb 21 10:58:02 2014
@@ -4,6 +4,9 @@ Changes for 4.4-alpha1
Changelog:
-------------------
+* [HTTPCLIENT-1466] FileBodyPart#generateContentType() ignores custom ContentType values.
+ Contributed by Oleg Kalnichevski <olegk at apache.org>
+
* [HTTPCLIENT-1461] fixed performance degradation in gzip encoded content processing
introduced by HTTPCLIENT-1432.
Contributed by Oleg Kalnichevski <olegk at apache.org>
Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java?rev=1570525&r1=1570524&r2=1570525&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java Fri Feb 21 10:58:02 2014
@@ -27,6 +27,8 @@
package org.apache.http.entity.mime;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.mime.content.AbstractContentBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.util.Args;
@@ -88,13 +90,23 @@ public class FormBodyPart {
}
protected void generateContentType(final ContentBody body) {
- final StringBuilder buffer = new StringBuilder();
- buffer.append(body.getMimeType()); // MimeType cannot be null
- if (body.getCharset() != null) { // charset may legitimately be null
- buffer.append("; charset=");
- buffer.append(body.getCharset());
+ final ContentType contentType;
+ if (body instanceof AbstractContentBody) {
+ contentType = ((AbstractContentBody) body).getContentType();
+ } else {
+ contentType = null;
+ }
+ if (contentType != null) {
+ addField(MIME.CONTENT_TYPE, contentType.toString());
+ } else {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append(body.getMimeType()); // MimeType cannot be null
+ if (body.getCharset() != null) { // charset may legitimately be null
+ buffer.append("; charset=");
+ buffer.append(body.getCharset());
+ }
+ addField(MIME.CONTENT_TYPE, buffer.toString());
}
- addField(MIME.CONTENT_TYPE, buffer.toString());
}
protected void generateTransferEncoding(final ContentBody body) {
Modified: httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java?rev=1570525&r1=1570524&r2=1570525&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java Fri Feb 21 10:58:02 2014
@@ -100,6 +100,40 @@ public class TestMultipartForm {
}
@Test
+ public void testMultipartFormCustomContentType() throws Exception {
+ final FormBodyPart p1 = new FormBodyPart(
+ "field1",
+ new StringBody("this stuff", ContentType.DEFAULT_TEXT));
+ final FormBodyPart p2 = new FormBodyPart(
+ "field2",
+ new StringBody("that stuff", ContentType.parse("stuff/plain; param=value")));
+ final HttpStrictMultipart multipart = new HttpStrictMultipart("form-data", null, "foo",
+ Arrays.asList(p1, p2));
+
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ multipart.writeTo(out);
+ out.close();
+
+ final String expected =
+ "--foo\r\n" +
+ "Content-Disposition: form-data; name=\"field1\"\r\n" +
+ "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
+ "Content-Transfer-Encoding: 8bit\r\n" +
+ "\r\n" +
+ "this stuff\r\n" +
+ "--foo\r\n" +
+ "Content-Disposition: form-data; name=\"field2\"\r\n" +
+ "Content-Type: stuff/plain; param=value\r\n" +
+ "Content-Transfer-Encoding: 8bit\r\n" +
+ "\r\n" +
+ "that stuff\r\n" +
+ "--foo--\r\n";
+ final String s = out.toString("US-ASCII");
+ Assert.assertEquals(expected, s);
+ Assert.assertEquals(s.length(), multipart.getTotalLength());
+ }
+
+ @Test
public void testMultipartFormBinaryParts() throws Exception {
tmpfile = File.createTempFile("tmp", ".bin");
final Writer writer = new FileWriter(tmpfile);