hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1790666 - in /httpcomponents/httpcore/trunk: httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/http2/ httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http/ httpcore5-testing/src/test/java/org/apache/...
Date Sat, 08 Apr 2017 09:32:06 GMT
Author: olegk
Date: Sat Apr  8 09:32:06 2017
New Revision: 1790666

URL: http://svn.apache.org/viewvc?rev=1790666&view=rev
Log:
Improved implementation of async consumers

Added:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java
  (contents, props changed)
      - copied, changed from r1790665, httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicAsyncPushHandler.java
Removed:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicAsyncPushHandler.java
Modified:
    httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/http2/Http2CompatibilityTest.java
    httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http/Http1IntegrationTest.java
    httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http2/Http2IntegrationTest.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncRequestConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractBinAsyncEntityConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/NoopEntityConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractCharAsyncEntityConsumer.java

Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/http2/Http2CompatibilityTest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/http2/Http2CompatibilityTest.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/http2/Http2CompatibilityTest.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/http2/Http2CompatibilityTest.java
Sat Apr  8 09:32:06 2017
@@ -53,7 +53,7 @@ import org.apache.hc.core5.http.nio.Basi
 import org.apache.hc.core5.http.nio.BasicResponseConsumer;
 import org.apache.hc.core5.http.nio.entity.NoopEntityConsumer;
 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
-import org.apache.hc.core5.http.nio.support.BasicAsyncPushHandler;
+import org.apache.hc.core5.http.nio.support.AbstractAsyncPushHandler;
 import org.apache.hc.core5.http2.config.H2Config;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.testing.nio.http.ClientSessionEndpoint;
@@ -127,7 +127,7 @@ public class Http2CompatibilityTest {
 
                 @Override
                 public AsyncPushConsumer get() {
-                    return new BasicAsyncPushHandler<Void>(new BasicResponseConsumer<>(new
NoopEntityConsumer())) {
+                    return new AbstractAsyncPushHandler<Message<HttpResponse, Void>>(new
BasicResponseConsumer<>(new NoopEntityConsumer())) {
 
                         @Override
                         protected void handleResponse(

Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http/Http1IntegrationTest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http/Http1IntegrationTest.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http/Http1IntegrationTest.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http/Http1IntegrationTest.java
Sat Apr  8 09:32:06 2017
@@ -1398,7 +1398,8 @@ public class Http1IntegrationTest extend
         final ClientSessionEndpoint streamEndpoint = connectFuture.get();
 
         final AsyncRequestProducer requestProducer = new BasicRequestProducer("GET", createRequestURI(serverEndpoint,
"/hello"));
-        final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(new
StringAsyncEntityConsumer());
+        final StringAsyncEntityConsumer entityConsumer = new StringAsyncEntityConsumer();
+        final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(entityConsumer);
         final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(requestProducer,
responseConsumer, null);
         try {
             future1.get();
@@ -1406,7 +1407,7 @@ public class Http1IntegrationTest extend
         } catch (final ExecutionException ex) {
             final Throwable cause = ex.getCause();
             Assert.assertTrue(cause instanceof MalformedChunkCodingException);
-            Assert.assertEquals("garbage", responseConsumer.getResponseContent());
+            Assert.assertEquals("garbage", entityConsumer.generateContent());
         }
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http2/Http2IntegrationTest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http2/Http2IntegrationTest.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http2/Http2IntegrationTest.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/http2/Http2IntegrationTest.java
Sat Apr  8 09:32:06 2017
@@ -92,8 +92,8 @@ import org.apache.hc.core5.http.nio.enti
 import org.apache.hc.core5.http.nio.entity.NoopEntityConsumer;
 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
+import org.apache.hc.core5.http.nio.support.AbstractAsyncPushHandler;
 import org.apache.hc.core5.http.nio.support.AbstractServerExchangeHandler;
-import org.apache.hc.core5.http.nio.support.BasicAsyncPushHandler;
 import org.apache.hc.core5.http.nio.support.ResponseTrigger;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http2.H2Error;
@@ -550,7 +550,7 @@ public class Http2IntegrationTest extend
 
             @Override
             public AsyncPushConsumer get() {
-                return new BasicAsyncPushHandler<String>(new BasicResponseConsumer<>(new
StringAsyncEntityConsumer())) {
+                return new AbstractAsyncPushHandler<Message<HttpResponse, String>>(new
BasicResponseConsumer<>(new StringAsyncEntityConsumer())) {
 
                     @Override
                     protected void handleResponse(

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncRequestConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncRequestConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncRequestConsumer.java
Sat Apr  8 09:32:06 2017
@@ -44,4 +44,6 @@ public interface AsyncRequestConsumer<T>
 
     void failed(Exception cause);
 
+    T getResult();
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java
Sat Apr  8 09:32:06 2017
@@ -44,4 +44,6 @@ public interface AsyncResponseConsumer<T
 
     void failed(Exception cause);
 
+    T getResult();
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestConsumer.java
Sat Apr  8 09:32:06 2017
@@ -45,8 +45,10 @@ public class BasicRequestConsumer<T> imp
 
     private final AsyncEntityConsumer<T> dataConsumer;
 
+    private volatile Message<HttpRequest, T> result;
+
     public BasicRequestConsumer(final AsyncEntityConsumer<T> dataConsumer) {
-        this.dataConsumer = dataConsumer;
+        this.dataConsumer = Args.notNull(dataConsumer, "Data consumer");
     }
 
     @Override
@@ -55,28 +57,41 @@ public class BasicRequestConsumer<T> imp
             final EntityDetails entityDetails,
             final FutureCallback<Message<HttpRequest, T>> resultCallback) throws
HttpException, IOException {
         Args.notNull(request, "Request");
-        Args.notNull(resultCallback, "Result callback");
         if (entityDetails != null) {
             dataConsumer.streamStart(entityDetails, new FutureCallback<T>() {
 
                 @Override
-                public void completed(final T result) {
-                    resultCallback.completed(new Message<>(request, result));
+                public void completed(final T body) {
+                    result = new Message<>(request, body);
+                    if (resultCallback != null) {
+                        resultCallback.completed(result);
+                    }
+                    dataConsumer.releaseResources();
                 }
 
                 @Override
                 public void failed(final Exception ex) {
-                    resultCallback.failed(ex);
+                    if (resultCallback != null) {
+                        resultCallback.failed(ex);
+                    }
+                    dataConsumer.releaseResources();
                 }
 
                 @Override
                 public void cancelled() {
-                    resultCallback.cancelled();
+                    if (resultCallback != null) {
+                        resultCallback.cancelled();
+                    }
+                    dataConsumer.releaseResources();
                 }
 
             });
         } else {
-            resultCallback.completed(new Message<>(request, (T) null));
+            result = new Message<>(request, null);
+            if (resultCallback != null) {
+                resultCallback.completed(result);
+            }
+            dataConsumer.releaseResources();
         }
     }
 
@@ -101,6 +116,11 @@ public class BasicRequestConsumer<T> imp
     }
 
     @Override
+    public Message<HttpRequest, T> getResult() {
+        return result;
+    }
+
+    @Override
     public void releaseResources() {
         dataConsumer.releaseResources();
     }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java
Sat Apr  8 09:32:06 2017
@@ -29,7 +29,6 @@ package org.apache.hc.core5.http.nio;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.hc.core5.concurrent.FutureCallback;
 import org.apache.hc.core5.http.EntityDetails;
@@ -45,11 +44,11 @@ import org.apache.hc.core5.util.Args;
 public class BasicResponseConsumer<T> implements AsyncResponseConsumer<Message<HttpResponse,
T>> {
 
     private final AsyncEntityConsumer<T> dataConsumer;
-    private final AtomicReference<Exception> exception;
+
+    private volatile Message<HttpResponse, T> result;
 
     public BasicResponseConsumer(final AsyncEntityConsumer<T> dataConsumer) {
         this.dataConsumer = Args.notNull(dataConsumer, "Consumer");
-        this.exception = new AtomicReference<>(null);
     }
 
     @Override
@@ -58,32 +57,41 @@ public class BasicResponseConsumer<T> im
             final EntityDetails entityDetails,
             final FutureCallback<Message<HttpResponse, T>> resultCallback) throws
HttpException, IOException {
         Args.notNull(response, "Response");
-        Args.notNull(resultCallback, "Result callback");
 
         if (entityDetails != null) {
             dataConsumer.streamStart(entityDetails, new FutureCallback<T>() {
 
                 @Override
-                public void completed(final T result) {
-                    resultCallback.completed(new Message<>(response, result));
+                public void completed(final T body) {
+                    result = new Message<>(response, body);
+                    if (resultCallback != null) {
+                        resultCallback.completed(result);
+                    }
+                    resultCallback.completed(result);
                 }
 
                 @Override
                 public void failed(final Exception ex) {
-                    resultCallback.failed(ex);
+                    if (resultCallback != null) {
+                        resultCallback.failed(ex);
+                    }
                 }
 
                 @Override
                 public void cancelled() {
-                    resultCallback.cancelled();
+                    if (resultCallback != null) {
+                        resultCallback.cancelled();
+                    }
                 }
 
             });
         } else {
-            resultCallback.completed(new Message<HttpResponse, T>(response, null));
+            result = new Message<>(response, null);
+            if (resultCallback != null) {
+                resultCallback.completed(result);
+            }
             dataConsumer.releaseResources();
         }
-
     }
 
     @Override
@@ -103,17 +111,12 @@ public class BasicResponseConsumer<T> im
 
     @Override
     public void failed(final Exception cause) {
-        if (exception.compareAndSet(null, cause)) {
-            dataConsumer.failed(cause);
-        }
+        releaseResources();
     }
 
-    public Exception getException() {
-        return exception.get();
-    }
-
-    public T getResponseContent() {
-        return dataConsumer.getContent();
+    @Override
+    public Message<HttpResponse, T> getResult() {
+        return result;
     }
 
     @Override

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractBinAsyncEntityConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractBinAsyncEntityConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractBinAsyncEntityConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractBinAsyncEntityConsumer.java
Sat Apr  8 09:32:06 2017
@@ -39,22 +39,47 @@ import org.apache.hc.core5.util.Args;
 
 public abstract class AbstractBinAsyncEntityConsumer<T> extends AbstractBinDataConsumer
implements AsyncEntityConsumer<T> {
 
-    protected abstract void start(ContentType contentType, FutureCallback<T> resultCallback)
throws HttpException, IOException;
+    private volatile FutureCallback<T> resultCallback;
+    private volatile T content;
+
+    protected abstract void streamStart(ContentType contentType) throws HttpException, IOException;
+
+    protected abstract T generateContent() throws IOException;
 
     @Override
     public final void streamStart(
             final EntityDetails entityDetails,
             final FutureCallback<T> resultCallback) throws IOException, HttpException
{
         Args.notNull(resultCallback, "Result callback");
+        this.resultCallback = resultCallback;
         try {
             final ContentType contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType())
: null;
-            start(contentType, resultCallback);
-            if (entityDetails == null) {
-                completed();
-            }
+            streamStart(contentType);
         } catch (final UnsupportedCharsetException ex) {
             throw new UnsupportedEncodingException(ex.getMessage());
         }
     }
 
+    @Override
+    protected final void completed() throws IOException {
+        content = generateContent();
+        if (resultCallback != null) {
+            resultCallback.completed(content);
+        }
+        releaseResources();
+    }
+
+    @Override
+    public final void failed(final Exception cause) {
+        if (resultCallback != null) {
+            resultCallback.failed(cause);
+        }
+        releaseResources();
+    }
+
+    @Override
+    public final T getContent() {
+        return content;
+    }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java
Sat Apr  8 09:32:06 2017
@@ -44,13 +44,19 @@ import org.apache.hc.core5.util.Args;
  */
 public abstract class AbstractCharAsyncEntityConsumer<T> extends AbstractCharDataConsumer
implements AsyncEntityConsumer<T> {
 
-    protected abstract void start(ContentType contentType, FutureCallback<T> resultCallback)
throws HttpException, IOException;
+    private volatile FutureCallback<T> resultCallback;
+    private volatile T content;
+
+    protected abstract void streamStart(ContentType contentType) throws HttpException, IOException;
+
+    protected abstract T generateContent() throws IOException;
 
     @Override
     public final void streamStart(
             final EntityDetails entityDetails,
             final FutureCallback<T> resultCallback) throws IOException, HttpException
{
         Args.notNull(resultCallback, "Result callback");
+        this.resultCallback = resultCallback;
         try {
             final ContentType contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType())
: null;
             Charset charset = contentType != null ? contentType.getCharset() : null;
@@ -58,13 +64,32 @@ public abstract class AbstractCharAsyncE
                 charset = StandardCharsets.US_ASCII;
             }
             setCharset(charset);
-            start(contentType, resultCallback);
-            if (entityDetails == null) {
-                completed();
-            }
+            streamStart(contentType);
         } catch (final UnsupportedCharsetException ex) {
             throw new UnsupportedEncodingException(ex.getMessage());
         }
     }
 
+    @Override
+    protected final void completed() throws IOException {
+        content = generateContent();
+        if (resultCallback != null) {
+            resultCallback.completed(content);
+        }
+        releaseResources();
+    }
+
+    @Override
+    public final void failed(final Exception cause) {
+        if (resultCallback != null) {
+            resultCallback.failed(cause);
+        }
+        releaseResources();
+    }
+
+    @Override
+    public final T getContent() {
+        return content;
+    }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java
Sat Apr  8 09:32:06 2017
@@ -73,10 +73,7 @@ public abstract class AbstractCharDataCo
 
     private void doDecode(final boolean endOfStream) throws IOException {
         charbuf.flip();
-        final int chunk = charbuf.remaining();
-        if (chunk > 0) {
-            data(charbuf, endOfStream);
-        }
+        data(charbuf, endOfStream);
         charbuf.clear();
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/NoopEntityConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/NoopEntityConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/NoopEntityConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/NoopEntityConsumer.java
Sat Apr  8 09:32:06 2017
@@ -70,7 +70,9 @@ public final class NoopEntityConsumer im
 
     @Override
     public void failed(final Exception cause) {
-        releaseResources();
+        if (resultCallback != null) {
+            resultCallback.failed(cause);
+        }
     }
 
     @Override

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java
Sat Apr  8 09:32:06 2017
@@ -28,9 +28,7 @@ package org.apache.hc.core5.http.nio.ent
 
 import java.io.IOException;
 import java.nio.CharBuffer;
-import java.util.concurrent.atomic.AtomicReference;
 
-import org.apache.hc.core5.concurrent.FutureCallback;
 import org.apache.hc.core5.http.ContentType;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.util.Args;
@@ -40,15 +38,11 @@ public class StringAsyncEntityConsumer e
 
     private final int capacityIncrement;
     private final CharArrayBuffer content;
-    private final AtomicReference<Exception> exception;
-
-    private FutureCallback<String> resultCallback;
 
     public StringAsyncEntityConsumer(final int capacityIncrement) {
         Args.positive(capacityIncrement, "Capacity increment");
         this.capacityIncrement = capacityIncrement;
         this.content = new CharArrayBuffer(1024);
-        this.exception = new AtomicReference<>(null);
     }
 
     public StringAsyncEntityConsumer() {
@@ -56,9 +50,7 @@ public class StringAsyncEntityConsumer e
     }
 
     @Override
-    protected final void start(
-            final ContentType contentType, final FutureCallback<String> resultCallback)
throws HttpException, IOException {
-        this.resultCallback = resultCallback;
+    protected final void streamStart(final ContentType contentType) throws HttpException,
IOException {
     }
 
     @Override
@@ -76,28 +68,7 @@ public class StringAsyncEntityConsumer e
     }
 
     @Override
-    protected void completed() throws IOException {
-        if (resultCallback != null) {
-            resultCallback.completed(content.toString());
-        }
-    }
-
-    @Override
-    public final void failed(final Exception cause) {
-        if (exception.compareAndSet(null, cause)) {
-            if (resultCallback != null) {
-                resultCallback.failed(cause);
-            }
-            releaseResources();
-        }
-    }
-
-    public Exception getException() {
-        return exception.get();
-    }
-
-    @Override
-    public String getContent() {
+    public String generateContent() {
         return content.toString();
     }
 

Copied: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java
(from r1790665, httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicAsyncPushHandler.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java?p2=httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicAsyncPushHandler.java&r1=1790665&r2=1790666&rev=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicAsyncPushHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java
Sat Apr  8 09:32:06 2017
@@ -36,7 +36,6 @@ import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpResponse;
-import org.apache.hc.core5.http.Message;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
 import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
 import org.apache.hc.core5.http.nio.CapacityChannel;
@@ -45,17 +44,16 @@ import org.apache.hc.core5.util.Args;
 /**
  * @since 5.0
  */
-public class BasicAsyncPushHandler<T> implements AsyncPushConsumer {
+public abstract class AbstractAsyncPushHandler<T> implements AsyncPushConsumer {
 
-    private final AsyncResponseConsumer<Message<HttpResponse, T>> responseConsumer;
+    private final AsyncResponseConsumer<T> responseConsumer;
 
-    public BasicAsyncPushHandler(final AsyncResponseConsumer<Message<HttpResponse,
T>> responseConsumer) {
+    public AbstractAsyncPushHandler(final AsyncResponseConsumer<T> responseConsumer)
{
         this.responseConsumer = Args.notNull(responseConsumer, "Response consumer");
     }
 
-    protected void handleResponse(
-            final HttpRequest promise, final Message<HttpResponse, T> responseMessage)
throws IOException, HttpException {
-    }
+    protected abstract void handleResponse(
+            final HttpRequest promise, final T responseMessage) throws IOException, HttpException;
 
     protected void handleError(final HttpRequest promise, final Exception cause) {
     }
@@ -65,10 +63,10 @@ public class BasicAsyncPushHandler<T> im
             final HttpRequest promise,
             final HttpResponse response,
             final EntityDetails entityDetails) throws HttpException, IOException {
-        responseConsumer.consumeResponse(response, entityDetails, new FutureCallback<Message<HttpResponse,
T>>() {
+        responseConsumer.consumeResponse(response, entityDetails, new FutureCallback<T>()
{
 
             @Override
-            public void completed(final Message<HttpResponse, T> result) {
+            public void completed(final T result) {
                 try {
                     handleResponse(promise, result);
                 } catch (final Exception ex) {

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
Sat Apr  8 09:32:06 2017
@@ -67,8 +67,6 @@ public class TestAbstractBinAsyncEntityC
     static private class ByteArrayAsyncEntityConsumer extends AbstractBinAsyncEntityConsumer<byte[]>
{
 
         private final InternalBuffer buffer;
-        private volatile FutureCallback<byte[]> resultCallback;
-        private byte[] content;
 
         public ByteArrayAsyncEntityConsumer() {
             super();
@@ -76,10 +74,7 @@ public class TestAbstractBinAsyncEntityC
         }
 
         @Override
-        protected void start(
-                final ContentType contentType,
-                final FutureCallback<byte[]> resultCallback) throws HttpException,
IOException {
-            this.resultCallback = resultCallback;
+        protected void streamStart(final ContentType contentType) throws HttpException, IOException
{
         }
 
         @Override
@@ -93,20 +88,8 @@ public class TestAbstractBinAsyncEntityC
         }
 
         @Override
-        protected void completed() throws IOException {
-            content = buffer.toByteArray();
-            if (resultCallback != null) {
-                resultCallback.completed(content);
-            }
-        }
-
-        @Override
-        public void failed(final Exception cause) {
-        }
-
-        @Override
-        public byte[] getContent() {
-            return content;
+        protected byte[] generateContent() throws IOException {
+            return buffer.toByteArray();
         }
 
         @Override

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractCharAsyncEntityConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractCharAsyncEntityConsumer.java?rev=1790666&r1=1790665&r2=1790666&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractCharAsyncEntityConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractCharAsyncEntityConsumer.java
Sat Apr  8 09:32:06 2017
@@ -45,8 +45,6 @@ public class TestAbstractCharAsyncEntity
     static private class StringBuilderAsyncEntityConsumer extends AbstractCharAsyncEntityConsumer<String>
{
 
         private final StringBuilder buffer;
-        private volatile FutureCallback<String> resultCallback;
-        private String content;
 
         public StringBuilderAsyncEntityConsumer() {
             super();
@@ -54,10 +52,7 @@ public class TestAbstractCharAsyncEntity
         }
 
         @Override
-        protected void start(
-                final ContentType contentType,
-                final FutureCallback<String> resultCallback) throws HttpException,
IOException {
-            this.resultCallback = resultCallback;
+        protected void streamStart(final ContentType contentType) throws HttpException, IOException
{
         }
 
         @Override
@@ -71,24 +66,13 @@ public class TestAbstractCharAsyncEntity
         }
 
         @Override
-        protected void completed() throws IOException {
-            content = buffer.toString();
-            if (resultCallback != null) {
-                resultCallback.completed(content);
-            }
-        }
-
-        @Override
-        public void failed(final Exception cause) {
-        }
-
-        @Override
-        public String getContent() {
-            return content;
+        protected String generateContent() throws IOException {
+            return buffer.toString();
         }
 
         @Override
         public void releaseResources() {
+            buffer.setLength(0);
         }
 
     };



Mime
View raw message