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 2011/12/23 15:49:22 UTC

svn commit: r1222696 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/nio/entity/ main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/nio/integration/

Author: olegk
Date: Fri Dec 23 14:49:21 2011
New Revision: 1222696

URL: http://svn.apache.org/viewvc?rev=1222696&view=rev
Log:
Improved content handling in AbstractAsyncResponseConsumer / AbstractAsyncRequestConsumer

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/HttpAsyncContentProducer.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/HttpAsyncContentProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/HttpAsyncContentProducer.java?rev=1222696&r1=1222695&r2=1222696&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/HttpAsyncContentProducer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/HttpAsyncContentProducer.java Fri Dec 23 14:49:21 2011
@@ -47,7 +47,7 @@ public interface HttpAsyncContentProduce
      * The {@link IOControl} interface can be used to suspend output events
      * if the entity is temporarily unable to produce more content.
      * <p>
-     * When all content is finished, the producer <b>MUST</b> call 
+     * When all content is finished, the producer <b>MUST</b> call
      * {@link ContentEncoder#complete()}. Failure to do so may cause the entity
      * to be incorrectly delimited.
      *

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java?rev=1222696&r1=1222695&r2=1222696&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java Fri Dec 23 14:49:21 2011
@@ -28,10 +28,12 @@ package org.apache.http.nio.protocol;
 
 import java.io.IOException;
 
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.entity.ContentType;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.IOControl;
 import org.apache.http.protocol.HttpContext;
@@ -65,7 +67,18 @@ public abstract class AbstractAsyncReque
      * @throws HttpException in case of HTTP protocol violation
      * @throws IOException in case of an I/O error
      */
-    protected abstract void onRequestReceived(HttpRequest request) throws HttpException, IOException;
+    protected abstract void onRequestReceived(
+            HttpRequest request) throws HttpException, IOException;
+
+    /**
+     * Invoked if the request message encloses a content entity.
+     *
+     * @param entity HTTP entity
+     * @param contentType expected content type.
+     * @throws IOException in case of an I/O error
+     */
+    protected abstract void onEntityEnclosed(
+            HttpEntity entity, ContentType contentType) throws IOException;
 
     /**
      * Invoked to process a chunk of content from the {@link ContentDecoder}.
@@ -103,6 +116,13 @@ public abstract class AbstractAsyncReque
     public final synchronized void requestReceived(
             final HttpRequest request) throws HttpException, IOException {
         onRequestReceived(request);
+        if (request instanceof HttpEntityEnclosingRequest) {
+            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+            if (entity != null) {
+                ContentType contentType = ContentType.getOrDefault(entity);
+                onEntityEnclosed(entity, contentType);
+            }
+        }
     }
 
     /**

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java?rev=1222696&r1=1222695&r2=1222696&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java Fri Dec 23 14:49:21 2011
@@ -28,9 +28,11 @@ package org.apache.http.nio.protocol;
 
 import java.io.IOException;
 
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
 import org.apache.http.HttpResponse;
 import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.entity.ContentType;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.IOControl;
 import org.apache.http.protocol.HttpContext;
@@ -63,7 +65,8 @@ public abstract class AbstractAsyncRespo
      * @throws HttpException in case of HTTP protocol violation
      * @throws IOException in case of an I/O error
      */
-    protected abstract void onResponseReceived(final HttpResponse response) throws HttpException, IOException;
+    protected abstract void onResponseReceived(
+            HttpResponse response) throws HttpException, IOException;
 
     /**
      * Invoked to process a chunk of content from the {@link ContentDecoder}.
@@ -78,7 +81,17 @@ public abstract class AbstractAsyncRespo
      * @throws IOException in case of an I/O error
      */
     protected abstract void onContentReceived(
-            final ContentDecoder decoder, final IOControl ioctrl) throws IOException;
+            ContentDecoder decoder, IOControl ioctrl) throws IOException;
+
+    /**
+     * Invoked if the response message encloses a content entity.
+     *
+     * @param entity HTTP entity
+     * @param contentType expected content type.
+     * @throws IOException in case of an I/O error
+     */
+    protected abstract void onEntityEnclosed(
+            HttpEntity entity, ContentType contentType) throws IOException;
 
     /**
      * Invoked to generate a result object from the received HTTP response
@@ -101,6 +114,11 @@ public abstract class AbstractAsyncRespo
     public final synchronized void responseReceived(
             final HttpResponse response) throws IOException, HttpException {
         onResponseReceived(response);
+        HttpEntity entity = response.getEntity();
+        if (entity != null) {
+            ContentType contentType = ContentType.getOrDefault(entity);
+            onEntityEnclosed(entity, contentType);
+        }
     }
 
     /**

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java?rev=1222696&r1=1222695&r2=1222696&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java Fri Dec 23 14:49:21 2011
@@ -33,6 +33,7 @@ import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpRequest;
 import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.entity.ContentType;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.IOControl;
 import org.apache.http.nio.entity.ContentBufferEntity;
@@ -78,6 +79,10 @@ public class BasicAsyncRequestConsumer e
     }
 
     @Override
+    protected void onEntityEnclosed(final HttpEntity entity, final ContentType contentType) {
+    }
+
+    @Override
     protected void onContentReceived(
             final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
         if (this.buf == null) {

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java?rev=1222696&r1=1222695&r2=1222696&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java Fri Dec 23 14:49:21 2011
@@ -32,6 +32,7 @@ import org.apache.http.ContentTooLongExc
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.entity.ContentType;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.IOControl;
 import org.apache.http.nio.entity.ContentBufferEntity;
@@ -74,6 +75,10 @@ public class BasicAsyncResponseConsumer 
     }
 
     @Override
+    protected void onEntityEnclosed(final HttpEntity entity, final ContentType contentType) {
+    }
+
+    @Override
     protected void onContentReceived(
             final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
         if (this.buf == null) {

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java?rev=1222696&r1=1222695&r2=1222696&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java Fri Dec 23 14:49:21 2011
@@ -319,6 +319,7 @@ public class HttpAsyncClientProtocolHand
     private void closeHandler(final State state) {
         HttpAsyncRequestExecutionHandler<?> handler = state.getHandler();
         if (handler != null) {
+            state.setHandler(null);
             try {
                 handler.close();
             } catch (IOException ioex) {
@@ -439,7 +440,6 @@ public class HttpAsyncClientProtocolHand
             this.requestState = MessageState.READY;
             this.response = null;
             this.request = null;
-            this.handler = null;
             this.timeout = 0;
         }
 

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java?rev=1222696&r1=1222695&r2=1222696&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java Fri Dec 23 14:49:21 2011
@@ -52,7 +52,7 @@ public interface HttpAsyncRequestHandler
      * the request and consume message content if enclosed. The consumer
      * can optionally parse or transform the message content into a structured
      * object which is then passed onto
-     * the {@link #handle(Object, HttpAsyncServiceExchange, HttpContext))}
+     * the {@link #handle(Object, HttpAsyncServiceExchange, HttpContext)}
      * method for further processing.
      *
      * @param request the entity enclosing request.

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java?rev=1222696&r1=1222695&r2=1222696&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java Fri Dec 23 14:49:21 2011
@@ -35,6 +35,7 @@ import java.util.concurrent.ExecutionExc
 import java.util.concurrent.Future;
 
 import org.apache.http.HttpCoreNIOTestBase;
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequestFactory;
 import org.apache.http.HttpResponse;
@@ -44,6 +45,7 @@ import org.apache.http.LoggingNHttpServe
 import org.apache.http.MalformedChunkCodingException;
 import org.apache.http.TruncatedChunkException;
 import org.apache.http.entity.ContentLengthStrategy;
+import org.apache.http.entity.ContentType;
 import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.http.impl.io.HttpTransportMetricsImpl;
@@ -241,6 +243,10 @@ public class TestTruncatedChunks extends
         }
 
         @Override
+        protected void onEntityEnclosed(final HttpEntity entity, final ContentType contentType) {
+        }
+
+        @Override
         protected void onContentReceived(
                 final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
             boolean finished = false;