Index: apache/http/client/methods/UrlEncodedFormEntity.java
===================================================================
--- apache/http/client/methods/UrlEncodedFormEntity.java (revision 632259)
+++ apache/http/client/methods/UrlEncodedFormEntity.java (working copy)
@@ -1,98 +1,18 @@
-/*
- * $Header$
- * $Revision$
- * $Date$
- *
- * ====================================================================
- *
- * 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
- * .
- *
- */
-
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.io.*;
+import java.util.*;
+import org.apache.http.client.utils.*;
+import org.apache.http.entity.*;
+import org.apache.http.protocol.*;
-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.protocol.HTTP;
-import org.apache.http.util.EncodingUtils;
-
-public class UrlEncodedFormEntity extends AbstractHttpEntity {
-
- /** 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);
+public class UrlEncodedFormEntity extends StringEntity {
+ public UrlEncodedFormEntity (final Map > parameters, final String encoding) throws UnsupportedEncodingException {
+ super(UrlEncodedUtils.format(parameters, encoding), HTTP.US_ASCII);
+ setContentType(UrlEncodedUtils.CONTENT_TYPE);
}
-
- public UrlEncodedFormEntity(final NameValuePair[] fields) {
- super();
- String s = URLUtils.simpleFormUrlEncode(fields, HTTP.UTF_8);
- this.content = EncodingUtils.getAsciiBytes(s);
- }
-
- public boolean isRepeatable() {
- return true;
- }
-
- public long getContentLength() {
- return this.content.length;
- }
- public InputStream getContent() throws IOException {
- return new ByteArrayInputStream(this.content);
+ public UrlEncodedFormEntity (final Map > parameters) throws UnsupportedEncodingException {
+ this(parameters, null);
}
-
- @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();
- }
-
}
Index: apache/http/client/utils/UrlEncodedUtils.java
===================================================================
--- apache/http/client/utils/UrlEncodedUtils.java (revision 0)
+++ apache/http/client/utils/UrlEncodedUtils.java (revision 0)
@@ -0,0 +1,82 @@
+package org.apache.http.client.utils;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import org.apache.http.*;
+import org.apache.http.protocol.*;
+import org.apache.http.util.*;
+
+public final class UrlEncodedUtils {
+ public static final String CONTENT_TYPE = "application/x-www-form-urlencoded";
+ private static final String PARAMETER_SEPARATOR = "&";
+ private static final String NAME_VALUE_SEPARATOR = "=";
+
+ public static Map > parse (final URI uri, final String encoding) {
+ Map > result = Collections.emptyMap();
+ final String query = uri.getRawQuery();
+ if (query != null && query.length() > 0) {
+ result = new TreeMap >();
+ parse(result, new Scanner(query), encoding);
+ }
+ return result;
+ }
+
+ public static Map > parse (final HttpEntity entity) throws IOException {
+ Map > result = Collections.emptyMap();
+ if (isEncoded(entity)) {
+ final String content = EntityUtils.toString(entity);
+ if (content != null && content.length() > 0) {
+ result = new TreeMap >();
+ parse(result, new Scanner(content), null);
+ }
+ }
+ return result;
+ }
+
+ public static boolean isEncoded (final HttpEntity entity) {
+ final Header contentType = entity.getContentType();
+ return (contentType != null && contentType.getValue().equalsIgnoreCase(CONTENT_TYPE));
+ }
+
+ public static void parse (final Map > result, final Scanner scanner, final String encoding) {
+ 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");
+ try {
+ final String name = URLDecoder.decode(nameValue[0], encoding != null ? encoding : HTTP.DEFAULT_CONTENT_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], encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
+ values.add(value);
+ }
+ } catch (UnsupportedEncodingException problem) {
+ throw new IllegalArgumentException(problem);
+ }
+ }
+ }
+
+ public static String format (final Map > parameters, final String encoding) throws UnsupportedEncodingException {
+ final StringBuilder result = new StringBuilder(64);
+ for (final String name : parameters.keySet()) {
+ final List extends String> values = parameters.get(name);
+ if (values != null) {
+ final String encodedName = URLEncoder.encode(name, encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
+ for (final String value : values) {
+ if (result.length() > 0)
+ result.append(PARAMETER_SEPARATOR);
+ final String encodedValue = URLEncoder.encode(value, encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
+ result.append(encodedName);
+ result.append(NAME_VALUE_SEPARATOR);
+ result.append(encodedValue);
+ }
+ }
+ }
+ return result.toString();
+ }
+}