Index: src/contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java =================================================================== retrieving revision 1.4 diff -u -r1.4 HttpMethodCloner.java --- src/contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java 22 Feb 2004 18:08:45 -0000 1.4 +++ src/contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java 26 Apr 2004 02:31:33 -0000 @@ -51,7 +51,7 @@ EntityEnclosingMethod m, EntityEnclosingMethod copy ) throws java.io.IOException { - copy.setRequestBody(m.getRequestBodyAsString()); + copy.setRequestEntity(m.getRequestEntity()); } private static void copyHttpMethodBase( Index: src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java =================================================================== retrieving revision 1.31 diff -u -r1.31 EntityEnclosingMethod.java --- src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java 18 Apr 2004 23:51:37 -0000 1.31 +++ src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java 26 Apr 2004 02:31:34 -0000 @@ -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. * *

@@ -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 true 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.
@@ -382,6 +385,8 @@ * content encoding is used (ISO-8859-1). * * @param body Request body content as a string + * + * @deprecated use {@link #setRequestEntity(RequestEntity)} */ public void setRequestBody(String body) { LOG.trace("enter EntityEnclosingMethod.setRequestBody(String)"); @@ -390,25 +395,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 +427,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 +442,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 +477,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; } + } Index: src/java/org/apache/commons/httpclient/methods/PostMethod.java =================================================================== retrieving revision 1.53 diff -u -r1.53 PostMethod.java --- src/java/org/apache/commons/httpclient/methods/PostMethod.java 18 Apr 2004 23:51:37 -0000 1.53 +++ src/java/org/apache/commons/httpclient/methods/PostMethod.java 26 Apr 2004 02:31:35 -0000 @@ -157,29 +157,20 @@ super.clearRequestBody(); } - /** - * Generates the request body. - * - *

This method must be overwritten by sub-classes that implement - * alternative request content input methods - *

- * - * @return request body as an array of bytes. If the request content - * has not been set, returns null. - * - * @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 @@ } } } - } Index: src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java =================================================================== retrieving revision 1.7 diff -u -r1.7 TestMethodCharEncoding.java --- src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java 22 Feb 2004 18:08:49 -0000 1.7 +++ src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java 26 Apr 2004 02:31:36 -0000 @@ -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('='); Index: src/test/org/apache/commons/httpclient/TestMethodsNoHost.java =================================================================== retrieving revision 1.21 diff -u -r1.21 TestMethodsNoHost.java --- src/test/org/apache/commons/httpclient/TestMethodsNoHost.java 22 Feb 2004 18:08:49 -0000 1.21 +++ src/test/org/apache/commons/httpclient/TestMethodsNoHost.java 26 Apr 2004 02:31:36 -0000 @@ -30,6 +30,7 @@ package org.apache.commons.httpclient; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; @@ -40,11 +41,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 Jeff Dever - * @author Ortwin Gl?ck + * @author Ortwin Gl?ck * @author Oleg Kalnichevski * @version $Revision: 1.21 $ $Date: 2004/02/22 18:08:49 $ */ @@ -74,18 +76,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 +101,7 @@ PostMethod post = new PostMethod("/foo"); String body = "this+is+the+body"; post.setRequestBody(body); - assertEquals(body, post.getRequestBodyAsString()); + assertEquals(body, getRequestAsString(post.getRequestEntity())); } Index: src/test/org/apache/commons/httpclient/TestWebappPostMethod.java =================================================================== retrieving revision 1.5 diff -u -r1.5 TestWebappPostMethod.java --- src/test/org/apache/commons/httpclient/TestWebappPostMethod.java 22 Feb 2004 18:08:50 -0000 1.5 +++ src/test/org/apache/commons/httpclient/TestWebappPostMethod.java 26 Apr 2004 02:31:37 -0000 @@ -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¶m=b¶m=c", method.getRequestBodyAsString()); + assertEquals("param=a¶m=b¶m=c", getRequestAsString(method.getRequestEntity())); method.setParameter("param", "a"); - assertEquals("param=a", method.getRequestBodyAsString()); + assertEquals("param=a", getRequestAsString(method.getRequestEntity())); } } Index: src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java =================================================================== RCS file: src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java diff -N src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,93 @@ +/* + * $Header: $ + * $Revision: $ + * $Date: $ + * + * ==================================================================== + * + * 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 + * . + * + * [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 true + */ + 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; + } + +} Index: src/java/org/apache/commons/httpclient/methods/InputStreamRequestEntity.java =================================================================== RCS file: src/java/org/apache/commons/httpclient/methods/InputStreamRequestEntity.java diff -N src/java/org/apache/commons/httpclient/methods/InputStreamRequestEntity.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/java/org/apache/commons/httpclient/methods/InputStreamRequestEntity.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,161 @@ +/* + * $Header: $ + * $Revision: $ + * $Date: $ + * + * ==================================================================== + * + * 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 + * . + * + * [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 true 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 CONTENT_LENGTH_CHUNKED 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; + } + +} Index: src/java/org/apache/commons/httpclient/methods/RequestEntity.java =================================================================== RCS file: src/java/org/apache/commons/httpclient/methods/RequestEntity.java diff -N src/java/org/apache/commons/httpclient/methods/RequestEntity.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/java/org/apache/commons/httpclient/methods/RequestEntity.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,59 @@ +/* + * $Header: $ + * $Revision: $ + * $Date: $ + * + * ==================================================================== + * + * 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 + * . + * + */ + +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(); + +}