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 2012/01/13 15:05:24 UTC

svn commit: r1231075 - in /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client: entity/UrlEncodedFormEntity.java utils/URLEncodedUtils.java

Author: olegk
Date: Fri Jan 13 14:05:24 2012
New Revision: 1231075

URL: http://svn.apache.org/viewvc?rev=1231075&view=rev
Log:
Added convenience #create methods to UrlEncodedFormEntity

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java?rev=1231075&r1=1231074&r2=1231075&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java Fri Jan 13 14:05:24 2012
@@ -33,6 +33,7 @@ import org.apache.http.annotation.NotThr
 
 import org.apache.http.NameValuePair;
 import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.entity.ContentType;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.protocol.HTTP;
 
@@ -45,20 +46,38 @@ import org.apache.http.protocol.HTTP;
 @NotThreadSafe // AbstractHttpEntity is not thread-safe
 public class UrlEncodedFormEntity extends StringEntity {
 
+    public static UrlEncodedFormEntity create(
+            final Iterable <? extends NameValuePair> parameters, final String charset) {
+        try {
+            return new UrlEncodedFormEntity(parameters, charset);
+        } catch (UnsupportedEncodingException ex) {
+            throw new IllegalArgumentException(ex.getMessage(), ex);
+        }
+    }
+    
+    public static UrlEncodedFormEntity create(
+            final Iterable <? extends NameValuePair> parameters) {
+        try {
+            return new UrlEncodedFormEntity(parameters, null);
+        } catch (UnsupportedEncodingException ex) {
+            throw new IllegalArgumentException(ex.getMessage(), ex);
+        }
+    }
+
     /**
      * Constructs a new {@link UrlEncodedFormEntity} with the list
      * of parameters in the specified encoding.
      *
      * @param parameters list of name/value pairs
-     * @param encoding encoding the name/value pairs be encoded with
+     * @param charset encoding the name/value pairs be encoded with
      * @throws UnsupportedEncodingException if the encoding isn't supported
      */
     public UrlEncodedFormEntity (
         final List <? extends NameValuePair> parameters,
-        final String encoding) throws UnsupportedEncodingException {
-        super(URLEncodedUtils.format(parameters, encoding), encoding);
-        setContentType(URLEncodedUtils.CONTENT_TYPE + HTTP.CHARSET_PARAM +
-                (encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET));
+        final String charset) throws UnsupportedEncodingException {
+        super(URLEncodedUtils.format(parameters, 
+                charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET), 
+                ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
     }
 
     /**
@@ -66,17 +85,17 @@ public class UrlEncodedFormEntity extend
      * of parameters in the specified encoding.
      *
      * @param parameters iterable collection of name/value pairs
-     * @param encoding encoding the name/value pairs be encoded with
+     * @param charset encoding the name/value pairs be encoded with
      * @throws UnsupportedEncodingException if the encoding isn't supported
      *
      * @since 4.2
      */
     public UrlEncodedFormEntity (
         final Iterable <? extends NameValuePair> parameters,
-        final String encoding) throws UnsupportedEncodingException {
-        super(URLEncodedUtils.format(parameters, encoding), encoding);
-        setContentType(URLEncodedUtils.CONTENT_TYPE + HTTP.CHARSET_PARAM +
-                (encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET));
+        final String charset) throws UnsupportedEncodingException {
+        super(URLEncodedUtils.format(parameters, 
+                charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET), 
+                ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
     }
 
     /**
@@ -88,7 +107,7 @@ public class UrlEncodedFormEntity extend
      */
     public UrlEncodedFormEntity (
         final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
-        this(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
+        this(parameters, null);
     }
 
     /**
@@ -102,7 +121,7 @@ public class UrlEncodedFormEntity extend
      */
     public UrlEncodedFormEntity (
         final Iterable <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
-        this(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
+        this(parameters, null);
     }
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java?rev=1231075&r1=1231074&r2=1231075&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java Fri Jan 13 14:05:24 2012
@@ -244,17 +244,17 @@ public class URLEncodedUtils {
      * list of parameters in an HTTP PUT or HTTP POST.
      *
      * @param parameters  The parameters to include.
-     * @param encoding The encoding to use.
+     * @param charset The encoding to use.
      *
      * @since 4.2
      */
     public static String format (
             final Iterable<? extends NameValuePair> parameters,
-            final String encoding) {
+            final String charset) {
         final StringBuilder result = new StringBuilder();
         for (final NameValuePair parameter : parameters) {
-            final String encodedName = encode(parameter.getName(), encoding);
-            final String encodedValue = encode(parameter.getValue(), encoding);
+            final String encodedName = encode(parameter.getName(), charset);
+            final String encodedValue = encode(parameter.getValue(), charset);
             if (result.length() > 0) {
                 result.append(PARAMETER_SEPARATOR);
             }
@@ -267,25 +267,25 @@ public class URLEncodedUtils {
         return result.toString();
     }
 
-    private static String decode (final String content, final String encoding) {
+    private static String decode (final String content, final String charset) {
         if (content == null) {
             return null;
         }
         try {
             return URLDecoder.decode(content,
-                    encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
+                    charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET);
         } catch (UnsupportedEncodingException problem) {
             throw new IllegalArgumentException(problem);
         }
     }
 
-    private static String encode (final String content, final String encoding) {
+    private static String encode (final String content, final String charset) {
         if (content == null) {
             return null;
         }
         try {
             return URLEncoder.encode(content,
-                    encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
+                    charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET);
         } catch (UnsupportedEncodingException problem) {
             throw new IllegalArgumentException(problem);
         }