diff --git a/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java b/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java index 1247982ec..4929b35e6 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java +++ b/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java @@ -30,69 +30,45 @@ package org.apache.http.client.methods; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.Map; -import org.apache.http.Header; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLUtils; -import org.apache.http.entity.AbstractHttpEntity; -import org.apache.http.message.BasicHeader; +import org.apache.http.entity.StringEntity; import org.apache.http.protocol.HTTP; -import org.apache.http.util.EncodingUtils; -public class UrlEncodedFormEntity extends AbstractHttpEntity { +public class UrlEncodedFormEntity extends StringEntity { /** The Content-Type for www-form-urlencoded. */ public static final String FORM_URL_ENCODED_CONTENT_TYPE = "application/x-www-form-urlencoded"; - private final byte[] content; - public UrlEncodedFormEntity( final NameValuePair[] fields, final String charset) throws UnsupportedEncodingException { - super(); - String s = URLUtils.formUrlEncode(fields, charset); - this.content = EncodingUtils.getAsciiBytes(s); + super(URLUtils.formUrlEncode(fields, charset), charset); + setContentType(FORM_URL_ENCODED_CONTENT_TYPE); } - public UrlEncodedFormEntity(final NameValuePair[] fields) { - super(); - String s = URLUtils.simpleFormUrlEncode(fields, HTTP.UTF_8); - this.content = EncodingUtils.getAsciiBytes(s); + public UrlEncodedFormEntity( + final NameValuePair[] fields) throws UnsupportedEncodingException { + super(URLUtils.formUrlEncode(fields, HTTP.UTF_8), HTTP.US_ASCII); + setContentType(FORM_URL_ENCODED_CONTENT_TYPE); } - public boolean isRepeatable() { - return true; + public UrlEncodedFormEntity ( + final Map> parameters, + final String charset) throws UnsupportedEncodingException { + super(URLUtils.format(parameters, charset), HTTP.US_ASCII); + setContentType(FORM_URL_ENCODED_CONTENT_TYPE); } - public long getContentLength() { - return this.content.length; - } - - public InputStream getContent() throws IOException { - return new ByteArrayInputStream(this.content); + public UrlEncodedFormEntity ( + final Map> parameters) throws UnsupportedEncodingException { + super(URLUtils.format(parameters, HTTP.UTF_8), HTTP.US_ASCII); + setContentType(FORM_URL_ENCODED_CONTENT_TYPE); } - @Override - public Header getContentType() { - return new BasicHeader(HTTP.CONTENT_TYPE, FORM_URL_ENCODED_CONTENT_TYPE); - } - - public boolean isStreaming() { - return false; - } - - public void writeTo(final OutputStream outstream) throws IOException { - if (outstream == null) { - throw new IllegalArgumentException("Output stream may not be null"); - } - outstream.write(this.content); - outstream.flush(); - } - } diff --git a/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java b/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java index 31cb8d226..73a101551 100644 --- a/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java +++ b/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java @@ -32,10 +32,18 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import java.util.TreeMap; -import org.apache.commons.codec.net.URLCodec; import org.apache.http.HttpHost; import org.apache.http.NameValuePair; +import org.apache.http.protocol.HTTP; /** * The home for utility methods that handle various URL encoding tasks. @@ -49,6 +57,8 @@ public class URLUtils { /** Default content encoding chatset */ private static final String DEFAULT_CHARSET = "ISO-8859-1"; + private static final String PARAMETER_SEPARATOR = "&"; + private static final String NAME_VALUE_SEPARATOR = "="; /** * Form-urlencoding routine. @@ -109,23 +119,81 @@ public static String formUrlEncode( final String charset) throws UnsupportedEncodingException { StringBuilder buf = new StringBuilder(); for (int i = 0; i < pairs.length; i++) { - URLCodec codec = new URLCodec(); NameValuePair pair = pairs[i]; if (pair.getName() != null) { if (i > 0) { buf.append("&"); } - buf.append(codec.encode(pair.getName(), charset)); + buf.append(URLEncoder.encode(pair.getName(), charset)); buf.append("="); if (pair.getValue() != null) { - buf.append(codec.encode(pair.getValue(), charset)); + buf.append(URLEncoder.encode(pair.getValue(), charset)); } } } return buf.toString(); } - public static URI createURI( + public static Map > parse ( + final URI uri, + String charset) throws UnsupportedEncodingException { + Map > result = Collections.emptyMap(); + final String query = uri.getRawQuery(); + if (query != null && query.length() > 0) { + result = new TreeMap >(); + parse(result, new Scanner(query), charset); + } + return result; + } + + public static void parse ( + final Map > result, + final Scanner scanner, String charset) throws UnsupportedEncodingException { + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_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 = URLDecoder.decode(nameValue[0], charset); + if (nameValue.length == 2) { + if (!result.containsKey(name)) + result.put(name, new LinkedList ()); + String value = null; + final List values = result.get(name); + value = URLDecoder.decode(nameValue[1], charset); + values.add(value); + } + } + } + + public static String format ( + final Map > parameters, + String charset) throws UnsupportedEncodingException { + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_CHARSET; + } + final StringBuilder result = new StringBuilder(64); + for (final String name : parameters.keySet()) { + final List values = parameters.get(name); + if (values != null) { + final String encodedName = URLEncoder.encode(name, charset); + for (final String value : values) { + if (result.length() > 0) + result.append(PARAMETER_SEPARATOR); + final String encodedValue = URLEncoder.encode(value, charset); + result.append(encodedName); + result.append(NAME_VALUE_SEPARATOR); + result.append(encodedValue); + } + } + } + return result.toString(); + } + + public static URI createURI( final String scheme, final String host, int port,