Configurable charset for request and URI builders Contributed by isabelabel <bel at jusbrasil.com.br> git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1599798 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8e30f60efe
commit
5ff23f9613
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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¶meter2=%s¶meter3=%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;
|
||||
}
|
||||
}
|
|
@ -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¶meter2=%s¶meter3=%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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue