hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r932551 - in /httpcomponents/httpcore/trunk: httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/ httpcore/src/examples/org/apache/http/examples/ httpcore/src/main/java/org/apache/http/ httpcore/src/main/java/org/apache/http...
Date Fri, 09 Apr 2010 19:05:44 GMT
Author: olegk
Date: Fri Apr  9 19:05:43 2010
New Revision: 932551

URL: http://svn.apache.org/viewvc?rev=932551&view=rev
Log:
Deprecated HttpEntity#consumeContent in favor of a more standard InputStream#close contract

Modified:
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java
    httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java
    httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpEntity.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/AbstractHttpEntity.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/BasicHttpEntity.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/EntityTemplate.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/HttpEntityWrapper.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestAbstractHttpEntity.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestBasicHttpEntity.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestHttpEntityWrapper.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
    httpcomponents/httpcore/trunk/src/docbkx/fundamentals.xml

Modified: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
Fri Apr  9 19:05:43 2010
@@ -50,6 +50,7 @@ import org.apache.http.nio.protocol.NHtt
 import org.apache.http.nio.protocol.NHttpResponseTrigger;
 import org.apache.http.nio.util.HeapByteBufferAllocator;
 import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.EntityUtils;
 
 class NRandomDataHandler implements NHttpRequestHandler  {
     
@@ -75,7 +76,7 @@ class NRandomDataHandler implements NHtt
         }
         if (request instanceof HttpEntityEnclosingRequest) {
             HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
-            entity.consumeContent();
+            EntityUtils.consume(entity);
         }
         String target = request.getRequestLine().getUri();
 
@@ -114,7 +115,7 @@ class NRandomDataHandler implements NHtt
         }
         if (request instanceof HttpEntityEnclosingRequest) {
             HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
-            entity.consumeContent();
+            EntityUtils.consume(entity);
         }
         String target = request.getRequestLine().getUri();
 

Modified: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java
Fri Apr  9 19:05:43 2010
@@ -42,6 +42,7 @@ import org.apache.http.entity.AbstractHt
 import org.apache.http.entity.StringEntity;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.util.EntityUtils;
 
 class RandomDataHandler implements HttpRequestHandler  {
     
@@ -59,7 +60,7 @@ class RandomDataHandler implements HttpR
         }
         if (request instanceof HttpEntityEnclosingRequest) {
             HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
-            entity.consumeContent();
+            EntityUtils.consume(entity);
         }
         String target = request.getRequestLine().getUri();
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java
Fri Apr  9 19:05:43 2010
@@ -199,9 +199,9 @@ public class ElementalHttpServer {
             this.httpService = new HttpService(
                     httpproc, 
                     new DefaultConnectionReuseStrategy(), 
-                    new DefaultHttpResponseFactory());
-            this.httpService.setParams(this.params);
-            this.httpService.setHandlerResolver(reqistry);
+                    new DefaultHttpResponseFactory(),
+                    reqistry,
+                    this.params);
         }
         
         public void run() {

Modified: httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
Fri Apr  9 19:05:43 2010
@@ -218,9 +218,9 @@ public class ElementalReverseProxy {
             this.httpService = new HttpService(
                     inhttpproc, 
                     new DefaultConnectionReuseStrategy(), 
-                    new DefaultHttpResponseFactory());
-            this.httpService.setParams(this.params);
-            this.httpService.setHandlerResolver(reqistry);
+                    new DefaultHttpResponseFactory(),
+                    reqistry,
+                    this.params);
         }
         
         public void run() {

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpEntity.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpEntity.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpEntity.java Fri
Apr  9 19:05:43 2010
@@ -121,11 +121,14 @@ public interface HttpEntity {
     Header getContentEncoding();
 
     /**
-     * Creates a new InputStream object of the entity.
-     * It is a programming error
-     * to return the same InputStream object more than once.
-     * Entities that are not {@link #isRepeatable repeatable}
-     * will throw an exception if this method is called multiple times.
+     * Creates a new InputStream object of the entity. It is an interface 
+     * violation to return the same {@link InputStream} object more than 
+     * once. Entities that are not {@link #isRepeatable repeatable}
+     * should throw an exception if this method is called multiple times.
+     * <p>
+     * IMPORTANT: Please note all entity implementations must ensure that 
+     * all allocated resources are properly deallocated after 
+     * the {@link InputStream#close()} method is invoked.  
      *
      * @return a new input stream that returns the entity data.
      *
@@ -137,7 +140,12 @@ public interface HttpEntity {
     InputStream getContent() throws IOException, IllegalStateException;
 
     /**
-     * Writes the entity content to the output stream.  
+     * Writes the entity content out to the output stream. 
+     * <p>
+     * <p>
+     * IMPORTANT: Please note all entity implementations must ensure that 
+     * all allocated resources are properly deallocated when this method 
+     * returns.  
      * 
      * @param outstream the output stream to write entity content to
      * 
@@ -147,27 +155,17 @@ public interface HttpEntity {
 
     /**
      * Tells whether this entity depends on an underlying stream.
-     * Streamed entities should return <code>true</code> until the
-     * content has been consumed, <code>false</code> afterwards.
-     * Self-contained entities should return <code>false</code>.
-     * Wrapping entities should delegate this call to the wrapped entity.
-     * <br/>
-     * The content of a streamed entity is consumed when the stream
-     * returned by {@link #getContent getContent} has been read to EOF,
-     * or after {@link #consumeContent consumeContent} has been called.
-     * If a streamed entity can not detect whether the stream has been
-     * read to EOF, it should return <code>true</code> until
-     * {@link #consumeContent consumeContent} is called.
+     * Streamed entities that read data directly from the socket should 
+     * return <code>true</code>. Self-contained entities should return 
+     * <code>false</code>. Wrapping entities should delegate this call 
+     * to the wrapped entity.
      *
-     * @return  <code>true</code> if the entity content is streamed and
-     *          not yet consumed, <code>false</code> otherwise
+     * @return  <code>true</code> if the entity content is streamed, 
+     *          <code>false</code> otherwise
      */
     boolean isStreaming(); // don't expect an exception here
 
     /**
-     * TODO: The name of this method is misnomer. It will be renamed to
-     * #finish() in the next major release.
-     * <br/>
      * This method is called to indicate that the content of this entity
      * is no longer required. All entity implementations are expected to
      * release all allocated resources as a result of this method 
@@ -182,7 +180,9 @@ public interface HttpEntity {
      *
      * @throws IOException if an I/O error occurs.
      *          This indicates that connection keep-alive is not possible.
+     * 
+     * @deprecated see {@link #getContent()} and {@link #writeTo(OutputStream)}
      */
     void consumeContent() throws IOException;
 
-} // interface HttpEntity
+}

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/AbstractHttpEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/AbstractHttpEntity.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/AbstractHttpEntity.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/AbstractHttpEntity.java
Fri Apr  9 19:05:43 2010
@@ -28,6 +28,7 @@
 package org.apache.http.entity;
 
 import java.io.IOException;
+import java.io.OutputStream;
 
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
@@ -164,24 +165,10 @@ public abstract class AbstractHttpEntity
 
     /**
      * Does not consume anything.
-     * The default implementation does nothing if
-     * {@link HttpEntity#isStreaming isStreaming}
-     * returns <code>false</code>, and throws an exception
-     * if it returns <code>true</code>.
-     * This removes the burden of implementing
-     * an empty method for non-streaming entities.
-     *
-     * @throws IOException      in case of an I/O problem
-     * @throws UnsupportedOperationException
-     *          if a streaming subclass does not override this method
-     */
-    public void consumeContent()
-        throws IOException, UnsupportedOperationException{
-        if (isStreaming()) {
-            throw new UnsupportedOperationException
-                ("streaming entity does not implement consumeContent()");
-        }
-    } // consumeContent
-
+     * 
+     * @deprecated see {@link #getContent()} and {@link #writeTo(OutputStream)}
+     */
+    public void consumeContent() throws IOException, UnsupportedOperationException{
+    }
     
-} // class AbstractHttpEntity
+}

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/BasicHttpEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/BasicHttpEntity.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/BasicHttpEntity.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/BasicHttpEntity.java
Fri Apr  9 19:05:43 2010
@@ -40,7 +40,6 @@ import java.io.OutputStream;
 public class BasicHttpEntity extends AbstractHttpEntity {
 
     private InputStream content;
-    private boolean contentObtained;
     private long length;
 
     /**
@@ -53,7 +52,6 @@ public class BasicHttpEntity extends Abs
         this.length = -1;
     }
 
-    // non-javadoc, see interface HttpEntity
     public long getContentLength() {
         return this.length;
     }
@@ -65,21 +63,15 @@ public class BasicHttpEntity extends Abs
      *          since {@link #setContent setContent} has been called
      *
      * @throws IllegalStateException
-     *          if the content has been obtained before, or
-     *          has not yet been provided
+     *          if the content has not been provided
      */
-    public InputStream getContent()
-        throws IllegalStateException {
+    public InputStream getContent() throws IllegalStateException {
         if (this.content == null) {
             throw new IllegalStateException("Content has not been provided");
         }
-        if (this.contentObtained) {
-            throw new IllegalStateException("Content has been consumed");
-        }
-        this.contentObtained = true;
         return this.content;
 
-    } // getContent
+    }
 
     /**
      * Tells that this entity is not repeatable.
@@ -108,32 +100,35 @@ public class BasicHttpEntity extends Abs
      */
     public void setContent(final InputStream instream) {
         this.content = instream;
-        this.contentObtained = false; 
     }
 
-    // non-javadoc, see interface HttpEntity
     public void writeTo(final OutputStream outstream) throws IOException {
         if (outstream == null) {
             throw new IllegalArgumentException("Output stream may not be null");
         }
         InputStream instream = getContent();
-        int l;
-        byte[] tmp = new byte[2048];
-        while ((l = instream.read(tmp)) != -1) {
-            outstream.write(tmp, 0, l);
+        try {
+            int l;
+            byte[] tmp = new byte[2048];
+            while ((l = instream.read(tmp)) != -1) {
+                outstream.write(tmp, 0, l);
+            }
+        } finally {
+            instream.close();
         }
     }
 
-    // non-javadoc, see interface HttpEntity
     public boolean isStreaming() {
-        return !this.contentObtained && this.content != null;
+        return this.content != null;
     }
 
-    // non-javadoc, see interface HttpEntity
+    /**
+     * @deprecated see {@link #getContent()} and {@link #writeTo(OutputStream)}
+     */
     public void consumeContent() throws IOException {
         if (content != null) {
             content.close(); // reads to the end of the entity
         }
     }
     
-} // class BasicHttpEntity
+}

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/EntityTemplate.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/EntityTemplate.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/EntityTemplate.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/EntityTemplate.java
Fri Apr  9 19:05:43 2010
@@ -69,9 +69,12 @@ public class EntityTemplate extends Abst
     }
 
     public boolean isStreaming() {
-        return true;
+        return false;
     }
 
+    /**
+     * @deprecated
+     */
     public void consumeContent() throws IOException {
     }
     

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/HttpEntityWrapper.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/HttpEntityWrapper.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/HttpEntityWrapper.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/HttpEntityWrapper.java
Fri Apr  9 19:05:43 2010
@@ -100,9 +100,12 @@ public class HttpEntityWrapper implement
         return wrappedEntity.isStreaming();
     }
 
+    /**
+     * @deprecated
+     */
     public void consumeContent()
         throws IOException {
         wrappedEntity.consumeContent();
     }
 
-} // class HttpEntityWrapper
+}

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
Fri Apr  9 19:05:43 2010
@@ -47,6 +47,7 @@ import org.apache.http.entity.ByteArrayE
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.DefaultedHttpParams;
 import org.apache.http.util.EncodingUtils;
+import org.apache.http.util.EntityUtils;
 
 /**
  * HttpService is a server side HTTP protocol handler based in the blocking 
@@ -292,9 +293,7 @@ public class HttpService {
             // Make sure the request content is fully consumed
             if (request instanceof HttpEntityEnclosingRequest) {
                 HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
-                if (entity != null) {
-                    entity.consumeContent();
-                }
+                EntityUtils.consume(entity);
             }
             
         } catch (HttpException ex) {

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
Fri Apr  9 19:05:43 2010
@@ -49,6 +49,27 @@ public final class EntityUtils {
     }
 
     /**
+     * Ensures that the entity content is fully consumed and the content stream, if exists,
+     * is closed.
+     * 
+     * @param entity
+     * @throws IOException if an error occurs reading the input stream
+     * 
+     * @since 4.1
+     */
+    public static void consume(final HttpEntity entity) throws IOException {
+        if (entity == null) {
+            return;
+        }
+        if (entity.isStreaming()) {
+            InputStream instream = entity.getContent();
+            if (instream != null) {
+                instream.close();
+            }
+        }
+    }
+    
+    /**
      * Read the contents of an entity and return it as a byte array.
      * 
      * @param entity

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestAbstractHttpEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestAbstractHttpEntity.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestAbstractHttpEntity.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestAbstractHttpEntity.java
Fri Apr  9 19:05:43 2010
@@ -94,16 +94,4 @@ public class TestAbstractHttpEntity exte
         assertTrue(httpentity.isChunked());
     }
     
-    public void testConsumeContent() throws Exception {
-        HttpEntityMockup httpentity = new HttpEntityMockup();
-        httpentity.setStreaming(true);
-        try {
-            httpentity.consumeContent();
-        } catch (UnsupportedOperationException ex) {
-            // expected
-        }
-        httpentity.setStreaming(false);
-        httpentity.consumeContent();
-    }
-    
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestBasicHttpEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestBasicHttpEntity.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestBasicHttpEntity.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestBasicHttpEntity.java
Fri Apr  9 19:05:43 2010
@@ -79,6 +79,8 @@ public class TestBasicHttpEntity extends
         } catch (IllegalStateException ex) {
             // expected
         }
+        httpentity.setContent(content);
+        assertEquals(content, httpentity.getContent());
 
         httpentity.setContent(null);
         try {
@@ -87,16 +89,6 @@ public class TestBasicHttpEntity extends
         } catch (IllegalStateException ex) {
             // expected
         }
-
-        httpentity.setContent(content);
-        httpentity.getContent();
-        try {
-            httpentity.getContent();
-            fail("IllegalStateException should have been thrown");
-        } catch (IllegalStateException ex) {
-            // expected
-        }
-
     }
     
     public void testWriteTo() throws Exception {
@@ -113,14 +105,6 @@ public class TestBasicHttpEntity extends
         for (int i = 0; i < bytes.length; i++) {
             assertEquals(bytes[i], bytes2[i]);
         }
-        out = new ByteArrayOutputStream();
-        try {
-            httpentity.writeTo(out);
-            fail("IllegalStateException should have been thrown");
-        } catch (IllegalStateException ex) {
-            // expected
-        }
-
         httpentity.setContent(null);
         out = new ByteArrayOutputStream();
         try {
@@ -138,14 +122,4 @@ public class TestBasicHttpEntity extends
         }
     }
 
-    public void testConsumeContent() throws Exception {
-        byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
-        InputStream content = new ByteArrayInputStream(bytes);
-        BasicHttpEntity httpentity = new BasicHttpEntity();
-        httpentity.setContent(null);
-        httpentity.consumeContent();
-        httpentity.setContent(content);
-        httpentity.consumeContent();
-    }
-    
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java
Fri Apr  9 19:05:43 2010
@@ -68,7 +68,7 @@ public class TestEntityTemplate extends 
         
         assertEquals(-1, httpentity.getContentLength());
         assertTrue(httpentity.isRepeatable());
-        assertTrue(httpentity.isStreaming());
+        assertFalse(httpentity.isStreaming());
     }
 
     public void testIllegalConstructor() throws Exception {
@@ -103,7 +103,7 @@ public class TestEntityTemplate extends 
         }
     }
     
-    public void testConsume() throws Exception {
+    public void testgetContent() throws Exception {
         HttpEntity httpentity = new EntityTemplate(new ContentProducer() {
 
             public void writeTo(final OutputStream outstream) throws IOException {
@@ -111,14 +111,12 @@ public class TestEntityTemplate extends 
             }
           
         });
-        httpentity.consumeContent(); // Has no effect
         try {
             httpentity.getContent();
             fail("UnsupportedOperationException should have been thrown");
         } catch (UnsupportedOperationException ex) {
             // expected
         }
-        
     }
         
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestHttpEntityWrapper.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestHttpEntityWrapper.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestHttpEntityWrapper.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestHttpEntityWrapper.java
Fri Apr  9 19:05:43 2010
@@ -34,6 +34,7 @@ import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
 import org.apache.http.protocol.HTTP;
+import org.apache.http.util.EntityUtils;
 
 /**
  * Unit tests for {@link HttpEntityWrapper}.
@@ -115,8 +116,8 @@ public class TestHttpEntityWrapper exten
         String s = "Message content";
         StringEntity httpentity = new StringEntity(s);
         HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
-        wrapped.consumeContent();
-        wrapped.consumeContent();
+        EntityUtils.consume(wrapped);
+        EntityUtils.consume(wrapped);
     }
     
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
Fri Apr  9 19:05:43 2010
@@ -578,7 +578,7 @@ public class TestHttpServiceAndExecutor 
 
                 HttpEntity entity = response.getEntity();
                 assertNotNull(entity);
-                entity.consumeContent();
+                EntityUtils.consume(entity);
                 
                 if (r < 2) {
                     assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getStatusLine().getStatusCode());

Modified: httpcomponents/httpcore/trunk/src/docbkx/fundamentals.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/src/docbkx/fundamentals.xml?rev=932551&r1=932550&r2=932551&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/src/docbkx/fundamentals.xml (original)
+++ httpcomponents/httpcore/trunk/src/docbkx/fundamentals.xml Fri Apr  9 19:05:43 2010
@@ -304,24 +304,36 @@ important message
 ]]></programlisting>
             </section>
             <section>
-                <title>Ensuring release of low level resources</title>
+                <title>Ensuring release of system resources</title>
                 <para>
-                When finished with an entity that relies on an underlying input stream, it's

-                important to execute the <methodname>HttpEntity#consumeContent()</methodname>

-                method, so as to consume any available content on the stream, so the connection
-                could be released to any connection pools. If the incoming content is not
consumed
-                fully, other requests may fail when this connection is re-used. 
+                In order to ensure proper release of system resources one must close the
content
+                stream associated with the entity. 
                 </para>
+                <programlisting><![CDATA[
+HttpResponse response;
+HttpEntity entity = response.getEntity();
+if (entity != null) {
+    InputStream instream = entity.getContent();
+    try {
+        // do something useful
+    } finally {
+        instream.close();
+    }
+}
+]]></programlisting>
                 <para>
-                Alternatively one can simply check the result of <methodname>
-                HttpEntity#isStreaming()</methodname>, and keep reading from the input
stream 
-                until it returns false. 
+                Please note that <methodname>HttpEntity#writeTo(OutputStream)</methodname>

+                method is also required to ensure proper release of system resources once
the 
+                entity has been fully written out. If this method obtains an instance of

+                <classname>java.io.InputStream</classname> by calling 
+                <methodname>HttpEntity#getContent()</methodname>, it is also
expected to close
+                the stream in a finally clause.
                 </para>
                 <para>
-                Self contained entities will always return false with <methodname>
-                HttpEntity#isStreaming()</methodname>, as there is no underlying stream
it depends 
-                on. For these entities <methodname>HttpEntity#consumeContent()</methodname>
will 
-                do nothing, and does not need to be called.
+                When working with streaming entities, one can use the
+                <methodname>EntityUtils#consume(HttpEntity)</methodname> method
to ensure that
+                the entity content has been fully consumed and the underlying stream has
been
+                closed.
                 </para>
             </section>
         </section>
@@ -591,8 +603,8 @@ metrics.getSentBytesCount();
             <para>
             HTTP connection interfaces, both client and server, send and receive messages
in two 
             stages. The message head is transmitted first. Depending on properties of the
message 
-            head it may be followed by a message body. Please note it is very important to
call 
-            <methodname>HttpEntity#consumeContent()</methodname> to signal that
the processing of 
+            head it may be followed by a message body. Please note it is very important to
always 
+            close the underlying content stream in order to signal that the processing of

             the message is complete. HTTP entities that stream out their content directly
from the 
             input stream of the underlying connection must ensure the content of the message
body 
             is fully consumed for that connection to be potentially re-usable.
@@ -612,9 +624,10 @@ HttpResponse response = conn.receiveResp
 conn.receiveResponseEntity(response);
 HttpEntity entity = response.getEntity();
 if (entity != null) {
-    // Do something useful with the entity and, when done, call 
-    // consumeContent() to make sure the connection can be re-used 
-    entity.consumeContent();
+    // Do something useful with the entity and, when done, ensure all 
+    // content has been consumed, so that the underlying connection 
+    // coult be re-used 
+    EntityUtils.consume(entity);
 }
 ]]></programlisting>
             <para>
@@ -632,10 +645,10 @@ if (request instanceof HttpEntityEnclosi
     HttpEntity entity = ((HttpEntityEnclosingRequest) request)
         .getEntity();
     if (entity != null) {
-        // Do something useful with the entity and, when done, 
-        // call consumeContent() to make sure the connection 
-        // can be re-used 
-        entity.consumeContent();
+        // Do something useful with the entity and, when done, ensure all 
+        // content has been consumed, so that the underlying connection 
+        // coult be re-used 
+        EntityUtils.consume(entity);
     }
 }
 HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
@@ -1214,9 +1227,7 @@ response.setParams(params);
 httpexecutor.postProcess(response, httpproc, context);
 
 HttpEntity entity = response.getEntity();
-if (entity != null) {
-    entity.consumeContent();
-}
+EntityUtils.consume(entity);
 ]]></programlisting>
             <para>
             Methods of <classname>HttpRequestExecutor</classname> are safe to
execute from multiple 



Mime
View raw message