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 2007/04/13 17:21:04 UTC

svn commit: r528521 - in /jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client: methods/UrlEncodedFormEntity.java utils/URLUtils.java

Author: olegk
Date: Fri Apr 13 08:21:04 2007
New Revision: 528521

URL: http://svn.apache.org/viewvc?view=rev&rev=528521
Log:
Added URL encoded form entity

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java   (with props)
Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java

Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java?view=auto&rev=528521
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java Fri Apr 13 08:21:04 2007
@@ -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
+ * <http://www.apache.org/>.
+ *
+ */
+
+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();
+    }
+
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/UrlEncodedFormEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java?view=diff&rev=528521&r1=528520&r2=528521
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java Fri Apr 13 08:21:04 2007
@@ -71,12 +71,14 @@
      * 
      * @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 @@
      * 
      * @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);