hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1478742 - in /httpcomponents/httpcore/trunk: RELEASE_NOTES.txt httpcore/src/main/java/org/apache/http/entity/ContentType.java httpcore/src/test/java/org/apache/http/entity/TestContentType.java
Date Fri, 03 May 2013 12:02:35 GMT
Author: olegk
Date: Fri May  3 12:02:34 2013
New Revision: 1478742

URL: http://svn.apache.org/r1478742
Log:
HTTPCORE-300: ContentType to provide support for custom parameters

Modified:
    httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java

Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=1478742&r1=1478741&r2=1478742&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Fri May  3 12:02:34 2013
@@ -1,6 +1,9 @@
 Changes since Release 4.3-BETA1
 -------------------
 
+* [HTTPCORE-300] ContentType to provide support for custom parameters.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * Non-blocking connection pool to avoid scanning the entire queue of pending connection requests

   on each connection lease / release operation (under heavy load the request queue can contain

   a significant number of pending requests, a full linear scan of which can cause massive


Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java?rev=1478742&r1=1478741&r2=1478742&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
Fri May  3 12:02:34 2013
@@ -39,8 +39,11 @@ import org.apache.http.HttpEntity;
 import org.apache.http.NameValuePair;
 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.ParserCursor;
 import org.apache.http.util.Args;
+import org.apache.http.util.CharArrayBuffer;
 import org.apache.http.util.TextUtils;
 
 /**
@@ -89,17 +92,23 @@ public final class ContentType implement
 
     private final String mimeType;
     private final Charset charset;
+    private final NameValuePair[] params;
 
-    /**
-     * Given a MIME type and a character set, constructs a ContentType.
-     * @param mimeType The MIME type to use for the ContentType header.
-     * @param charset The optional character set to use with the ContentType header.
-     * @throws  UnsupportedCharsetException
-     *          If no support for the named charset is available in this Java virtual machine
-     */
-    ContentType(final String mimeType, final Charset charset) {
+    ContentType(
+            final String mimeType,
+            final Charset charset) {
         this.mimeType = mimeType;
         this.charset = charset;
+        this.params = null;
+    }
+
+    ContentType(
+            final String mimeType,
+            final NameValuePair[] params) throws UnsupportedCharsetException {
+        this.mimeType = mimeType;
+        this.params = params;
+        final String s = getParameter("charset");
+        this.charset = s != null ? Charset.forName(s) : null;
     }
 
     public String getMimeType() {
@@ -111,14 +120,33 @@ public final class ContentType implement
     }
 
     /**
-     * Converts a ContentType to a string which can be used as a ContentType header.
-     * If a charset is provided by the ContentType, it will be included in the string.
+     * @since 4.3
+     */
+    public String getParameter(final String name) {
+        Args.notEmpty(name, "Parameter name");
+        if (this.params == null) {
+            return null;
+        }
+        for (final NameValuePair param: this.params) {
+            if (param.getName().equalsIgnoreCase(name)) {
+                return param.getValue();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Generates textual representation of this content type which can be used as the value
+     * of a <code>Content-Type</code> header.
      */
     @Override
     public String toString() {
-        final StringBuilder buf = new StringBuilder();
+        final CharArrayBuffer buf = new CharArrayBuffer(64);
         buf.append(this.mimeType);
-        if (this.charset != null) {
+        if (this.params != null) {
+            buf.append("; ");
+            BasicHeaderValueFormatter.INSTANCE.formatParameters(buf, this.params, false);
+        } else if (this.charset != null) {
             buf.append("; charset=");
             buf.append(this.charset.name());
         }
@@ -178,12 +206,8 @@ public final class ContentType implement
 
     private static ContentType create(final HeaderElement helem) {
         final String mimeType = helem.getName();
-        String charset = null;
-        final NameValuePair param = helem.getParameterByName("charset");
-        if (param != null) {
-            charset = param.getValue();
-        }
-        return create(mimeType, charset);
+        final NameValuePair[] params = helem.getParameters();
+        return new ContentType(mimeType, params != null && params.length > 0 ?
params : null);
     }
 
     /**
@@ -199,7 +223,10 @@ public final class ContentType implement
     public static ContentType parse(
             final String s) throws ParseException, UnsupportedCharsetException {
         Args.notNull(s, "Content type");
-        final HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null);
+        final CharArrayBuffer buf = new CharArrayBuffer(s.length());
+        buf.append(s);
+        final ParserCursor cursor = new ParserCursor(0, s.length());
+        final HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(buf,
cursor);
         if (elements.length > 0) {
             return create(elements[0]);
         } else {

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java?rev=1478742&r1=1478741&r2=1478742&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
Fri May  3 12:02:34 2013
@@ -113,7 +113,21 @@ public class TestContentType {
         final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\"");
         Assert.assertEquals("text/plain", contentType.getMimeType());
         Assert.assertEquals("US-ASCII", contentType.getCharset().name());
-        Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
+        Assert.assertEquals("text/plain; charset=ascii", contentType.toString());
+    }
+
+    @Test
+    public void testParseMultiparam() throws Exception {
+        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("text/plain; charset=ascii; p0; p1=blah-blah; p2=\" yada yada
\"",
+                contentType.toString());
+        Assert.assertEquals(null, contentType.getParameter("p0"));
+        Assert.assertEquals("blah-blah", contentType.getParameter("p1"));
+        Assert.assertEquals(" yada yada ", contentType.getParameter("p2"));
+        Assert.assertEquals(null, contentType.getParameter("p3"));
     }
 
     @Test



Mime
View raw message