diff --git a/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java b/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java new file mode 100644 index 000000000..9bb11cc67 --- /dev/null +++ b/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java @@ -0,0 +1,97 @@ +/* + * $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 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 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 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/src/java/org/apache/http/client/utils/URLUtils.java b/src/java/org/apache/http/client/utils/URLUtils.java index b77f0f838..ec4b1e14d 100644 --- a/src/java/org/apache/http/client/utils/URLUtils.java +++ b/src/java/org/apache/http/client/utils/URLUtils.java @@ -71,12 +71,14 @@ public class URLUtils { * * @since 4.0 */ - public static String formUrlEncode(final NameValuePair[] pairs, final String charset) { + public static String simpleFormUrlEncode( + final NameValuePair[] pairs, + final String charset) { try { - return doFormUrlEncode(pairs, charset); + return formUrlEncode(pairs, charset); } catch (UnsupportedEncodingException e) { try { - return doFormUrlEncode(pairs, DEFAULT_CHARSET); + return formUrlEncode(pairs, DEFAULT_CHARSET); } catch (UnsupportedEncodingException fatal) { // Should never happen. ISO-8859-1 must be supported on all JVMs throw new Error("HttpClient requires " + DEFAULT_CHARSET + " support"); @@ -104,7 +106,7 @@ public class URLUtils { * * @since 2.0 final */ - private static String doFormUrlEncode( + public static String formUrlEncode( final NameValuePair[] pairs, final String charset) throws UnsupportedEncodingException { CharArrayBuffer buf = new CharArrayBuffer(32);