commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mbe...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods RequestEntity.java InputStreamRequestEntity.java ByteArrayRequestEntity.java PostMethod.java EntityEnclosingMethod.java
Date Wed, 28 Apr 2004 02:23:17 GMT
mbecke      2004/04/27 19:23:17

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestMethodCharEncoding.java
                        TestWebappPostMethod.java TestMethodsNoHost.java
               httpclient/src/contrib/org/apache/commons/httpclient/contrib/utils
                        HttpMethodCloner.java
               httpclient/src/java/org/apache/commons/httpclient/methods
                        PostMethod.java EntityEnclosingMethod.java
  Added:       httpclient/src/java/org/apache/commons/httpclient/methods
                        RequestEntity.java InputStreamRequestEntity.java
                        ByteArrayRequestEntity.java
  Log:
  Adds support for streaming entities.
  
  PR: 26070
  Submitted by: Michael Becke
  Reviewed by: Oleg Kalnichevski
  
  Revision  Changes    Path
  1.8       +20 -9     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java
  
  Index: TestMethodCharEncoding.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestMethodCharEncoding.java	22 Feb 2004 18:08:49 -0000	1.7
  +++ TestMethodCharEncoding.java	28 Apr 2004 02:23:16 -0000	1.8
  @@ -30,6 +30,9 @@
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
  +
  +import java.io.ByteArrayInputStream;
  +import java.io.ByteArrayOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import java.util.HashMap;
  @@ -38,6 +41,7 @@
   
   import org.apache.commons.httpclient.methods.GetMethod;
   import org.apache.commons.httpclient.methods.PostMethod;
  +import org.apache.commons.httpclient.methods.RequestEntity;
   import org.apache.commons.httpclient.util.URIUtil;
   
   /**
  @@ -160,10 +164,15 @@
       }
   
   
  -    private void verifyEncoding(final InputStream instream, final int[] sample)
  +    private void verifyEncoding(RequestEntity entity, int[] sample)
        throws IOException  {
  -        assertNotNull("Request body", instream);
           
  +        assertNotNull("Request body", entity);
  +
  +        ByteArrayOutputStream bos = new ByteArrayOutputStream();
  +        entity.writeRequest(bos);
  +        
  +        InputStream instream = new ByteArrayInputStream(bos.toByteArray());
           for (int i = 0; i < sample.length; i++) {
               int b = instream.read();
               assertTrue("Unexpected end of stream", b != -1);
  @@ -174,16 +183,15 @@
           assertTrue("End of stream expected", instream.read() == -1);
       }
       
  -    
       public void testLatinAccentInRequestBody() throws IOException {
   
           PostMethod httppost = new PostMethod("/");
           httppost.setRequestBody(constructString(SWISS_GERMAN_STUFF_UNICODE));
           // Test default encoding ISO-8859-1
  -        verifyEncoding(httppost.getRequestBody(), SWISS_GERMAN_STUFF_ISO8859_1);
  +        verifyEncoding(httppost.getRequestEntity(), SWISS_GERMAN_STUFF_ISO8859_1);
           // Test UTF-8 encoding
           httppost.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_UTF8);
  -        verifyEncoding(httppost.getRequestBody(), SWISS_GERMAN_STUFF_UTF8);
  +        verifyEncoding(httppost.getRequestEntity(), SWISS_GERMAN_STUFF_UTF8);
   
       }
       
  @@ -194,13 +202,13 @@
   
           // Test UTF-8 encoding
           httppost.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_UTF8);
  -        verifyEncoding(httppost.getRequestBody(), RUSSIAN_STUFF_UTF8);
  +        verifyEncoding(httppost.getRequestEntity(), RUSSIAN_STUFF_UTF8);
           // Test KOI8-R
           httppost.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_KOI8_R);
  -        verifyEncoding(httppost.getRequestBody(), RUSSIAN_STUFF_KOI8R);
  +        verifyEncoding(httppost.getRequestEntity(), RUSSIAN_STUFF_KOI8R);
           // Test WIN1251
           httppost.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_WIN1251);
  -        verifyEncoding(httppost.getRequestBody(), RUSSIAN_STUFF_WIN1251);
  +        verifyEncoding(httppost.getRequestEntity(), RUSSIAN_STUFF_WIN1251);
   
       }
   
  @@ -247,9 +255,12 @@
           httppost.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE

               + "; charset=" + CHARSET_UTF8);
   
  +        ByteArrayOutputStream bos = new ByteArrayOutputStream();
  +        httppost.getRequestEntity().writeRequest(bos);
  +        
           Map params = new HashMap();
           StringTokenizer tokenizer = new StringTokenizer(
  -        httppost.getRequestBodyAsString(), "&");
  +            new String(bos.toByteArray(), CHARSET_UTF8), "&");
           while (tokenizer.hasMoreTokens()) {
               String s = tokenizer.nextToken();
               int i = s.indexOf('=');
  
  
  
  1.6       +12 -6     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java
  
  Index: TestWebappPostMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestWebappPostMethod.java	22 Feb 2004 18:08:50 -0000	1.5
  +++ TestWebappPostMethod.java	28 Apr 2004 02:23:16 -0000	1.6
  @@ -203,6 +203,12 @@
           assertFalse("Return value of the method is expected to be false", method.removeParameter("param"));
       }
   
  +    private String getRequestAsString(RequestEntity entity) throws Exception {
  +        ByteArrayOutputStream bos = new ByteArrayOutputStream();
  +        entity.writeRequest(bos);
  +        return new String(bos.toByteArray(), "UTF-8");
  +    }
  +    
       /**
        * Test if setParameter overwrites existing parameter values.
        */
  @@ -212,9 +218,9 @@
           method.addParameter("param", "a");
           method.addParameter("param", "b");
           method.addParameter("param", "c");
  -        assertEquals("param=a&param=b&param=c", method.getRequestBodyAsString());
  +        assertEquals("param=a&param=b&param=c", getRequestAsString(method.getRequestEntity()));
           method.setParameter("param", "a");
  -        assertEquals("param=a", method.getRequestBodyAsString());
  +        assertEquals("param=a", getRequestAsString(method.getRequestEntity()));
       }
   
   }
  
  
  
  1.22      +18 -11    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java
  
  Index: TestMethodsNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- TestMethodsNoHost.java	22 Feb 2004 18:08:49 -0000	1.21
  +++ TestMethodsNoHost.java	28 Apr 2004 02:23:16 -0000	1.22
  @@ -30,7 +30,7 @@
   
   package org.apache.commons.httpclient;
   
  -import java.io.IOException;
  +import java.io.ByteArrayOutputStream;
   import java.io.InputStreamReader;
   import java.io.Reader;
   
  @@ -40,11 +40,12 @@
   import org.apache.commons.httpclient.methods.GetMethod;
   import org.apache.commons.httpclient.methods.HeadMethod;
   import org.apache.commons.httpclient.methods.PostMethod;
  +import org.apache.commons.httpclient.methods.RequestEntity;
   
   /**
    * @author Rodney Waldhoff
    * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  - * @author Ortwin Glck
  + * @author Ortwin Gl?ck
    * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
    * @version $Revision$ $Date$
    */
  @@ -74,18 +75,24 @@
   
       // ----------------------------------------------------------------- Tests
   
  -    public void testPostParametersEncoding() throws IOException {
  +    private String getRequestAsString(RequestEntity entity) throws Exception {
  +        ByteArrayOutputStream bos = new ByteArrayOutputStream();
  +        entity.writeRequest(bos);
  +        return new String(bos.toByteArray(), "UTF-8");
  +    }
  +    
  +    public void testPostParametersEncoding() throws Exception {
           PostMethod post = new PostMethod();
           post.setRequestBody(new NameValuePair[] { PAIR });
  -        assertEquals("name=value", post.getRequestBodyAsString());
  +        assertEquals("name=value", getRequestAsString(post.getRequestEntity()));
   
           post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2 });
           assertEquals("name=value&name1=value1&name2=value2", 
  -            post.getRequestBodyAsString());
  +            getRequestAsString(post.getRequestEntity()));
   
           post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new NameValuePair("hasSpace",
"a b c d") });
           assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d",
  -            post.getRequestBodyAsString());
  +            getRequestAsString(post.getRequestEntity()));
   
       }
   
  @@ -93,7 +100,7 @@
           PostMethod post = new PostMethod("/foo");
           String body = "this+is+the+body";
           post.setRequestBody(body);
  -        assertEquals(body, post.getRequestBodyAsString());
  +        assertEquals(body, getRequestAsString(post.getRequestEntity()));
       }
   
   
  
  
  
  1.5       +1 -1      jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java
  
  Index: HttpMethodCloner.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- HttpMethodCloner.java	22 Feb 2004 18:08:45 -0000	1.4
  +++ HttpMethodCloner.java	28 Apr 2004 02:23:16 -0000	1.5
  @@ -51,7 +51,7 @@
         EntityEnclosingMethod m, EntityEnclosingMethod copy )
           throws java.io.IOException
        {
  -         copy.setRequestBody(m.getRequestBodyAsString());
  +         copy.setRequestEntity(m.getRequestEntity());
        }
    
       private static void copyHttpMethodBase(
  
  
  
  1.54      +13 -23    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java
  
  Index: PostMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
  retrieving revision 1.53
  retrieving revision 1.54
  diff -u -r1.53 -r1.54
  --- PostMethod.java	18 Apr 2004 23:51:37 -0000	1.53
  +++ PostMethod.java	28 Apr 2004 02:23:17 -0000	1.54
  @@ -65,7 +65,7 @@
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
    * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
    * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  - * @author Ortwin Gl�ck
  + * @author Ortwin Gl???ck
    * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
    * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
    *
  @@ -157,29 +157,20 @@
           super.clearRequestBody();
       }
   
  -    /**
  -     * Generates the request body.
  -     * 
  -     * <p>This method must be overwritten by sub-classes that implement
  -     * alternative request content input methods
  -     * </p>   
  -     * 
  -     * @return request body as an array of bytes. If the request content 
  -     *          has not been set, returns <tt>null</tt>.
  -     * 
  -     * @since 2.0beta1
  +    /* (non-Javadoc)
  +     * @see org.apache.commons.httpclient.methods.EntityEnclosingMethod#generateRequestEntity()
        */
  -    protected byte[] generateRequestBody() {
  -        LOG.trace("enter PostMethod.renerateRequestBody()");
  +    protected RequestEntity generateRequestEntity() {
           if (!this.params.isEmpty()) {
               String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());
  -            return EncodingUtil.getAsciiBytes(content);
  +            ByteArrayRequestEntity entity = new ByteArrayRequestEntity();
  +            entity.setContent(EncodingUtil.getAsciiBytes(content));
  +            return entity;
           } else {
  -            return super.generateRequestBody();
  +            return super.generateRequestEntity();
           }
       }
  -
  -
  +    
       /**
        * Sets the value of parameter with parameterName to parameterValue. This method
        * does not preserve the initial insertion order.
  @@ -438,5 +429,4 @@
               }
           }
       }
  -
   }
  
  
  
  1.32      +75 -127   jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java
  
  Index: EntityEnclosingMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- EntityEnclosingMethod.java	18 Apr 2004 23:51:37 -0000	1.31
  +++ EntityEnclosingMethod.java	28 Apr 2004 02:23:17 -0000	1.32
  @@ -29,19 +29,16 @@
   
   package org.apache.commons.httpclient.methods;
   
  -import java.io.ByteArrayInputStream;
  -import java.io.ByteArrayOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.OutputStream;
   
   import org.apache.commons.httpclient.ChunkedOutputStream;
  -import org.apache.commons.httpclient.ContentLengthInputStream;
   import org.apache.commons.httpclient.HttpConnection;
   import org.apache.commons.httpclient.HttpException;
  +import org.apache.commons.httpclient.HttpState;
   import org.apache.commons.httpclient.HttpVersion;
   import org.apache.commons.httpclient.ProtocolException;
  -import org.apache.commons.httpclient.HttpState;
   import org.apache.commons.httpclient.util.EncodingUtil;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -75,19 +72,13 @@
       /** LOG object for this class. */
       private static final Log LOG = LogFactory.getLog(EntityEnclosingMethod.class);
   
  -    /** The buffered request body, if any. */
  -    private byte[] buffer = null;
  -
       /** The unbuffered request body, if any. */
       private InputStream requestStream = null;
   
       /** The request body as string, if any. */
       private String requestString = null;
   
  -    /** for optimization purpose, the generated request body may be
  -     * cached when the method is being executed.
  -     */
  -    private byte[] contentCache = null; 
  +    private RequestEntity requestEntity;
       
       /** Counts how often the request was sent to the server. */
       private int repeatCount = 0;
  @@ -134,7 +125,7 @@
        */
       protected boolean hasRequestContent() {
           LOG.trace("enter EntityEnclosingMethod.hasRequestContent()");
  -        return (this.buffer != null) 
  +        return (this.requestEntity != null) 
               || (this.requestStream != null) 
               || (this.requestString != null);
       }
  @@ -151,8 +142,7 @@
           LOG.trace("enter EntityEnclosingMethod.clearRequestBody()");
           this.requestStream = null;
           this.requestString = null;
  -        this.buffer = null;
  -        this.contentCache = null;
  +        this.requestEntity = null;
       }
   
       /**
  @@ -168,19 +158,33 @@
        */
       protected byte[] generateRequestBody() {
           LOG.trace("enter EntityEnclosingMethod.renerateRequestBody()");
  -        if (this.requestStream != null) {
  -            bufferContent();
  -        }
  -         
  -        if (this.buffer != null) { 
  -            return this.buffer;
  +        return null;
  +    }
  +
  +    protected RequestEntity generateRequestEntity() {
  +        
  +        byte[] requestBody = generateRequestBody();
  +        if (requestBody != null) {
  +            // use the request body, if it exists.
  +            // this is just for backwards compatability
  +            ByteArrayRequestEntity entity = new ByteArrayRequestEntity();
  +            entity.setContent(requestBody);
  +            this.requestEntity = entity;
  +        } else if (this.requestStream != null) {
  +            InputStreamRequestEntity entity = new InputStreamRequestEntity();
  +            entity.setContent(requestStream);
  +            entity.setContentLength(requestContentLength);
  +            this.requestStream = null;
  +            this.requestEntity = entity;
           } else if (this.requestString != null) {
  -            return EncodingUtil.getBytes(this.requestString, getRequestCharSet());
  -        } else {
  -            return null;
  +            ByteArrayRequestEntity entity = new ByteArrayRequestEntity();
  +            entity.setContent(EncodingUtil.getBytes(this.requestString, getRequestCharSet()));
  +            this.requestEntity = entity;
           }
  -    }
   
  +        return this.requestEntity;
  +    }
  +    
       /**
        * Entity enclosing requests cannot be redirected without user intervention
        * according to RFC 2616.
  @@ -208,8 +212,6 @@
       }
   
       /**
  -     * @deprecated use {@link #setRequestContentLength(long)} instead
  -     * 
        * Sets length information about the request body.
        *
        * <p>
  @@ -227,7 +229,9 @@
        *        the user is responsible to supply the correct content length.
        *        If CONTENT_LENGTH_AUTO is specified the request will be buffered
        *        before it is sent over the network.
  -     *
  +     * 
  +     * @deprecated Use {@link #setContentChunked(boolean)} or 
  +     * {@link #setRequestEntity(RequestEntity)}
        */
       public void setRequestContentLength(int length) {
           LOG.trace("enter EntityEnclosingMethod.setRequestContentLength(int)");
  @@ -252,7 +256,9 @@
        *        the user is responsible to supply the correct content length.
        *        If CONTENT_LENGTH_AUTO is specified the request will be buffered
        *        before it is sent over the network.
  -     *
  +     * 
  +     * @deprecated Use {@link #setContentChunked(boolean)} or 
  +     * {@link #setRequestEntity(RequestEntity)}
        */
       public void setRequestContentLength(long length) {
           LOG.trace("enter EntityEnclosingMethod.setRequestContentLength(int)");
  @@ -260,6 +266,15 @@
       }
   
       /**
  +     * Sets whether or not the content should be chunked.
  +     * 
  +     * @param chunked <code>true</code> if the content should be chunked
  +     */
  +    public void setContentChunked(boolean chunked) {
  +        this.requestContentLength = chunked ? CONTENT_LENGTH_CHUNKED : CONTENT_LENGTH_AUTO;
  +    }
  +    
  +    /**
        * Returns the length of the request body.
        *
        * @return number of bytes in the request body
  @@ -270,13 +285,15 @@
           if (!hasRequestContent()) {
               return 0;
           }
  +        // TODO what to do about setting request content and content length
           if (this.requestContentLength != CONTENT_LENGTH_AUTO) {
               return this.requestContentLength;
           }
  -        if (this.contentCache == null) {
  -            this.contentCache = generateRequestBody(); 
  +        
  +        if (this.requestEntity == null) {
  +            this.requestEntity = generateRequestEntity(); 
           }
  -        return (this.contentCache == null) ? 0 : this.contentCache.length;
  +        return (this.requestEntity == null) ? 0 : this.requestEntity.getContentLength();
       }
   
       /**
  @@ -348,6 +365,8 @@
        * Sets the request body to be the specified inputstream.
        *
        * @param body Request body content as {@link java.io.InputStream}
  +     * 
  +     * @deprecated use {@link #setRequestEntity(RequestEntity)}
        */
       public void setRequestBody(InputStream body) {
           LOG.trace("enter EntityEnclosingMethod.setRequestBody(InputStream)");
  @@ -356,22 +375,6 @@
       }
   
       /**
  -     * Returns the request body as a {@link java.io.InputStream}.
  -     * Calling this method will cause the content to be buffered.
  -     *
  -     * @return The request body as a {@link java.io.InputStream} if it has been set.
  -     */
  -    public InputStream getRequestBody() {
  -        LOG.trace("enter EntityEnclosingMethod.getRequestBody()");
  -        byte [] content = generateRequestBody();
  -        if (content != null) {
  -            return new ByteArrayInputStream(content);
  -        } else {
  -            return new ByteArrayInputStream(new byte[] {});
  -        }
  -    }
  -
  -    /**
        * Sets the request body to be the specified string.
        * The string will be submitted, using the encoding
        * specified in the Content-Type request header.<br>
  @@ -390,25 +393,6 @@
       }
   
       /**
  -     * Returns the request body as a {@link java.lang.String}.
  -     * Calling this method will cause the content to be buffered.
  -     *
  -     * @return the request body as a {@link java.lang.String}
  -     * 
  -     * @throws IOException if I/O error occurs while reading the request body
  -     */
  -    public String getRequestBodyAsString() throws IOException {
  -        LOG.trace("enter EntityEnclosingMethod.getRequestBodyAsString()");
  -        byte [] content = generateRequestBody();
  -        if (content != null) {
  -            return EncodingUtil.getString(content, getRequestCharSet());
  -        } else {
  -            return null;
  -        }
  -    }
  -
  -
  -    /**
        * Writes the request body to the given {@link HttpConnection connection}.
        *
        * @param state the {@link HttpState state} information associated with this method
  @@ -441,26 +425,13 @@
                    getHttpVersion().toString());
           }
           
  -        InputStream instream = null;
  -        if (this.requestStream != null) {
  -            LOG.debug("Using unbuffered request body"); 
  -            instream = this.requestStream;
  -        } else { 
  -            if (this.contentCache == null) {
  -                this.contentCache = generateRequestBody();
  -            }
  -            if (this.contentCache != null) { 
  -                LOG.debug("Using buffered request body"); 
  -                instream = new ByteArrayInputStream(this.contentCache);
  -            }
  -        }
  -
  -        if (instream == null) {
  +        this.requestEntity = generateRequestEntity();
  +        if (requestEntity == null) {
               LOG.debug("Request body is empty");
               return true;
           }
   
  -        if ((this.repeatCount > 0) && (this.contentCache == null)) {
  +        if ((this.repeatCount > 0) && !requestEntity.isRepeatable()) {
               // TODO: Is this the right exception to throw here?
               throw new ProtocolException(
                   "Unbuffered entity enclosing request can not be repeated.");
  @@ -469,31 +440,20 @@
           this.repeatCount++;
   
           OutputStream outstream = conn.getRequestOutputStream();
  -
  +        
           if (contentLength == CONTENT_LENGTH_CHUNKED) {
               outstream = new ChunkedOutputStream(outstream);
           }
  -        if (contentLength >= 0) {
  -            // don't need a watcher here - we're reading from something local,
  -            // not server-side.
  -            instream = new ContentLengthInputStream(instream, contentLength);
  -        }
  -
  -        byte[] tmp = new byte[4096];
  -        int total = 0;
  -        int i = 0;
  -        while ((i = instream.read(tmp)) >= 0) {
  -            outstream.write(tmp, 0, i);
  -            total += i;
  -        }
  +        
  +        requestEntity.writeRequest(outstream);
  +        
           // This is hardly the most elegant solution to closing chunked stream
           if (outstream instanceof ChunkedOutputStream) {
               ((ChunkedOutputStream) outstream).finish();
           }
  -        if ((contentLength > 0) && (total < contentLength)) {
  -            throw new IOException("Unexpected end of input stream after "
  -                + total + " bytes (expected " + contentLength + " bytes)");
  -        }
  +        
  +        outstream.flush();
  +        
           LOG.debug("Request body sent");
           return true;
       }
  @@ -515,30 +475,18 @@
       }
   
       /**
  -     * Buffers request body input stream.
  +     * @return Returns the requestEntity.
        */
  -    private void bufferContent() {
  -        LOG.trace("enter EntityEnclosingMethod.bufferContent()");
  +    public RequestEntity getRequestEntity() {
  +        return generateRequestEntity();
  +    }
   
  -        if (this.buffer != null) {
  -            // Already been buffered
  -            return;
  -        }
  -        if (this.requestStream != null) {
  -            try {
  -                ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  -                byte[] data = new byte[4096];
  -                int l = 0;
  -                while ((l = this.requestStream.read(data)) >= 0) {
  -                    tmp.write(data, 0, l);
  -                }
  -                this.buffer = tmp.toByteArray();
  -                this.requestStream = null;
  -            } catch (IOException e) {
  -                LOG.error(e.getMessage(), e);
  -                this.buffer = null;
  -                this.requestStream = null;
  -            }
  -        }
  +    /**
  +     * @param requestEntity The requestEntity to set.
  +     */
  +    public void setRequestEntity(RequestEntity requestEntity) {
  +        clearRequestBody();
  +        this.requestEntity = requestEntity;
       }
  +
   }
  
  
  
  1.1                  jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/RequestEntity.java
  
  Index: RequestEntity.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/RequestEntity.java,v
1.1 2004/04/28 02:23:17 mbecke Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/28 02:23:17 $
   *
   * ====================================================================
   *
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed 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.commons.httpclient.methods;
  
  import java.io.IOException;
  import java.io.OutputStream;
  
  /**
   */
  public interface RequestEntity {
  
      /**
       * Tests if {@link #writeRequest(OutputStream)} can be called more than once.
       * @return
       */
      boolean isRepeatable();
  
      /**
       * Writes the request entity to the given stream.
       * @param out
       * @throws IOException
       */
      void writeRequest(OutputStream out) throws IOException;
      
      /**
       * Gets the request entity's length.
       * @return either a number >= 0 or 
       * {@link org.apache.commons.httpclient.methods.EntityEnclosingMethod#CONTENT_LENGTH_CHUNKED}
       */
      long getContentLength();
      
  }
  
  
  
  1.1                  jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/InputStreamRequestEntity.java
  
  Index: InputStreamRequestEntity.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/InputStreamRequestEntity.java,v
1.1 2004/04/28 02:23:17 mbecke Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/28 02:23:17 $
   *
   * ====================================================================
   *
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed 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/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  package org.apache.commons.httpclient.methods;
  
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * A RequestEntity that contains an InputStream.
   */
  public class InputStreamRequestEntity implements RequestEntity {
  
      private static final Log LOG = LogFactory.getLog(InputStreamRequestEntity.class);
      
      private long contentLength = EntityEnclosingMethod.CONTENT_LENGTH_AUTO;
      
      private InputStream content = null;
  
      /** The buffered request body, if any. */
      private byte[] buffer = null;
  
      /**
       * Creates a new InputStreamRequestEntity with no content.
       */
      public InputStreamRequestEntity() {
      }
  
      /**
       * Buffers request body input stream.
       */
      private void bufferContent() {
  
          if (this.buffer != null) {
              // Already been buffered
              return;
          }
          if (this.content != null) {
              try {
                  ByteArrayOutputStream tmp = new ByteArrayOutputStream();
                  byte[] data = new byte[4096];
                  int l = 0;
                  while ((l = this.content.read(data)) >= 0) {
                      tmp.write(data, 0, l);
                  }
                  this.buffer = tmp.toByteArray();
                  this.content = null;
                  this.contentLength = buffer.length;
              } catch (IOException e) {
                  LOG.error(e.getMessage(), e);
                  this.buffer = null;
                  this.content = null;
                  this.contentLength = 0;
              }
          }
      }
      
      /**
       * Tests if this method is repeatable.  Only <code>true</code> if the content
has been
       * buffered.
       * 
       * @see #getContentLength()
       * @see #setContentLength(long)
       */
      public boolean isRepeatable() {
          return content != null;
      }
  
      /* (non-Javadoc)
       * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
       */
      public void writeRequest(OutputStream out) throws IOException {
          
          if (content != null) {
              byte[] tmp = new byte[4096];
              int total = 0;
              int i = 0;
              while ((i = content.read(tmp)) >= 0) {
                  out.write(tmp, 0, i);
                  total += i;
              }        
          } else if (buffer != null) {
              out.write(buffer);
          } else {
              throw new IllegalStateException("Content must be set before entity is written");
          }
      }
  
      /**
       * Gets the content length.  If the content length has not been set, the content will
be
       * buffered to determine the actual content length.
       * 
       * @see #setContentLength(long)
       */
      public long getContentLength() {
          if (contentLength == EntityEnclosingMethod.CONTENT_LENGTH_AUTO && buffer
== null) {
              bufferContent();
          }
          return contentLength;
      }
  
      /**
       * Sets the content length.
       * 
       * @param contentLength The content size in bytes or any of 
       * {@link EntityEnclosingMethod#CONTENT_LENGTH_AUTO CONTENT_LENGTH_AUTO},
       * {@link EntityEnclosingMethod#CONTENT_LENGTH_CHUNKED CONTENT_LENGTH_CHUNKED}. If the
number 
       * of bytes or <code>CONTENT_LENGTH_CHUNKED</code> is specified the content
will not be 
       * buffered when {@link #getContentLength()} is called.
       */
      public void setContentLength(long contentLength) {
          this.contentLength = contentLength;
      }
  
      /**
       * @return Returns the content.
       */
      public InputStream getContent() {
          return content;
      }
  
      /**
       * @param inputStream The content to set.
       */
      public void setContent(InputStream inputStream) {
          this.content = inputStream;
      }
  
  }
  
  
  
  1.1                  jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java
  
  Index: ByteArrayRequestEntity.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java,v
1.1 2004/04/28 02:23:17 mbecke Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/28 02:23:17 $
   *
   * ====================================================================
   *
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed 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/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  package org.apache.commons.httpclient.methods;
  
  import java.io.IOException;
  import java.io.OutputStream;
  
  /**
   * A RequestEntity that contains an array of bytes.
   */
  public class ByteArrayRequestEntity implements RequestEntity {
  
      /** The content */
      private byte[] content;
      
      /**
       * Creates a new entity with no content.  The content must be set before this entity
can be 
       * used. 
       */
      public ByteArrayRequestEntity() {
          super();
      }
  
      /**
       * @return <code>true</code>
       */
      public boolean isRepeatable() {
          return true;
      }
  
      /* (non-Javadoc)
       * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
       */
      public void writeRequest(OutputStream out) throws IOException {
          if (content == null) {
              throw new IllegalStateException("Content must be set before entity is written");
          }
          out.write(content);
      }
  
      /**
       * @return The length of the content.
       */
      public long getContentLength() {
          return content.length;
      }
  
      /**
       * @return Returns the content.
       */
      public byte[] getContent() {
          return content;
      }
  
      /**
       * @param content The content to set.
       */
      public void setContent(byte[] content) {
          if (content == null) {
              throw new IllegalArgumentException("The content cannot be null");
          }
          this.content = content;
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message