You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/06/10 21:27:08 UTC

svn commit: r1348648 - in /httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http: conn/ impl/client/exec/

Author: olegk
Date: Sun Jun 10 19:27:07 2012
New Revision: 1348648

URL: http://svn.apache.org/viewvc?rev=1348648&view=rev
Log:
Fixed bug in RetryFacade; deprecated BasicManagedEntity

Added:
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ManagedEntity.java
      - copied, changed from r1348580, httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java
Modified:
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicEofSensorWatcher.java
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/RetryFacade.java

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicEofSensorWatcher.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicEofSensorWatcher.java?rev=1348648&r1=1348647&r2=1348648&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicEofSensorWatcher.java (original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicEofSensorWatcher.java Sun Jun 10 19:27:07 2012
@@ -37,7 +37,10 @@ import org.apache.http.annotation.NotThr
  * is released on close or EOF.
  *
  * @since 4.0
+ * 
+ * @deprecated (4.3) do not use.
  */
+@Deprecated
 @NotThreadSafe
 public class BasicEofSensorWatcher implements EofSensorWatcher {
 

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java?rev=1348648&r1=1348647&r2=1348648&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java (original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java Sun Jun 10 19:27:07 2012
@@ -45,7 +45,10 @@ import org.apache.http.util.EntityUtils;
  * the unmanaged entity in the response with a managed one.
  *
  * @since 4.0
+ * 
+ * @deprecated (4.3) do not use.
  */
+@Deprecated
 @NotThreadSafe
 public class BasicManagedEntity extends HttpEntityWrapper
     implements ConnectionReleaseTrigger, EofSensorWatcher {

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java?rev=1348648&r1=1348647&r2=1348648&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java (original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java Sun Jun 10 19:27:07 2012
@@ -51,7 +51,6 @@ import org.apache.http.client.UserTokenH
 import org.apache.http.client.methods.HttpExecutionAware;
 import org.apache.http.client.params.HttpClientParams;
 import org.apache.http.client.protocol.ClientContext;
-import org.apache.http.conn.BasicManagedEntity;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.ClientConnectionRequest;
 import org.apache.http.conn.ConnectionKeepAliveStrategy;
@@ -360,7 +359,7 @@ public class MainRequestExecutor impleme
                 managedConn = null;
             } else {
                 // install an auto-release entity
-                entity = new BasicManagedEntity(entity, managedConn, reuse);
+                entity = new ManagedEntity(entity, managedConn, reuse);
                 response.setEntity(entity);
             }
 

Copied: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ManagedEntity.java (from r1348580, httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ManagedEntity.java?p2=httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ManagedEntity.java&p1=httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java&r1=1348580&r2=1348648&rev=1348648&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java (original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ManagedEntity.java Sun Jun 10 19:27:07 2012
@@ -25,7 +25,7 @@
  *
  */
 
-package org.apache.http.conn;
+package org.apache.http.impl.client.exec;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -33,49 +33,31 @@ import java.io.OutputStream;
 import java.net.SocketException;
 
 import org.apache.http.annotation.NotThreadSafe;
+import org.apache.http.conn.EofSensorInputStream;
+import org.apache.http.conn.EofSensorWatcher;
+import org.apache.http.conn.ManagedClientConnection;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.entity.HttpEntityWrapper;
 import org.apache.http.util.EntityUtils;
 
 /**
- * An entity that releases a {@link ManagedClientConnection connection}.
- * A {@link ManagedClientConnection} will
- * typically <i>not</i> return a managed entity, but you can replace
- * the unmanaged entity in the response with a managed one.
+ * An entity that automatically releases a {@link ManagedClientConnection connection}.
  *
- * @since 4.0
+ * @since 4.3
  */
 @NotThreadSafe
-public class BasicManagedEntity extends HttpEntityWrapper
-    implements ConnectionReleaseTrigger, EofSensorWatcher {
+class ManagedEntity extends HttpEntityWrapper implements EofSensorWatcher {
 
-    /** The connection to release. */
-    protected ManagedClientConnection managedConn;
+    private ManagedClientConnection managedConn;
+    private final boolean attemptReuse;
 
-    /** Whether to keep the connection alive. */
-    protected final boolean attemptReuse;
-
-    /**
-     * Creates a new managed entity that can release a connection.
-     *
-     * @param entity    the entity of which to wrap the content.
-     *                  Note that the argument entity can no longer be used
-     *                  afterwards, since the content will be taken by this
-     *                  managed entity.
-     * @param conn      the connection to release
-     * @param reuse     whether the connection should be re-used
-     */
-    public BasicManagedEntity(HttpEntity entity,
-                              ManagedClientConnection conn,
-                              boolean reuse) {
+    public ManagedEntity(
+            final HttpEntity entity,
+            final ManagedClientConnection managedConn,
+            boolean reuse) {
         super(entity);
-
-        if (conn == null)
-            throw new IllegalArgumentException
-                ("Connection may not be null.");
-
-        this.managedConn = conn;
+        this.managedConn = managedConn;
         this.attemptReuse = reuse;
     }
 
@@ -86,27 +68,25 @@ public class BasicManagedEntity extends 
 
     @Override
     public InputStream getContent() throws IOException {
-        return new EofSensorInputStream(wrappedEntity.getContent(), this);
+        return new EofSensorInputStream(this.wrappedEntity.getContent(), this);
     }
 
     private void ensureConsumed() throws IOException {
-        if (managedConn == null)
+        if (this.managedConn == null) {
             return;
-
+        }
         try {
-            if (attemptReuse) {
+            if (this.attemptReuse) {
                 // this will not trigger a callback from EofSensorInputStream
-                EntityUtils.consume(wrappedEntity);
-                managedConn.markReusable();
+                EntityUtils.consume(this.wrappedEntity);
+                releaseConnection();
             }
         } finally {
-            releaseManagedConnection();
+            cleanup();
         }
     }
 
-    /**
-     * @deprecated (4.1) Use {@link EntityUtils#consume(HttpEntity)}
-     */
+    @Deprecated
     @Override
     public void consumeContent() throws IOException {
         ensureConsumed();
@@ -118,44 +98,31 @@ public class BasicManagedEntity extends 
         ensureConsumed();
     }
 
-    public void releaseConnection() throws IOException {
-        ensureConsumed();
-    }
-
-    public void abortConnection() throws IOException {
-
-        if (managedConn != null) {
-            try {
-                managedConn.abortConnection();
-            } finally {
-                managedConn = null;
-            }
-        }
-    }
-
-    public boolean eofDetected(InputStream wrapped) throws IOException {
+    public boolean eofDetected(final InputStream wrapped) throws IOException {
         try {
-            if (attemptReuse && (managedConn != null)) {
+            if (this.attemptReuse && (this.managedConn != null)) {
                 // there may be some cleanup required, such as
                 // reading trailers after the response body:
                 wrapped.close();
-                managedConn.markReusable();
+                this.managedConn.markReusable();
+                releaseConnection();
             }
         } finally {
-            releaseManagedConnection();
+            cleanup();
         }
         return false;
     }
 
     public boolean streamClosed(InputStream wrapped) throws IOException {
         try {
-            if (attemptReuse && (managedConn != null)) {
-                boolean valid = managedConn.isOpen();
+            if (this.attemptReuse && (this.managedConn != null)) {
+                boolean valid = this.managedConn.isOpen();
                 // this assumes that closing the stream will
                 // consume the remainder of the response body:
                 try {
                     wrapped.close();
-                    managedConn.markReusable();
+                    this.managedConn.markReusable();
+                    releaseConnection();
                 } catch (SocketException ex) {
                     if (valid) {
                         throw ex;
@@ -163,36 +130,27 @@ public class BasicManagedEntity extends 
                 }
             }
         } finally {
-            releaseManagedConnection();
+            cleanup();
         }
         return false;
     }
 
     public boolean streamAbort(InputStream wrapped) throws IOException {
-        if (managedConn != null) {
-            managedConn.abortConnection();
-        }
+        cleanup();
         return false;
     }
 
-    /**
-     * Releases the connection gracefully.
-     * The connection attribute will be nullified.
-     * Subsequent invocations are no-ops.
-     *
-     * @throws IOException      in case of an IO problem.
-     *         The connection attribute will be nullified anyway.
-     */
-    protected void releaseManagedConnection()
-        throws IOException {
-
-        if (managedConn != null) {
-            try {
-                managedConn.releaseConnection();
-            } finally {
-                managedConn = null;
-            }
+    private void releaseConnection() throws IOException {
+        if (this.managedConn != null) {
+            this.managedConn.releaseConnection();
+            this.managedConn = null;
         }
     }
 
+    private void cleanup() throws IOException {
+        if (this.managedConn != null) {
+            this.managedConn.abortConnection();
+            this.managedConn = null;
+        }
+    }
 }

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/RetryFacade.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/RetryFacade.java?rev=1348648&r1=1348647&r2=1348648&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/RetryFacade.java (original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/RetryFacade.java Sun Jun 10 19:27:07 2012
@@ -31,6 +31,7 @@ import java.io.IOException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.http.Header;
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
@@ -80,6 +81,7 @@ public class RetryFacade implements Http
         if (context == null) {
             throw new IllegalArgumentException("HTTP context may not be null");
         }
+        Header[] origheaders = request.getAllHeaders();
         for (int execCount = 0;; execCount++) {
             try {
                 this.requestExecutor.execute(route, request, context, execAware);
@@ -103,6 +105,7 @@ public class RetryFacade implements Http
                         throw new NonRepeatableRequestException("Cannot retry request " +
                                 "with a non-repeatable request entity", ex);
                     }
+                    request.setHeaders(origheaders);
                     this.log.info("Retrying request");
                 } else {
                     throw ex;