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 2016/11/19 09:34:54 UTC

svn commit: r1770476 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java

Author: olegk
Date: Sat Nov 19 09:34:53 2016
New Revision: 1770476

URL: http://svn.apache.org/viewvc?rev=1770476&view=rev
Log:
Moved utility method to parse URL encoded entities from HttpClient

Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java?rev=1770476&r1=1770475&r2=1770476&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java Sat Nov 19 09:34:53 2016
@@ -27,9 +27,6 @@
 
 package org.apache.hc.core5.http.io.entity;
 
-import static org.apache.hc.core5.http.ContentType.DEFAULT_TEXT;
-import static org.apache.hc.core5.http.ContentType.parse;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -38,11 +35,15 @@ import java.io.UnsupportedEncodingExcept
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.UnsupportedCharsetException;
+import java.util.Collections;
+import java.util.List;
 
 import org.apache.hc.core5.http.ClassicHttpResponse;
 import org.apache.hc.core5.http.ContentType;
 import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.NameValuePair;
 import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.net.URLEncodedUtils;
 import org.apache.hc.core5.util.Args;
 import org.apache.hc.core5.util.ByteArrayBuffer;
 import org.apache.hc.core5.util.CharArrayBuffer;
@@ -129,7 +130,7 @@ public final class EntityUtils {
         }
         final String contentType = entity.getContentType();
         if (contentType != null) {
-            return parse(contentType);
+            return ContentType.parse(contentType);
         }
         return null;
     }
@@ -167,7 +168,7 @@ public final class EntityUtils {
      */
     public static ContentType getContentTypeOrDefault(final HttpEntity entity) throws UnsupportedCharsetException {
         final ContentType contentType = getContentType(entity);
-        return contentType != null ? contentType : DEFAULT_TEXT;
+        return contentType != null ? contentType : ContentType.DEFAULT_TEXT;
     }
 
     /**
@@ -181,7 +182,7 @@ public final class EntityUtils {
      */
     public static ContentType getContentTypeLenientOrDefault(final HttpEntity entity) throws UnsupportedCharsetException {
         final ContentType contentType = getContentType(entity);
-        return contentType != null ? contentType : DEFAULT_TEXT;
+        return contentType != null ? contentType : ContentType.DEFAULT_TEXT;
     }
 
     /**
@@ -288,7 +289,7 @@ public final class EntityUtils {
                 contentType = contentType.withCharset(defaultCharset);
             }
         } else {
-            contentType = DEFAULT_TEXT.withCharset(defaultCharset);
+            contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset);
         }
         return toString(entity, contentType);
     }
@@ -331,4 +332,48 @@ public final class EntityUtils {
         return toString(entity, getContentType(entity));
     }
 
+    /**
+     * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
+     * The encoding is taken from the entity's Content-Encoding header.
+     * <p>
+     * This is typically used while parsing an HTTP POST.
+     *
+     * @param entity
+     *            The entity to parse
+     * @return a list of {@link NameValuePair} as built from the URI's query portion.
+     * @throws IOException
+     *             If there was an exception getting the entity's data.
+     */
+    public static List<NameValuePair> parse(final HttpEntity entity) throws IOException {
+        Args.notNull(entity, "HTTP entity");
+        final ContentType contentType = EntityUtils.getContentType(entity);
+        if (contentType == null || !contentType.getMimeType().equalsIgnoreCase(URLEncodedUtils.CONTENT_TYPE)) {
+            return Collections.emptyList();
+        }
+        final long len = entity.getContentLength();
+        Args.check(len <= Integer.MAX_VALUE, "HTTP entity is too large");
+        final Charset charset = contentType.getCharset() != null ? contentType.getCharset() : StandardCharsets.ISO_8859_1;
+        final InputStream instream = entity.getContent();
+        if (instream == null) {
+            return Collections.emptyList();
+        }
+        final CharArrayBuffer buf;
+        try {
+            buf = new CharArrayBuffer(len > 0 ? (int) len : 1024);
+            final Reader reader = new InputStreamReader(instream, charset);
+            final char[] tmp = new char[1024];
+            int l;
+            while((l = reader.read(tmp)) != -1) {
+                buf.append(tmp, 0, l);
+            }
+
+        } finally {
+            instream.close();
+        }
+        if (buf.length() == 0) {
+            return Collections.emptyList();
+        }
+        return URLEncodedUtils.parse(buf, charset, '&');
+    }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java?rev=1770476&r1=1770475&r2=1770476&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java Sat Nov 19 09:34:53 2016
@@ -32,8 +32,13 @@ import java.io.InputStream;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.UnsupportedCharsetException;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.http.message.BasicNameValuePair;
+import org.apache.hc.core5.net.URLEncodedUtils;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -276,6 +281,48 @@ public class TestEntityUtils {
         Assert.assertEquals(null, contentType.getCharset());
     }
 
+    private static void assertNameValuePair (
+            final NameValuePair parameter,
+            final String expectedName,
+            final String expectedValue) {
+        Assert.assertEquals(parameter.getName(), expectedName);
+        Assert.assertEquals(parameter.getValue(), expectedValue);
+    }
+
+    @Test
+    public void testParseEntity() throws Exception {
+        final StringEntity entity = new StringEntity("Name1=Value1");
+
+        entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
+        final List<NameValuePair> result = EntityUtils.parse(entity);
+        Assert.assertEquals(1, result.size());
+        assertNameValuePair(result.get(0), "Name1", "Value1");
+
+        entity.setContentType("text/test");
+        Assert.assertTrue(EntityUtils.parse(entity).isEmpty());
+    }
+
+    @Test
+    public void testParseUTF8Entity() throws Exception {
+        final String ru_hello = constructString(RUSSIAN_HELLO);
+        final String ch_hello = constructString(SWISS_GERMAN_HELLO);
+        final List <NameValuePair> parameters = new ArrayList<>();
+        parameters.add(new BasicNameValuePair("russian", ru_hello));
+        parameters.add(new BasicNameValuePair("swiss", ch_hello));
+
+        final String s = URLEncodedUtils.format(parameters, StandardCharsets.UTF_8);
+
+        Assert.assertEquals("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" +
+                "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s);
+
+        final StringEntity entity = new StringEntity(s, ContentType.create(
+                URLEncodedUtils.CONTENT_TYPE, StandardCharsets.UTF_8));
+        final List <NameValuePair> result = EntityUtils.parse(entity);
+        Assert.assertEquals(2, result.size());
+        assertNameValuePair(result.get(0), "russian", ru_hello);
+        assertNameValuePair(result.get(1), "swiss", ch_hello);
+    }
+
     /**
      * Helper class that returns {@code null} as the content.
      */
@@ -292,6 +339,6 @@ public class TestEntityUtils {
         public InputStream getContent() {
             return null;
         }
-    } // class NullEntity
+    }
 
-} // class TestEntityUtils
+}