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/12/07 20:20:10 UTC

svn commit: r1643711 - in /httpcomponents/httpcore/branches/4.4.x/httpcore/src: main/java/org/apache/http/entity/ContentType.java test/java/org/apache/http/entity/TestContentType.java

Author: olegk
Date: Sun Dec  7 19:20:09 2014
New Revision: 1643711

URL: http://svn.apache.org/r1643711
Log:
Added methods to create ContentType with arbitrary parameters

Modified:
    httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/entity/ContentType.java
    httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/entity/TestContentType.java

Modified: httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/entity/ContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/entity/ContentType.java?rev=1643711&r1=1643710&r2=1643711&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/entity/ContentType.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/entity/ContentType.java Sun Dec  7 19:20:09 2014
@@ -30,7 +30,11 @@ package org.apache.http.entity;
 import java.io.Serializable;
 import java.nio.charset.Charset;
 import java.nio.charset.UnsupportedCharsetException;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 
 import org.apache.http.Consts;
 import org.apache.http.Header;
@@ -41,6 +45,7 @@ import org.apache.http.ParseException;
 import org.apache.http.annotation.Immutable;
 import org.apache.http.message.BasicHeaderValueFormatter;
 import org.apache.http.message.BasicHeaderValueParser;
+import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.message.ParserCursor;
 import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
@@ -205,9 +210,10 @@ public final class ContentType implement
     }
 
     private static ContentType create(final HeaderElement helem, final boolean strict) {
-        final String mimeType = helem.getName();
-        final NameValuePair[] params = helem.getParameters();
+        return create(helem.getName(), helem.getParameters(), strict);
+    }
 
+    private static ContentType create(final String mimeType, final NameValuePair[] params, final boolean strict) {
         Charset charset = null;
         for (final NameValuePair param: params) {
             if (param.getName().equalsIgnoreCase("charset")) {
@@ -228,6 +234,23 @@ public final class ContentType implement
     }
 
     /**
+     * Creates a new instance of {@link ContentType} with the given parameters.
+     *
+     * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain
+     *        characters {@code <">, <;>, <,>} reserved by the HTTP specification.
+     * @param params parameters.
+     * @return content type
+     *
+     * @since 4.4
+     */
+    public static ContentType create(
+            final String mimeType, final NameValuePair... params) throws UnsupportedCharsetException {
+        final String type = Args.notBlank(mimeType, "MIME type").toLowerCase(Locale.ROOT);
+        Args.check(valid(type), "MIME type may not contain reserved characters");
+        return create(mimeType, params, true);
+    }
+
+    /**
      * Parses textual representation of {@code Content-Type} value.
      *
      * @param s text
@@ -362,4 +385,35 @@ public final class ContentType implement
         return create(this.getMimeType(), charset);
     }
 
+    /**
+     * Creates a new instance with this MIME type and the given parameters.
+     *
+     * @param params
+     * @return a new instance with this MIME type and the given parameters.
+     * @since 4.4
+     */
+    public ContentType withParameters(
+            final NameValuePair... params) throws UnsupportedCharsetException {
+        if (params.length == 0) {
+            return this;
+        }
+        final Map<String, String> paramMap = new LinkedHashMap<String, String>();
+        if (this.params != null) {
+            for (NameValuePair param: this.params) {
+                paramMap.put(param.getName(), param.getValue());
+            }
+        }
+        for (NameValuePair param: params) {
+            paramMap.put(param.getName(), param.getValue());
+        }
+        final List<NameValuePair> newParams = new ArrayList<NameValuePair>(paramMap.size() + 1);
+        if (this.charset != null && !paramMap.containsKey("charset")) {
+            newParams.add(new BasicNameValuePair("charset", this.charset.name()));
+        }
+        for (Map.Entry<String, String> entry: paramMap.entrySet()) {
+            newParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
+        }
+        return create(this.getMimeType(), newParams.toArray(new NameValuePair[newParams.size()]), true);
+    }
+
 }

Modified: httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/entity/TestContentType.java?rev=1643711&r1=1643710&r2=1643711&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/entity/TestContentType.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/entity/TestContentType.java Sun Dec  7 19:20:09 2014
@@ -30,9 +30,11 @@ package org.apache.http.entity;
 import java.nio.charset.Charset;
 import java.nio.charset.UnsupportedCharsetException;
 
+import org.apache.http.Consts;
 import org.apache.http.Header;
 import org.apache.http.ParseException;
 import org.apache.http.message.BasicHeader;
+import org.apache.http.message.BasicNameValuePair;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -46,7 +48,7 @@ public class TestContentType {
     public void testBasis() throws Exception {
         final ContentType contentType = ContentType.create("text/plain", "US-ASCII");
         Assert.assertEquals("text/plain", contentType.getMimeType());
-        Assert.assertEquals("US-ASCII", contentType.getCharset().name());
+        Assert.assertEquals(Consts.ASCII, contentType.getCharset());
         Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
     }
 
@@ -54,7 +56,7 @@ public class TestContentType {
     public void testWithCharset() throws Exception {
         ContentType contentType = ContentType.create("text/plain", "US-ASCII");
         Assert.assertEquals("text/plain", contentType.getMimeType());
-        Assert.assertEquals("US-ASCII", contentType.getCharset().name());
+        Assert.assertEquals(Consts.ASCII, contentType.getCharset());
         Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
         contentType = contentType.withCharset(Charset.forName("UTF-8"));
         Assert.assertEquals("text/plain", contentType.getMimeType());
@@ -66,11 +68,11 @@ public class TestContentType {
     public void testWithCharsetString() throws Exception {
         ContentType contentType = ContentType.create("text/plain", "US-ASCII");
         Assert.assertEquals("text/plain", contentType.getMimeType());
-        Assert.assertEquals("US-ASCII", contentType.getCharset().name());
+        Assert.assertEquals(Consts.ASCII, contentType.getCharset());
         Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
         contentType = contentType.withCharset("UTF-8");
         Assert.assertEquals("text/plain", contentType.getMimeType());
-        Assert.assertEquals("UTF-8", contentType.getCharset().name());
+        Assert.assertEquals(Consts.UTF_8, contentType.getCharset());
         Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString());
     }
 
@@ -78,7 +80,7 @@ public class TestContentType {
     public void testLowCaseText() throws Exception {
         final ContentType contentType = ContentType.create("Text/Plain", "ascii");
         Assert.assertEquals("text/plain", contentType.getMimeType());
-        Assert.assertEquals("US-ASCII", contentType.getCharset().name());
+        Assert.assertEquals(Consts.ASCII, contentType.getCharset());
     }
 
     @Test
@@ -113,7 +115,7 @@ public class TestContentType {
     public void testParse() throws Exception {
         final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\"");
         Assert.assertEquals("text/plain", contentType.getMimeType());
-        Assert.assertEquals("US-ASCII", contentType.getCharset().name());
+        Assert.assertEquals(Consts.ASCII, contentType.getCharset());
         Assert.assertEquals("text/plain; charset=ascii", contentType.toString());
     }
 
@@ -122,7 +124,7 @@ public class TestContentType {
         final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\"; " +
                 "p0 ; p1 = \"blah-blah\"  ; p2 = \" yada yada \" ");
         Assert.assertEquals("text/plain", contentType.getMimeType());
-        Assert.assertEquals("US-ASCII", contentType.getCharset().name());
+        Assert.assertEquals(Consts.ASCII, contentType.getCharset());
         Assert.assertEquals("text/plain; charset=ascii; p0; p1=blah-blah; p2=\" yada yada \"",
                 contentType.toString());
         Assert.assertEquals(null, contentType.getParameter("p0"));
@@ -173,7 +175,7 @@ public class TestContentType {
         final ContentType contentType = ContentType.get(httpentity);
         Assert.assertNotNull(contentType);
         Assert.assertEquals("text/plain", contentType.getMimeType());
-        Assert.assertEquals("UTF-8", contentType.getCharset().name());
+        Assert.assertEquals(Consts.UTF_8, contentType.getCharset());
     }
 
     @Test
@@ -215,4 +217,34 @@ public class TestContentType {
         Assert.assertEquals(null, contentType.getCharset());
     }
 
+    @Test
+    public void testWithParams() throws Exception {
+        ContentType contentType = ContentType.create("text/plain",
+                new BasicNameValuePair("charset", "UTF-8"),
+                new BasicNameValuePair("p", "this"),
+                new BasicNameValuePair("p", "that"));
+        Assert.assertEquals("text/plain", contentType.getMimeType());
+        Assert.assertEquals(Consts.UTF_8, contentType.getCharset());
+        Assert.assertEquals("text/plain; charset=UTF-8; p=this; p=that", contentType.toString());
+
+        contentType = contentType.withParameters(
+                new BasicNameValuePair("charset", "ascii"),
+                new BasicNameValuePair("p", "this and that"));
+        Assert.assertEquals("text/plain", contentType.getMimeType());
+        Assert.assertEquals(Consts.ASCII, contentType.getCharset());
+        Assert.assertEquals("text/plain; charset=ascii; p=\"this and that\"", contentType.toString());
+
+        contentType = ContentType.create("text/blah").withParameters(
+                new BasicNameValuePair("p", "blah"));
+        Assert.assertEquals("text/blah", contentType.getMimeType());
+        Assert.assertEquals(null, contentType.getCharset());
+        Assert.assertEquals("text/blah; p=blah", contentType.toString());
+
+        contentType = ContentType.create("text/blah", Consts.ISO_8859_1).withParameters(
+                new BasicNameValuePair("p", "blah"));
+        Assert.assertEquals("text/blah", contentType.getMimeType());
+        Assert.assertEquals(Consts.ISO_8859_1, contentType.getCharset());
+        Assert.assertEquals("text/blah; charset=ISO-8859-1; p=blah", contentType.toString());
+    }
+
 }