hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1136437 - in /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client: entity/UrlEncodedFormEntity.java utils/URLEncodedUtils.java
Date Thu, 16 Jun 2011 13:50:03 GMT
Author: olegk
Date: Thu Jun 16 13:50:02 2011
New Revision: 1136437

URL: http://svn.apache.org/viewvc?rev=1136437&view=rev
Log:
HTTPCLIENT-1086: additional constructors for UrlEncodedFormEntity that take iterable as input

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=1136437&r1=1136436&r2=1136437&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
Thu Jun 16 13:50:02 2011
@@ -63,6 +63,24 @@ public class UrlEncodedFormEntity extend
 
     /**
      * Constructs a new {@link UrlEncodedFormEntity} with the list
+     * of parameters in the specified encoding.
+     *
+     * @param parameters iterable collection of name/value pairs
+     * @param encoding 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));
+    }
+
+    /**
+     * Constructs a new {@link UrlEncodedFormEntity} with the list
      * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET}
      *
      * @param parameters list of name/value pairs
@@ -73,4 +91,18 @@ public class UrlEncodedFormEntity extend
         this(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
     }
 
+    /**
+     * Constructs a new {@link UrlEncodedFormEntity} with the list
+     * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET}
+     *
+     * @param parameters iterable collection of name/value pairs
+     * @throws UnsupportedEncodingException if the default encoding isn't supported
+     * 
+     * @since 4.2
+     */
+    public UrlEncodedFormEntity (
+        final Iterable <? extends NameValuePair> parameters) throws UnsupportedEncodingException
{
+        this(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
+    }
+
 }

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=1136437&r1=1136436&r2=1136437&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
Thu Jun 16 13:50:02 2011
@@ -199,6 +199,32 @@ public class URLEncodedUtils {
         return result.toString();
     }
 
+    /**
+     * Returns a String that is suitable for use as an <code>application/x-www-form-urlencoded</code>
+     * list of parameters in an HTTP PUT or HTTP POST.
+     *
+     * @param parameters  The parameters to include.
+     * @param encoding The encoding to use.
+     * 
+     * @since 4.2
+     */
+    public static String format (
+            final Iterable<? extends NameValuePair> parameters,
+            final String encoding) {
+        final StringBuilder result = new StringBuilder();
+        for (final NameValuePair parameter : parameters) {
+            final String encodedName = encode(parameter.getName(), encoding);
+            final String value = parameter.getValue();
+            final String encodedValue = value != null ? encode(value, encoding) : "";
+            if (result.length() > 0)
+                result.append(PARAMETER_SEPARATOR);
+            result.append(encodedName);
+            result.append(NAME_VALUE_SEPARATOR);
+            result.append(encodedValue);
+        }
+        return result.toString();
+    }
+
     private static String decode (final String content, final String encoding) {
         try {
             return URLDecoder.decode(content,



Mime
View raw message