hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1599798 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/client/methods/ main/java/org/apache/http/client/utils/ test/java/org/apache/http/client/utils/
Date Tue, 03 Jun 2014 21:08:41 GMT
Author: olegk
Date: Tue Jun  3 21:08:41 2014
New Revision: 1599798

URL: http://svn.apache.org/r1599798
Log:
https://github.com/apache/httpclient/pull/12
Configurable charset for request and URI builders
Contributed by isabelabel <bel at jusbrasil.com.br>

Added:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestRequestBuilder.java
  (with props)
Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java?rev=1599798&r1=1599797&r2=1599798&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java
Tue Jun  3 21:08:41 2014
@@ -30,10 +30,12 @@ package org.apache.http.client.methods;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.http.Consts;
 import org.apache.http.Header;
 import org.apache.http.HeaderIterator;
 import org.apache.http.HttpEntity;
@@ -68,6 +70,7 @@ import org.apache.http.util.Args;
 public class RequestBuilder {
 
     private String method;
+    private Charset charset;
     private ProtocolVersion version;
     private URI uri;
     private HeaderGroup headergroup;
@@ -77,6 +80,7 @@ public class RequestBuilder {
 
     RequestBuilder(final String method) {
         super();
+        this.charset = Consts.UTF_8;
         this.method = method;
     }
 
@@ -297,6 +301,21 @@ public class RequestBuilder {
         return this;
     }
 
+    /**
+     * @since 4.4
+     */
+    public RequestBuilder setCharset(final Charset charset) {
+        this.charset = charset;
+        return this;
+    }
+
+    /**
+     * @since 4.4
+     */
+    public Charset getCharset() {
+        return charset;
+    }
+
     public String getMethod() {
         return method;
     }
@@ -442,7 +461,10 @@ public class RequestBuilder {
                 entityCopy = new UrlEncodedFormEntity(parameters, HTTP.DEF_CONTENT_CHARSET);
             } else {
                 try {
-                    uriNotNull = new URIBuilder(uriNotNull).addParameters(parameters).build();
+                    uriNotNull = new URIBuilder(uriNotNull)
+                      .setCharset(this.charset)
+                      .addParameters(parameters)
+                      .build();
                 } catch (final URISyntaxException ex) {
                     // should never happen
                 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java?rev=1599798&r1=1599797&r2=1599798&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
Tue Jun  3 21:08:41 2014
@@ -59,6 +59,7 @@ public class URIBuilder {
     private String encodedQuery;
     private List<NameValuePair> queryParams;
     private String query;
+    private Charset charset;
     private String fragment;
     private String encodedFragment;
 
@@ -90,6 +91,21 @@ public class URIBuilder {
         digestURI(uri);
     }
 
+    /**
+     * @since 4.4
+     */
+    public URIBuilder setCharset(final Charset charset) {
+        this.charset = charset;
+        return this;
+    }
+
+    /**
+     * @since 4.4
+     */
+    public Charset getCharset() {
+        return charset;
+    }
+
     private List <NameValuePair> parseQuery(final String query, final Charset charset)
{
         if (query != null && !query.isEmpty()) {
             return URLEncodedUtils.parse(query, charset);
@@ -162,25 +178,25 @@ public class URIBuilder {
         this.encodedPath = uri.getRawPath();
         this.path = uri.getPath();
         this.encodedQuery = uri.getRawQuery();
-        this.queryParams = parseQuery(uri.getRawQuery(), Consts.UTF_8);
+        this.queryParams = parseQuery(uri.getRawQuery(), this.charset != null ? this.charset
: Consts.UTF_8);
         this.encodedFragment = uri.getRawFragment();
         this.fragment = uri.getFragment();
     }
 
     private String encodeUserInfo(final String userInfo) {
-        return URLEncodedUtils.encUserInfo(userInfo, Consts.UTF_8);
+        return URLEncodedUtils.encUserInfo(userInfo, this.charset != null ? this.charset
: Consts.UTF_8);
     }
 
     private String encodePath(final String path) {
-        return URLEncodedUtils.encPath(path, Consts.UTF_8);
+        return URLEncodedUtils.encPath(path, this.charset != null ? this.charset : Consts.UTF_8);
     }
 
     private String encodeUrlForm(final List<NameValuePair> params) {
-        return URLEncodedUtils.format(params, Consts.UTF_8);
+        return URLEncodedUtils.format(params, this.charset != null ? this.charset : Consts.UTF_8);
     }
 
     private String encodeUric(final String fragment) {
-        return URLEncodedUtils.encUric(fragment, Consts.UTF_8);
+        return URLEncodedUtils.encUric(fragment, this.charset != null ? this.charset : Consts.UTF_8);
     }
 
     /**
@@ -263,7 +279,7 @@ public class URIBuilder {
      */
     @Deprecated
     public URIBuilder setQuery(final String query) {
-        this.queryParams = parseQuery(query, Consts.UTF_8);
+        this.queryParams = parseQuery(query, this.charset != null ? this.charset : Consts.UTF_8);
         this.query = null;
         this.encodedQuery = null;
         this.encodedSchemeSpecificPart = null;

Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestRequestBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestRequestBuilder.java?rev=1599798&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestRequestBuilder.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestRequestBuilder.java
Tue Jun  3 21:08:41 2014
@@ -0,0 +1,74 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.client.utils;
+
+
+import org.apache.http.Consts;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.methods.RequestBuilder;
+import org.apache.http.message.BasicNameValuePair;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
+
+public class TestRequestBuilder {
+
+    @Test
+    public void testBuildGETwithUTF8() throws Exception {
+        assertBuild(Consts.UTF_8);
+    }
+
+    @Test
+    public void testBuildGETwithISO88591() throws Exception {
+        assertBuild(Consts.ISO_8859_1);
+    }
+
+    private void assertBuild(final Charset charset) throws Exception {
+        final RequestBuilder requestBuilder = RequestBuilder.create("GET").setCharset(charset);
+        requestBuilder.setUri("https://somehost.com/stuff");
+        requestBuilder.addParameters(createParameters());
+
+        final String encodedData1 = URLEncoder.encode("\"1\u00aa position\"", charset.displayName());
+        final String encodedData2 = URLEncoder.encode("Jos\u00e9 Abra\u00e3o", charset.displayName());
+
+        final String uriExpected = String.format("https://somehost.com/stuff?parameter1=value1&parameter2=%s&parameter3=%s",
encodedData1, encodedData2);
+
+        final HttpUriRequest request = requestBuilder.build();
+        Assert.assertEquals(uriExpected, request.getURI().toString());
+    }
+
+    private NameValuePair[] createParameters() {
+        final NameValuePair parameters[] = new NameValuePair[3];
+        parameters[0] = new BasicNameValuePair("parameter1", "value1");
+        parameters[1] = new BasicNameValuePair("parameter2", "\"1\u00aa position\"");
+        parameters[2] = new BasicNameValuePair("parameter3", "Jos\u00e9 Abra\u00e3o");
+        return parameters;
+    }
+}

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestRequestBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestRequestBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestRequestBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java?rev=1599798&r1=1599797&r2=1599798&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
Tue Jun  3 21:08:41 2014
@@ -27,7 +27,14 @@
 package org.apache.http.client.utils;
 
 import java.net.URI;
-
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.http.Consts;
+import org.apache.http.NameValuePair;
+import org.apache.http.message.BasicNameValuePair;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -232,4 +239,57 @@ public class TestURIBuilder {
 
     }
 
+    @Test
+    public void testBuildAddParametersUTF8() throws Exception {
+        assertAddParameters(Consts.UTF_8);
+    }
+
+    @Test
+    public void testBuildAddParametersISO88591() throws Exception {
+        assertAddParameters(Consts.ISO_8859_1);
+    }
+
+    public void assertAddParameters(final Charset charset) throws Exception {
+        final URI uri = new URIBuilder("https://somehost.com/stuff")
+                .setCharset(charset)
+                .addParameters(createParameters()).build();
+
+        assertBuild(charset, uri);
+    }
+
+    @Test
+    public void testBuildSetParametersUTF8() throws Exception {
+        assertSetParameters(Consts.UTF_8);
+    }
+
+    @Test
+    public void testBuildSetParametersISO88591() throws Exception {
+        assertSetParameters(Consts.ISO_8859_1);
+    }
+
+    public void assertSetParameters(final Charset charset) throws Exception {
+        final URI uri = new URIBuilder("https://somehost.com/stuff")
+                .setCharset(charset)
+                .setParameters(createParameters()).build();
+
+        assertBuild(charset, uri);
+    }
+
+    public void assertBuild(final Charset charset, final URI uri) throws Exception {
+        final String encodedData1 = URLEncoder.encode("\"1\u00aa position\"", charset.displayName());
+        final String encodedData2 = URLEncoder.encode("Jos\u00e9 Abra\u00e3o", charset.displayName());
+
+        final String uriExpected = String.format("https://somehost.com/stuff?parameter1=value1&parameter2=%s&parameter3=%s",
encodedData1, encodedData2);
+
+        Assert.assertEquals(uriExpected, uri.toString());
+    }
+
+    private List<NameValuePair> createParameters() {
+        final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
+        parameters.add(new BasicNameValuePair("parameter1", "value1"));
+        parameters.add(new BasicNameValuePair("parameter2", "\"1\u00aa position\""));
+        parameters.add(new BasicNameValuePair("parameter3", "Jos\u00e9 Abra\u00e3o"));
+        return parameters;
+    }
+
 }



Mime
View raw message