hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1570523 - in /httpcomponents/httpclient/branches/4.3.x: 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
Date Fri, 21 Feb 2014 10:55:16 GMT
Author: olegk
Date: Fri Feb 21 10:55:15 2014
New Revision: 1570523

URL: http://svn.apache.org/r1570523
Log:
HTTPCLIENT-1466: FileBodyPart#generateContentType() ignores custom ContentType values

Modified:
    httpcomponents/httpclient/branches/4.3.x/RELEASE_NOTES.txt
    httpcomponents/httpclient/branches/4.3.x/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java
    httpcomponents/httpclient/branches/4.3.x/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java

Modified: httpcomponents/httpclient/branches/4.3.x/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.3.x/RELEASE_NOTES.txt?rev=1570523&r1=1570522&r2=1570523&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.3.x/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/branches/4.3.x/RELEASE_NOTES.txt Fri Feb 21 10:55:15 2014
@@ -9,6 +9,9 @@ Users of HttpClient 4.3 are encouraged t
 Changelog:
 -------------------
 
+* [HTTPCLIENT-1466] FileBodyPart#generateContentType() ignores custom ContentType values.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * [HTTPCLIENT-1453] Thread safety regression in PoolingHttpClientConnectionManager
   #closeExpiredConnections that can lead to ConcurrentModificationException. 
   Contributed by Oleg Kalnichevski <olegk at apache.org>

Modified: httpcomponents/httpclient/branches/4.3.x/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.3.x/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java?rev=1570523&r1=1570522&r2=1570523&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.3.x/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java
(original)
+++ httpcomponents/httpclient/branches/4.3.x/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java
Fri Feb 21 10:55:15 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/branches/4.3.x/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.3.x/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java?rev=1570523&r1=1570522&r2=1570523&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.3.x/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
(original)
+++ httpcomponents/httpclient/branches/4.3.x/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
Fri Feb 21 10:55:15 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);



Mime
View raw message