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/03/19 15:47:45 UTC
svn commit: r1302482 - in /httpcomponents/httpclient/trunk/httpclient/src:
main/java/org/apache/http/client/utils/URLEncodedUtils.java
test/java/org/apache/http/client/utils/TestURLEncodedUtils.java
Author: olegk
Date: Mon Mar 19 14:47:44 2012
New Revision: 1302482
URL: http://svn.apache.org/viewvc?rev=1302482&view=rev
Log:
Un-deprecated #parse method that takes Scanner as a parameter
Modified:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java
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=1302482&r1=1302481&r2=1302482&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 Mon Mar 19 14:47:44 2012
@@ -99,15 +99,17 @@ public class URLEncodedUtils {
*/
public static List <NameValuePair> parse (
final HttpEntity entity) throws IOException {
- List <NameValuePair> result = null;
+ List <NameValuePair> result = new ArrayList<NameValuePair>();
ContentType contentType = ContentType.get(entity);
if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
String content = EntityUtils.toString(entity, HTTP.ASCII);
if (content != null && content.length() > 0) {
- result = parse(content, contentType.getCharset());
+ Scanner scanner = new Scanner(entity.getContent(), HTTP.ASCII);
+ parse(result, scanner, contentType.getCharset() != null ?
+ contentType.getCharset() : HTTP.DEFAULT_CONTENT_CHARSET);
}
}
- return result != null ? result : new ArrayList<NameValuePair>();
+ return result;
}
/**
@@ -142,24 +144,23 @@ public class URLEncodedUtils {
* Input that contains the parameters to parse.
* @param encoding
* Encoding to use when decoding the parameters.
- *
- * @deprecated use {@link #parse(String, String)}
*/
- @Deprecated
public static void parse (
final List <NameValuePair> parameters,
final Scanner scanner,
- final String encoding) {
+ final String charset) {
scanner.useDelimiter(PARAMETER_SEPARATOR);
while (scanner.hasNext()) {
- final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
- if (nameValue.length == 0 || nameValue.length > 2)
- throw new IllegalArgumentException("bad parameter");
-
- final String name = decode(nameValue[0], encoding);
+ String name = null;
String value = null;
- if (nameValue.length == 2)
- value = decode(nameValue[1], encoding);
+ String token = scanner.next();
+ int i = token.indexOf(NAME_VALUE_SEPARATOR);
+ if (i != -1) {
+ name = decode(token.substring(0, i).trim(), charset);
+ value = decode(token.substring(i + 1).trim(), charset);
+ } else {
+ name = decode(token.trim(), charset);
+ }
parameters.add(new BasicNameValuePair(name, value));
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java?rev=1302482&r1=1302481&r2=1302482&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java Mon Mar 19 14:47:44 2012
@@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
+import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
@@ -136,8 +137,8 @@ public class TestURLEncodedUtils {
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);
- StringEntity entity = new StringEntity(s, HTTP.UTF_8);
- entity.setContentType(URLEncodedUtils.CONTENT_TYPE + HTTP.CHARSET_PARAM + HTTP.UTF_8);
+ StringEntity entity = new StringEntity(s, ContentType.create(
+ URLEncodedUtils.CONTENT_TYPE, HTTP.UTF_8));
List <NameValuePair> result = URLEncodedUtils.parse(entity);
Assert.assertEquals(2, result.size());
assertNameValuePair(result.get(0), "russian", ru_hello);
@@ -145,6 +146,42 @@ public class TestURLEncodedUtils {
}
@Test
+ public void testParseUTF8String () throws Exception {
+ String ru_hello = constructString(RUSSIAN_HELLO);
+ String ch_hello = constructString(SWISS_GERMAN_HELLO);
+ List <NameValuePair> parameters = new ArrayList<NameValuePair>();
+ parameters.add(new BasicNameValuePair("russian", ru_hello));
+ parameters.add(new BasicNameValuePair("swiss", ch_hello));
+
+ String s = URLEncodedUtils.format(parameters, HTTP.UTF_8);
+
+ List <NameValuePair> result = URLEncodedUtils.parse(s, HTTP.UTF_8);
+ Assert.assertEquals(2, result.size());
+ assertNameValuePair(result.get(0), "russian", ru_hello);
+ assertNameValuePair(result.get(1), "swiss", ch_hello);
+ }
+
+ @Test
+ public void testParseEntityDefaultContentType () throws Exception {
+ String ch_hello = constructString(SWISS_GERMAN_HELLO);
+ String us_hello = "hi there";
+ List <NameValuePair> parameters = new ArrayList<NameValuePair>();
+ parameters.add(new BasicNameValuePair("english", us_hello));
+ parameters.add(new BasicNameValuePair("swiss", ch_hello));
+
+ String s = URLEncodedUtils.format(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
+
+ Assert.assertEquals("english=hi+there&swiss=Gr%FCezi_z%E4m%E4", s);
+
+ StringEntity entity = new StringEntity(s, ContentType.create(
+ URLEncodedUtils.CONTENT_TYPE, null));
+ List <NameValuePair> result = URLEncodedUtils.parse(entity);
+ Assert.assertEquals(2, result.size());
+ assertNameValuePair(result.get(0), "english", us_hello);
+ assertNameValuePair(result.get(1), "swiss", ch_hello);
+ }
+
+ @Test
public void testIsEncoded () throws Exception {
final StringEntity entity = new StringEntity("...");