Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AEB90E265 for ; Tue, 29 Jan 2013 14:35:28 +0000 (UTC) Received: (qmail 42282 invoked by uid 500); 29 Jan 2013 14:35:28 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 42227 invoked by uid 500); 29 Jan 2013 14:35:27 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 42218 invoked by uid 99); 29 Jan 2013 14:35:27 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 29 Jan 2013 14:35:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_FRT_STOCK2 X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 29 Jan 2013 14:35:19 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 2425823889DE for ; Tue, 29 Jan 2013 14:34:58 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1439908 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/nio/protocol/ Date: Tue, 29 Jan 2013 14:34:57 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130129143458.2425823889DE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Tue Jan 29 14:34:57 2013 New Revision: 1439908 URL: http://svn.apache.org/viewvc?rev=1439908&view=rev Log: Redesign of the client exchange handler API Added: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java - copied, changed from r1439907, httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java (with props) httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java (contents, props changed) - copied, changed from r1439907, httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestExecutionHandler.java Removed: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestExecutionHandler.java Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutionHandler.java httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequester.java Copied: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java (from r1439907, httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java) URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java?p2=httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java&p1=httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java&r1=1439907&r2=1439908&rev=1439908&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java Tue Jan 29 14:34:57 2013 @@ -32,7 +32,6 @@ import java.util.concurrent.Future; import org.apache.http.ConnectionReuseStrategy; import org.apache.http.HttpException; -import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.concurrent.BasicFuture; @@ -41,104 +40,58 @@ import org.apache.http.impl.DefaultConne import org.apache.http.nio.ContentDecoder; import org.apache.http.nio.ContentEncoder; import org.apache.http.nio.IOControl; -import org.apache.http.params.HttpParams; +import org.apache.http.nio.NHttpClientConnection; +import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpProcessor; import org.apache.http.util.Args; /** - * Basic implementation of {@link HttpAsyncRequestExecutionHandler} that executes + * Basic implementation of {@link HttpAsyncClientExchangeHandler} that executes * a single HTTP request / response exchange. * * @param the result type of request execution. - * @since 4.2 + * @since 4.3 */ -@SuppressWarnings("deprecation") -public class BasicAsyncRequestExecutionHandler implements HttpAsyncRequestExecutionHandler { +public class BasicAsyncClientExchangeHandler implements HttpAsyncClientExchangeHandler { private final HttpAsyncRequestProducer requestProducer; private final HttpAsyncResponseConsumer responseConsumer; private final BasicFuture future; private final HttpContext localContext; + private final NHttpClientConnection conn; private final HttpProcessor httppocessor; - private final ConnectionReuseStrategy reuseStrategy; + private final ConnectionReuseStrategy connReuseStrategy; private volatile boolean requestSent; /** - * @deprecated (4.3) use - * {@link BasicAsyncRequestExecutionHandler#BasicAsyncRequestExecutionHandler( - * HttpAsyncRequestProducer, HttpAsyncResponseConsumer, FutureCallback, HttpContext, - * HttpProcessor, ConnectionReuseStrategy)} - */ - @Deprecated - public BasicAsyncRequestExecutionHandler( - final HttpAsyncRequestProducer requestProducer, - final HttpAsyncResponseConsumer responseConsumer, - final FutureCallback callback, - final HttpContext localContext, - final HttpProcessor httppocessor, - final ConnectionReuseStrategy reuseStrategy, - final HttpParams params) { - super(); - Args.notNull(requestProducer, "Request producer"); - Args.notNull(responseConsumer, "Response consumer"); - Args.notNull(localContext, "HTTP context"); - Args.notNull(httppocessor, "HTTP processor"); - Args.notNull(reuseStrategy, "Connection reuse strategy"); - Args.notNull(params, "HTTP parameters"); - this.requestProducer = requestProducer; - this.responseConsumer = responseConsumer; - this.future = new BasicFuture(callback); - this.localContext = localContext; - this.httppocessor = httppocessor; - this.reuseStrategy = reuseStrategy; - } - - /** - * @deprecated (4.3) use - * {@link BasicAsyncRequestExecutionHandler#BasicAsyncRequestExecutionHandler( - * HttpAsyncRequestProducer, HttpAsyncResponseConsumer, FutureCallback, HttpContext, - * HttpProcessor, ConnectionReuseStrategy)} - */ - @Deprecated - public BasicAsyncRequestExecutionHandler( - final HttpAsyncRequestProducer requestProducer, - final HttpAsyncResponseConsumer responseConsumer, - final HttpContext localContext, - final HttpProcessor httppocessor, - final ConnectionReuseStrategy reuseStrategy, - final HttpParams params) { - this(requestProducer, responseConsumer, null, localContext, httppocessor, reuseStrategy, params); - } - - /** * Creates new instance of BasicAsyncRequestExecutionHandler. * * @param requestProducer the request producer. * @param responseConsumer the response consumer. * @param callback the future callback invoked when the operation is completed. * @param localContext the local execution context. + * @param conn the actual connection. * @param httppocessor the HTTP protocol processor. - * @param reuseStrategy the connection re-use strategy. If null - * {@link DefaultConnectionReuseStrategy#INSTANCE} will be used. - * - * @since 4.3 + * @param connReuseStrategy the connection re-use strategy. */ - public BasicAsyncRequestExecutionHandler( + public BasicAsyncClientExchangeHandler( final HttpAsyncRequestProducer requestProducer, final HttpAsyncResponseConsumer responseConsumer, final FutureCallback callback, final HttpContext localContext, + final NHttpClientConnection conn, final HttpProcessor httppocessor, - final ConnectionReuseStrategy reuseStrategy) { + final ConnectionReuseStrategy connReuseStrategy) { super(); this.requestProducer = Args.notNull(requestProducer, "Request producer"); this.responseConsumer = Args.notNull(responseConsumer, "Response consumer"); this.future = new BasicFuture(callback); this.localContext = Args.notNull(localContext, "HTTP context"); + this.conn = Args.notNull(conn, "HTTP connection"); this.httppocessor = Args.notNull(httppocessor, "HTTP processor"); - this.reuseStrategy = reuseStrategy != null ? reuseStrategy : + this.connReuseStrategy = connReuseStrategy != null ? connReuseStrategy : DefaultConnectionReuseStrategy.INSTANCE; } @@ -148,16 +101,16 @@ public class BasicAsyncRequestExecutionH * @param requestProducer the request producer. * @param responseConsumer the response consumer. * @param localContext the local execution context. + * @param conn the actual connection. * @param httppocessor the HTTP protocol processor. - * - * @since 4.3 */ - public BasicAsyncRequestExecutionHandler( + public BasicAsyncClientExchangeHandler( final HttpAsyncRequestProducer requestProducer, final HttpAsyncResponseConsumer responseConsumer, final HttpContext localContext, + final NHttpClientConnection conn, final HttpProcessor httppocessor) { - this(requestProducer, responseConsumer, null, localContext, httppocessor, null); + this(requestProducer, responseConsumer, null, localContext, conn, httppocessor, null); } public Future getFuture() { @@ -182,12 +135,12 @@ public class BasicAsyncRequestExecutionH } } - public HttpHost getTarget() { - return this.requestProducer.getTarget(); - } - public HttpRequest generateRequest() throws IOException, HttpException { - return this.requestProducer.generateRequest(); + HttpRequest request = this.requestProducer.generateRequest(); + this.localContext.setAttribute(ExecutionContext.HTTP_REQUEST, request); + this.localContext.setAttribute(ExecutionContext.HTTP_CONNECTION, this.conn); + this.httppocessor.process(request, this.localContext); + return request; } public void produceContent( @@ -195,19 +148,14 @@ public class BasicAsyncRequestExecutionH this.requestProducer.produceContent(encoder, ioctrl); } - public void requestCompleted(final HttpContext context) { - this.requestProducer.requestCompleted(context); + public void requestCompleted() { + this.requestProducer.requestCompleted(this.localContext); this.requestSent = true; } - public boolean isRepeatable() { - return false; - } - - public void resetRequest() { - } - public void responseReceived(final HttpResponse response) throws IOException, HttpException { + this.localContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response); + this.httppocessor.process(response, this.localContext); this.responseConsumer.responseReceived(response); } @@ -216,6 +164,27 @@ public class BasicAsyncRequestExecutionH this.responseConsumer.consumeContent(decoder, ioctrl); } + public void responseCompleted() { + try { + this.responseConsumer.responseCompleted(this.localContext); + final T result = this.responseConsumer.getResult(); + final Exception ex = this.responseConsumer.getException(); + if (ex == null) { + this.future.completed(result); + } else { + this.future.failed(ex); + } + releaseResources(); + } catch (final RuntimeException ex) { + failed(ex); + throw ex; + } + } + + public boolean keepAlive(final HttpResponse response) { + return this.connReuseStrategy.keepAlive(response, this.localContext); + } + public void failed(final Exception ex) { try { if (!this.requestSent) { @@ -243,43 +212,6 @@ public class BasicAsyncRequestExecutionH } } - public void responseCompleted(final HttpContext context) { - try { - this.responseConsumer.responseCompleted(context); - final T result = this.responseConsumer.getResult(); - final Exception ex = this.responseConsumer.getException(); - if (ex == null) { - this.future.completed(result); - } else { - this.future.failed(ex); - } - releaseResources(); - } catch (final RuntimeException ex) { - failed(ex); - throw ex; - } - } - - public T getResult() { - return this.responseConsumer.getResult(); - } - - public Exception getException() { - return this.responseConsumer.getException(); - } - - public HttpContext getContext() { - return this.localContext; - } - - public HttpProcessor getHttpProcessor() { - return this.httppocessor; - } - - public ConnectionReuseStrategy getConnectionReuseStrategy() { - return this.reuseStrategy; - } - public boolean isDone() { return this.responseConsumer.isDone(); } Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java?rev=1439908&r1=1439907&r2=1439908&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestExecutionHandler.java Tue Jan 29 14:34:57 2013 @@ -37,7 +37,6 @@ import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.concurrent.BasicFuture; import org.apache.http.concurrent.FutureCallback; -import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.nio.ContentDecoder; import org.apache.http.nio.ContentEncoder; import org.apache.http.nio.IOControl; @@ -52,8 +51,10 @@ import org.apache.http.util.Args; * * @param the result type of request execution. * @since 4.2 + * + * @deprecated (4.3) use {@link BasicAsyncClientExchangeHandler}. */ -@SuppressWarnings("deprecation") +@Deprecated public class BasicAsyncRequestExecutionHandler implements HttpAsyncRequestExecutionHandler { private final HttpAsyncRequestProducer requestProducer; @@ -65,13 +66,6 @@ public class BasicAsyncRequestExecutionH private volatile boolean requestSent; - /** - * @deprecated (4.3) use - * {@link BasicAsyncRequestExecutionHandler#BasicAsyncRequestExecutionHandler( - * HttpAsyncRequestProducer, HttpAsyncResponseConsumer, FutureCallback, HttpContext, - * HttpProcessor, ConnectionReuseStrategy)} - */ - @Deprecated public BasicAsyncRequestExecutionHandler( final HttpAsyncRequestProducer requestProducer, final HttpAsyncResponseConsumer responseConsumer, @@ -95,13 +89,6 @@ public class BasicAsyncRequestExecutionH this.reuseStrategy = reuseStrategy; } - /** - * @deprecated (4.3) use - * {@link BasicAsyncRequestExecutionHandler#BasicAsyncRequestExecutionHandler( - * HttpAsyncRequestProducer, HttpAsyncResponseConsumer, FutureCallback, HttpContext, - * HttpProcessor, ConnectionReuseStrategy)} - */ - @Deprecated public BasicAsyncRequestExecutionHandler( final HttpAsyncRequestProducer requestProducer, final HttpAsyncResponseConsumer responseConsumer, @@ -112,54 +99,6 @@ public class BasicAsyncRequestExecutionH this(requestProducer, responseConsumer, null, localContext, httppocessor, reuseStrategy, params); } - /** - * Creates new instance of BasicAsyncRequestExecutionHandler. - * - * @param requestProducer the request producer. - * @param responseConsumer the response consumer. - * @param callback the future callback invoked when the operation is completed. - * @param localContext the local execution context. - * @param httppocessor the HTTP protocol processor. - * @param reuseStrategy the connection re-use strategy. If null - * {@link DefaultConnectionReuseStrategy#INSTANCE} will be used. - * - * @since 4.3 - */ - public BasicAsyncRequestExecutionHandler( - final HttpAsyncRequestProducer requestProducer, - final HttpAsyncResponseConsumer responseConsumer, - final FutureCallback callback, - final HttpContext localContext, - final HttpProcessor httppocessor, - final ConnectionReuseStrategy reuseStrategy) { - super(); - this.requestProducer = Args.notNull(requestProducer, "Request producer"); - this.responseConsumer = Args.notNull(responseConsumer, "Response consumer"); - this.future = new BasicFuture(callback); - this.localContext = Args.notNull(localContext, "HTTP context"); - this.httppocessor = Args.notNull(httppocessor, "HTTP processor"); - this.reuseStrategy = reuseStrategy != null ? reuseStrategy : - DefaultConnectionReuseStrategy.INSTANCE; - } - - /** - * Creates new instance of BasicAsyncRequestExecutionHandler. - * - * @param requestProducer the request producer. - * @param responseConsumer the response consumer. - * @param localContext the local execution context. - * @param httppocessor the HTTP protocol processor. - * - * @since 4.3 - */ - public BasicAsyncRequestExecutionHandler( - final HttpAsyncRequestProducer requestProducer, - final HttpAsyncResponseConsumer responseConsumer, - final HttpContext localContext, - final HttpProcessor httppocessor) { - this(requestProducer, responseConsumer, null, localContext, httppocessor, null); - } - public Future getFuture() { return this.future; } Added: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java?rev=1439908&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java (added) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java Tue Jan 29 14:34:57 2013 @@ -0,0 +1,153 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.nio.protocol; + +import java.io.Closeable; +import java.io.IOException; + +import org.apache.http.HttpEntityEnclosingRequest; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.concurrent.Cancellable; +import org.apache.http.nio.ContentDecoder; +import org.apache.http.nio.ContentEncoder; +import org.apache.http.nio.IOControl; +import org.apache.http.protocol.HttpProcessor; + +/** + * HttpAsyncClientExchangeHandler represents a callback interface whose + * methods get invoked when executing one or multiple HTTP message exchanges + * on the client side. + *

+ * Individual HttpAsyncClientExchangeHandler are expected to make use of + * a {@link HttpProcessor} to generate mandatory protocol headers for all outgoing + * messages and apply common, cross-cutting message transformations to all incoming + * and outgoing messages. HttpAsyncClientExchangeHandlers can delegate + * implementation of application specific content generation and processing to + * a {@link HttpAsyncRequestProducer} and a {@link HttpAsyncResponseConsumer}. + * + * @since 4.3 + */ +public interface HttpAsyncClientExchangeHandler extends Closeable, Cancellable { + + /** + * Invoked to generate a HTTP request message head. The message is expected + * to implement the {@link HttpEntityEnclosingRequest} interface if it is + * to enclose a content entity. The {@link #produceContent(ContentEncoder, IOControl)} + * method will not be invoked if {@link HttpEntityEnclosingRequest#getEntity()} + * returns null. + * + * @return HTTP request message. + * @throws HttpException in case of HTTP protocol violation + * @throws IOException in case of an I/O error + */ + HttpRequest generateRequest() throws IOException, HttpException; + + /** + * Invoked to write out a chunk of content to the {@link ContentEncoder}. + * The {@link IOControl} interface can be used to suspend output event + * notifications if the producer is temporarily unable to produce more content. + *

+ * When all content is finished, the producer MUST call + * {@link ContentEncoder#complete()}. Failure to do so may cause the entity + * to be incorrectly delimited. + *

+ * Please note that the {@link ContentEncoder} object is not thread-safe and + * should only be used within the context of this method call. + * The {@link IOControl} object can be shared and used on other thread + * to resume output event notifications when more content is made available. + * + * @param encoder content encoder. + * @param ioctrl I/O control of the underlying connection. + * @throws IOException in case of an I/O error + */ + void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException; + + /** + * Invoked to signal that the request has been fully written out. + */ + void requestCompleted(); + + /** + * Invoked when a HTTP response message is received. Please note + * that the {@link #consumeContent(ContentDecoder, IOControl)} method + * will be invoked only if the response messages has a content entity + * enclosed. + * + * @param response HTTP response message. + * @throws HttpException in case of HTTP protocol violation + * @throws IOException in case of an I/O error + */ + void responseReceived(HttpResponse response) throws IOException, HttpException; + + /** + * Invoked to process a chunk of content from the {@link ContentDecoder}. + * The {@link IOControl} interface can be used to suspend input event + * notifications if the consumer is temporarily unable to process content. + *

+ * The consumer can use the {@link ContentDecoder#isCompleted()} method + * to find out whether or not the message content has been fully consumed. + *

+ * Please note that the {@link ContentDecoder} object is not thread-safe and + * should only be used within the context of this method call. + * The {@link IOControl} object can be shared and used on other thread + * to resume input event notifications when the consumer is capable of + * processing more content. + * + * @param decoder content decoder. + * @param ioctrl I/O control of the underlying connection. + * @throws IOException in case of an I/O error + */ + void consumeContent(ContentDecoder decoder, IOControl ioctrl) throws IOException; + + /** + * Invoked to signal that the response has been fully processed. + */ + void responseCompleted(); + + /** + * Determines if current connection can be kept alive after the given HTTP response. + * @param response message + * @return true to keep connection alive, false otherwise. + */ + boolean keepAlive(HttpResponse response); + + /** + * Invoked to signal that the response processing terminated abnormally. + * + * @param ex exception + */ + void failed(Exception ex); + + /** + * Determines whether or not the response processing completed. + */ + boolean isDone(); + +} Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutionHandler.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutionHandler.java?rev=1439908&r1=1439907&r2=1439908&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutionHandler.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutionHandler.java Tue Jan 29 14:34:57 2013 @@ -40,6 +40,7 @@ import org.apache.http.protocol.HttpProc * @param the result type of request execution. * @since 4.2 */ +@Deprecated public interface HttpAsyncRequestExecutionHandler extends HttpAsyncRequestProducer, HttpAsyncResponseConsumer { Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java?rev=1439908&r1=1439907&r2=1439908&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java Tue Jan 29 14:34:57 2013 @@ -32,7 +32,6 @@ import java.io.IOException; import java.net.SocketTimeoutException; import org.apache.http.ConnectionClosedException; -import org.apache.http.ConnectionReuseStrategy; import org.apache.http.HttpConnection; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpException; @@ -46,7 +45,6 @@ import org.apache.http.nio.ContentEncode import org.apache.http.nio.NHttpClientConnection; import org.apache.http.nio.NHttpClientEventHandler; import org.apache.http.nio.NHttpConnection; -import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpProcessor; import org.apache.http.util.Args; @@ -58,21 +56,22 @@ import org.apache.http.util.Asserts; * HttpAsyncRequestExecutor translates individual events fired through * the {@link NHttpClientEventHandler} interface into logically related HTTP * message exchanges. - *

- * HttpAsyncRequestExecutor relies on {@link HttpProcessor} - * to generate mandatory protocol headers for all outgoing messages and apply - * common, cross-cutting message transformations to all incoming and outgoing - * messages, whereas individual {@link HttpAsyncRequestExecutionHandler}s - * are expected to implement application specific content generation and - * processing. The caller is expected to pass an instance of - * {@link HttpAsyncRequestExecutionHandler} to be used for the next series + *

The caller is expected to pass an instance of + * {@link HttpAsyncClientExchangeHandler} to be used for the next series * of HTTP message exchanges through the connection context using * {@link #HTTP_HANDLER} attribute. HTTP exchange sequence is considered - * complete when the {@link HttpAsyncRequestExecutionHandler#isDone()} method + * complete when the {@link HttpAsyncClientExchangeHandler#isDone()} method * returns true. The {@link HttpAsyncRequester} utility class can * be used to facilitate initiation of asynchronous HTTP request execution. + *

+ * Individual HttpAsyncClientExchangeHandler are expected to make use of + * a {@link HttpProcessor} to generate mandatory protocol headers for all outgoing + * messages and apply common, cross-cutting message transformations to all incoming + * and outgoing messages. HttpAsyncClientExchangeHandlers can delegate + * implementation of application specific content generation and processing to + * a {@link HttpAsyncRequestProducer} and a {@link HttpAsyncResponseConsumer}. * - * @see HttpAsyncRequestExecutionHandler + * @see HttpAsyncClientExchangeHandler * * @since 4.2 */ @@ -109,7 +108,7 @@ public class HttpAsyncRequestExecutor im public void closed(final NHttpClientConnection conn) { final State state = getState(conn); - final HttpAsyncRequestExecutionHandler handler = getHandler(conn); + final HttpAsyncClientExchangeHandler handler = getHandler(conn); if (state == null || (handler != null && handler.isDone())) { closeHandler(handler); } @@ -121,7 +120,7 @@ public class HttpAsyncRequestExecutor im public void exception( final NHttpClientConnection conn, final Exception cause) { shutdownConnection(conn); - final HttpAsyncRequestExecutionHandler handler = getHandler(conn); + final HttpAsyncClientExchangeHandler handler = getHandler(conn); if (handler != null) { handler.failed(cause); } else { @@ -135,7 +134,7 @@ public class HttpAsyncRequestExecutor im if (state.getRequestState() != MessageState.READY) { return; } - HttpAsyncRequestExecutionHandler handler = getHandler(conn); + HttpAsyncClientExchangeHandler handler = getHandler(conn); if (handler != null && handler.isDone()) { closeHandler(handler); state.reset(); @@ -145,15 +144,7 @@ public class HttpAsyncRequestExecutor im return; } - final HttpContext context = handler.getContext(); - context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); - final HttpRequest request = handler.generateRequest(); - context.setAttribute(ExecutionContext.HTTP_REQUEST, request); - - final HttpProcessor httppocessor = handler.getHttpProcessor(); - httppocessor.process(request, context); - state.setRequest(request); conn.submitRequest(request); @@ -168,7 +159,7 @@ public class HttpAsyncRequestExecutor im state.setRequestState(MessageState.BODY_STREAM); } } else { - handler.requestCompleted(context); + handler.requestCompleted(); state.setRequestState(MessageState.COMPLETED); } } @@ -177,16 +168,15 @@ public class HttpAsyncRequestExecutor im final NHttpClientConnection conn, final ContentEncoder encoder) throws IOException { final State state = ensureNotNull(getState(conn)); - final HttpAsyncRequestExecutionHandler handler = ensureNotNull(getHandler(conn)); + final HttpAsyncClientExchangeHandler handler = ensureNotNull(getHandler(conn)); if (state.getRequestState() == MessageState.ACK_EXPECTED) { conn.suspendOutput(); return; } - final HttpContext context = handler.getContext(); handler.produceContent(encoder, conn); state.setRequestState(MessageState.BODY_STREAM); if (encoder.isCompleted()) { - handler.requestCompleted(context); + handler.requestCompleted(); state.setRequestState(MessageState.COMPLETED); } } @@ -194,7 +184,7 @@ public class HttpAsyncRequestExecutor im public void responseReceived( final NHttpClientConnection conn) throws HttpException, IOException { final State state = ensureNotNull(getState(conn)); - final HttpAsyncRequestExecutionHandler handler = ensureNotNull(getHandler(conn)); + final HttpAsyncClientExchangeHandler handler = ensureNotNull(getHandler(conn)); final HttpResponse response = conn.getHttpResponse(); final HttpRequest request = state.getRequest(); @@ -229,12 +219,6 @@ public class HttpAsyncRequestExecutor im handler.responseReceived(response); - final HttpContext context = handler.getContext(); - final HttpProcessor httpprocessor = handler.getHttpProcessor(); - - context.setAttribute(ExecutionContext.HTTP_RESPONSE, response); - httpprocessor.process(response, context); - state.setResponseState(MessageState.BODY_STREAM); if (!canResponseHaveBody(request, response)) { response.setEntity(null); @@ -247,7 +231,7 @@ public class HttpAsyncRequestExecutor im final NHttpClientConnection conn, final ContentDecoder decoder) throws IOException { final State state = ensureNotNull(getState(conn)); - final HttpAsyncRequestExecutionHandler handler = ensureNotNull(getHandler(conn)); + final HttpAsyncClientExchangeHandler handler = ensureNotNull(getHandler(conn)); handler.consumeContent(decoder, conn); state.setResponseState(MessageState.BODY_STREAM); if (decoder.isCompleted()) { @@ -260,7 +244,7 @@ public class HttpAsyncRequestExecutor im if (state != null) { if (state.getRequestState().compareTo(MessageState.READY) != 0) { state.invalidate(); - HttpAsyncRequestExecutionHandler handler = getHandler(conn); + HttpAsyncClientExchangeHandler handler = getHandler(conn); handler.failed(new ConnectionClosedException("Connection closed")); } } @@ -286,7 +270,7 @@ public class HttpAsyncRequestExecutor im return; } else { state.invalidate(); - HttpAsyncRequestExecutionHandler handler = getHandler(conn); + HttpAsyncClientExchangeHandler handler = getHandler(conn); if (handler != null) { handler.failed(new SocketTimeoutException()); handler.close(); @@ -323,11 +307,11 @@ public class HttpAsyncRequestExecutor im return state; } - private HttpAsyncRequestExecutionHandler getHandler(final NHttpConnection conn) { - return (HttpAsyncRequestExecutionHandler) conn.getContext().getAttribute(HTTP_HANDLER); + private HttpAsyncClientExchangeHandler getHandler(final NHttpConnection conn) { + return (HttpAsyncClientExchangeHandler) conn.getContext().getAttribute(HTTP_HANDLER); } - private HttpAsyncRequestExecutionHandler ensureNotNull(final HttpAsyncRequestExecutionHandler handler) { + private HttpAsyncClientExchangeHandler ensureNotNull(final HttpAsyncClientExchangeHandler handler) { Asserts.notNull(handler, "HTTP exchange handler"); return handler; } @@ -340,7 +324,7 @@ public class HttpAsyncRequestExecutor im } } - private void closeHandler(final HttpAsyncRequestExecutionHandler handler) { + private void closeHandler(final HttpAsyncClientExchangeHandler handler) { if (handler != null) { try { handler.close(); @@ -353,23 +337,21 @@ public class HttpAsyncRequestExecutor im private void processResponse( final NHttpClientConnection conn, final State state, - final HttpAsyncRequestExecutionHandler handler) throws IOException { - final HttpContext context = handler.getContext(); + final HttpAsyncClientExchangeHandler handler) throws IOException { if (state.isValid()) { final HttpRequest request = state.getRequest(); final HttpResponse response = state.getResponse(); final String method = request.getRequestLine().getMethod(); final int status = response.getStatusLine().getStatusCode(); if (!(method.equalsIgnoreCase("CONNECT") && status < 300)) { - final ConnectionReuseStrategy connReuseStrategy = handler.getConnectionReuseStrategy(); - if (!connReuseStrategy.keepAlive(response, context)) { + if (!handler.keepAlive(response)) { conn.close(); } } } else { conn.close(); } - handler.responseCompleted(context); + handler.responseCompleted(); state.reset(); } Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java?rev=1439908&r1=1439907&r2=1439908&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java Tue Jan 29 14:34:57 2013 @@ -61,7 +61,7 @@ import org.apache.http.util.Args; public class HttpAsyncRequester { private final HttpProcessor httppocessor; - private final ConnectionReuseStrategy reuseStrategy; + private final ConnectionReuseStrategy connReuseStrategy; /** * @deprecated (4.3) use {@link HttpAsyncRequester#HttpAsyncRequester(HttpProcessor, @@ -78,26 +78,20 @@ public class HttpAsyncRequester { /** * Creates new instance of HttpAsyncRequester. * - * @param httppocessor the HTTP protocol processor. - * @param reuseStrategy the connection re-use strategy. If null - * {@link DefaultConnectionReuseStrategy#INSTANCE} will be used. - * * @since 4.3 */ public HttpAsyncRequester( final HttpProcessor httppocessor, - final ConnectionReuseStrategy reuseStrategy) { + final ConnectionReuseStrategy connReuseStrategy) { super(); - this.httppocessor = httppocessor; - this.reuseStrategy = reuseStrategy != null ? reuseStrategy : + this.httppocessor = Args.notNull(httppocessor, "HTTP processor"); + this.connReuseStrategy = connReuseStrategy != null ? connReuseStrategy : DefaultConnectionReuseStrategy.INSTANCE; } /** * Creates new instance of HttpAsyncRequester. * - * @param httppocessor the HTTP protocol processor. - * * @since 4.3 */ public HttpAsyncRequester(final HttpProcessor httppocessor) { @@ -125,15 +119,15 @@ public class HttpAsyncRequester { Args.notNull(responseConsumer, "HTTP response consumer"); Args.notNull(conn, "HTTP connection"); Args.notNull(context, "HTTP context"); - final BasicAsyncRequestExecutionHandler handler = new BasicAsyncRequestExecutionHandler( - requestProducer, responseConsumer, callback, context, - this.httppocessor, this.reuseStrategy); + final BasicAsyncClientExchangeHandler handler = new BasicAsyncClientExchangeHandler( + requestProducer, responseConsumer, callback, context, conn, + this.httppocessor, this.connReuseStrategy); initExection(handler, conn); return handler.getFuture(); } - private void initExection( - final HttpAsyncRequestExecutionHandler handler, final NHttpClientConnection conn) { + private void initExection( + final HttpAsyncClientExchangeHandler handler, final NHttpClientConnection conn) { conn.getContext().setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, handler); if (!conn.isOpen()) { handler.failed(new ConnectionClosedException("Connection closed")); @@ -241,11 +235,11 @@ public class HttpAsyncRequester { Args.notNull(context, "HTTP context"); final BasicFuture future = new BasicFuture(callback); final NHttpClientConnection conn = poolEntry.getConnection(); - final BasicAsyncRequestExecutionHandler handler = new BasicAsyncRequestExecutionHandler( + final BasicAsyncClientExchangeHandler handler = new BasicAsyncClientExchangeHandler( requestProducer, responseConsumer, new RequestExecutionCallback(future, poolEntry, connPool), - context, - this.httppocessor, this.reuseStrategy); + context, conn, + this.httppocessor, this.connReuseStrategy); initExection(handler, conn); return future; } @@ -314,10 +308,10 @@ public class HttpAsyncRequester { return; } final NHttpClientConnection conn = result.getConnection(); - final BasicAsyncRequestExecutionHandler handler = new BasicAsyncRequestExecutionHandler( + final BasicAsyncClientExchangeHandler handler = new BasicAsyncClientExchangeHandler( this.requestProducer, this.responseConsumer, new RequestExecutionCallback(this.requestFuture, result, this.connPool), - this.context, httppocessor, reuseStrategy); + this.context, conn, httppocessor, connReuseStrategy); initExection(handler, conn); } Copied: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java (from r1439907, httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestExecutionHandler.java) URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java?p2=httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java&p1=httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestExecutionHandler.java&r1=1439907&r2=1439908&rev=1439908&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestExecutionHandler.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java Tue Jan 29 14:34:57 2013 @@ -30,7 +30,6 @@ package org.apache.http.nio.protocol; import java.util.concurrent.ExecutionException; import org.apache.http.ConnectionReuseStrategy; -import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpVersion; import org.apache.http.message.BasicHttpRequest; @@ -39,6 +38,7 @@ import org.apache.http.nio.ContentDecode import org.apache.http.nio.ContentEncoder; import org.apache.http.nio.NHttpClientConnection; import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpProcessor; import org.junit.After; @@ -47,7 +47,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; -public class TestBasicAsyncRequestExecutionHandler { +public class TestBasicAsyncClientExchangeHandler { private HttpAsyncRequestProducer requestProducer; private HttpAsyncResponseConsumer responseConsumer; @@ -55,7 +55,7 @@ public class TestBasicAsyncRequestExecut private HttpProcessor httpProcessor; private NHttpClientConnection conn; private ConnectionReuseStrategy reuseStrategy; - private BasicAsyncRequestExecutionHandler exchangeHandler; + private BasicAsyncClientExchangeHandler exchangeHandler; private ContentEncoder encoder; private ContentDecoder decoder; @@ -65,13 +65,15 @@ public class TestBasicAsyncRequestExecut this.requestProducer = Mockito.mock(HttpAsyncRequestProducer.class); this.responseConsumer = Mockito.mock(HttpAsyncResponseConsumer.class); this.context = new BasicHttpContext(); + this.conn = Mockito.mock(NHttpClientConnection.class); this.httpProcessor = Mockito.mock(HttpProcessor.class); this.reuseStrategy = Mockito.mock(ConnectionReuseStrategy.class); - this.exchangeHandler = new BasicAsyncRequestExecutionHandler( + this.exchangeHandler = new BasicAsyncClientExchangeHandler( this.requestProducer, this.responseConsumer, null, this.context, + this.conn, this.httpProcessor, this.reuseStrategy); this.encoder = Mockito.mock(ContentEncoder.class); @@ -85,45 +87,61 @@ public class TestBasicAsyncRequestExecut @Test public void testInvalidExecution() throws Exception { try { - new BasicAsyncRequestExecutionHandler( + new BasicAsyncClientExchangeHandler( null, this.responseConsumer, null, this.context, + this.conn, this.httpProcessor, this.reuseStrategy); Assert.fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException ex) { } try { - new BasicAsyncRequestExecutionHandler( + new BasicAsyncClientExchangeHandler( this.requestProducer, null, null, this.context, + this.conn, this.httpProcessor, this.reuseStrategy); Assert.fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException ex) { } try { - new BasicAsyncRequestExecutionHandler( + new BasicAsyncClientExchangeHandler( this.requestProducer, this.responseConsumer, null, null, + this.conn, this.httpProcessor, this.reuseStrategy); Assert.fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException ex) { } try { - new BasicAsyncRequestExecutionHandler( + new BasicAsyncClientExchangeHandler( this.requestProducer, this.responseConsumer, null, this.context, null, + this.httpProcessor, + this.reuseStrategy); + Assert.fail("IllegalArgumentException expected"); + } catch (final IllegalArgumentException ex) { + } + try { + new BasicAsyncClientExchangeHandler( + this.requestProducer, + this.responseConsumer, + null, + this.context, + this.conn, + null, this.reuseStrategy); Assert.fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException ex) { @@ -140,13 +158,6 @@ public class TestBasicAsyncRequestExecut } @Test - public void testGetTarget() throws Exception { - final HttpHost target = new HttpHost("somehost"); - Mockito.when(this.requestProducer.getTarget()).thenReturn(target); - Assert.assertSame(target, this.exchangeHandler.getTarget()); - } - - @Test public void testGenerateRequest() throws Exception { final BasicHttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1); Mockito.when(this.requestProducer.generateRequest()).thenReturn(request); @@ -156,6 +167,9 @@ public class TestBasicAsyncRequestExecut Assert.assertSame(request, result); Mockito.verify(this.requestProducer).generateRequest(); + Assert.assertSame(request, this.context.getAttribute(ExecutionContext.HTTP_REQUEST)); + Assert.assertSame(this.conn, this.context.getAttribute(ExecutionContext.HTTP_CONNECTION)); + Mockito.verify(this.httpProcessor).process(request, this.context); } @Test @@ -178,7 +192,7 @@ public class TestBasicAsyncRequestExecut @Test public void testRequestCompleted() throws Exception { - this.exchangeHandler.requestCompleted(this.context); + this.exchangeHandler.requestCompleted(); Mockito.verify(this.requestProducer).requestCompleted(this.context); } @@ -190,6 +204,8 @@ public class TestBasicAsyncRequestExecut this.exchangeHandler.responseReceived(response); Mockito.verify(this.responseConsumer).responseReceived(response); + Assert.assertSame(response, this.context.getAttribute(ExecutionContext.HTTP_RESPONSE)); + Mockito.verify(this.httpProcessor).process(response, this.context); } @Test @@ -218,7 +234,7 @@ public class TestBasicAsyncRequestExecut @Test public void testFailedAfterRequest() throws Exception { final Exception ooopsie = new Exception(); - this.exchangeHandler.requestCompleted(this.context); + this.exchangeHandler.requestCompleted(); this.exchangeHandler.failed(ooopsie); Mockito.verify(this.requestProducer, Mockito.never()).failed(ooopsie); @@ -282,7 +298,7 @@ public class TestBasicAsyncRequestExecut final Object obj = new Object(); Mockito.when(this.responseConsumer.getResult()).thenReturn(obj); - this.exchangeHandler.responseCompleted(this.context); + this.exchangeHandler.responseCompleted(); Mockito.verify(this.responseConsumer).responseCompleted(this.context); Mockito.verify(this.requestProducer).close(); @@ -296,7 +312,7 @@ public class TestBasicAsyncRequestExecut final Exception ooopsie = new Exception(); Mockito.when(this.responseConsumer.getException()).thenReturn(ooopsie); - this.exchangeHandler.responseCompleted(this.context); + this.exchangeHandler.responseCompleted(); Mockito.verify(this.responseConsumer).responseCompleted(this.context); Mockito.verify(this.requestProducer).close(); @@ -312,7 +328,7 @@ public class TestBasicAsyncRequestExecut public void testResponseCompletedWithException() throws Exception { Mockito.doThrow(new RuntimeException()).when(this.responseConsumer).responseCompleted(this.context); try { - this.exchangeHandler.responseCompleted(this.context); + this.exchangeHandler.responseCompleted(); Assert.fail("RuntimeException expected"); } catch (final RuntimeException ex) { Mockito.verify(this.requestProducer).close(); @@ -326,27 +342,16 @@ public class TestBasicAsyncRequestExecut } @Test - public void testMisc() throws Exception { - Assert.assertFalse(this.exchangeHandler.isRepeatable()); - final Object obj = new Object(); - Mockito.when(this.responseConsumer.getResult()).thenReturn(obj); - - final Object result = this.exchangeHandler.getResult(); - Assert.assertSame(obj, result); - Mockito.verify(this.responseConsumer).getResult(); - - final Exception ooopsie = new Exception(); - Mockito.when(this.responseConsumer.getException()).thenReturn(ooopsie); - - final Exception ex = this.exchangeHandler.getException(); - Assert.assertSame(ooopsie, ex); - Mockito.verify(this.responseConsumer).getException(); + public void testKeepAlive() throws Exception { + final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); + this.exchangeHandler.keepAlive(response); + Mockito.verify(this.reuseStrategy).keepAlive(response, this.context); + } + @Test + public void testIsDone() throws Exception { this.exchangeHandler.isDone(); Mockito.verify(this.responseConsumer).isDone(); - - Assert.assertSame(this.context, this.exchangeHandler.getContext()); - Assert.assertSame(this.reuseStrategy, this.exchangeHandler.getConnectionReuseStrategy()); } } Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java?rev=1439908&r1=1439907&r2=1439908&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java Tue Jan 29 14:34:57 2013 @@ -31,7 +31,6 @@ import java.io.IOException; import java.net.SocketTimeoutException; import org.apache.http.ConnectionClosedException; -import org.apache.http.ConnectionReuseStrategy; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpStatus; @@ -47,10 +46,8 @@ import org.apache.http.nio.NHttpConnecti import org.apache.http.nio.entity.NStringEntity; import org.apache.http.nio.protocol.HttpAsyncRequestExecutor.State; import org.apache.http.protocol.BasicHttpContext; -import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; -import org.apache.http.protocol.HttpProcessor; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -62,29 +59,19 @@ public class TestHttpAsyncRequestExecuto private HttpAsyncRequestExecutor protocolHandler; private HttpContext connContext; private NHttpClientConnection conn; - private HttpAsyncRequestExecutionHandler exchangeHandler; - private HttpContext exchangeContext; + private HttpAsyncClientExchangeHandler exchangeHandler; private ContentEncoder encoder; private ContentDecoder decoder; - private HttpProcessor httpprocessor; - private ConnectionReuseStrategy reuseStrategy; @Before public void setUp() throws Exception { this.protocolHandler = new HttpAsyncRequestExecutor(); this.connContext = new BasicHttpContext(); this.conn = Mockito.mock(NHttpClientConnection.class); - this.exchangeHandler = Mockito.mock(HttpAsyncRequestExecutionHandler.class); - this.exchangeContext = new BasicHttpContext(); + this.exchangeHandler = Mockito.mock(HttpAsyncClientExchangeHandler.class); this.encoder = Mockito.mock(ContentEncoder.class); this.decoder = Mockito.mock(ContentDecoder.class); - this.httpprocessor = Mockito.mock(HttpProcessor.class); - this.reuseStrategy = Mockito.mock(ConnectionReuseStrategy.class); - Mockito.when(this.conn.getContext()).thenReturn(this.connContext); - Mockito.when(this.exchangeHandler.getContext()).thenReturn(this.exchangeContext); - Mockito.when(this.exchangeHandler.getHttpProcessor()).thenReturn(this.httpprocessor); - Mockito.when(this.exchangeHandler.getConnectionReuseStrategy()).thenReturn(this.reuseStrategy); } @After @@ -105,7 +92,7 @@ public class TestHttpAsyncRequestExecuto Mockito.verify(this.exchangeHandler).generateRequest(); Assert.assertSame(request, state.getRequest()); Mockito.verify(this.conn).submitRequest(request); - Mockito.verify(this.exchangeHandler).requestCompleted(this.exchangeContext); + Mockito.verify(this.exchangeHandler).requestCompleted(); Assert.assertEquals(MessageState.COMPLETED, state.getRequestState()); Assert.assertEquals("request state: COMPLETED; request: GET / HTTP/1.1; " + "response state: READY; response: ; valid: true;", state.toString()); @@ -178,13 +165,8 @@ public class TestHttpAsyncRequestExecuto Mockito.verify(this.exchangeHandler).generateRequest(); Assert.assertSame(request, state.getRequest()); - Assert.assertSame(request, this.exchangeContext.getAttribute(ExecutionContext.HTTP_REQUEST)); - Assert.assertSame(this.conn, this.exchangeContext.getAttribute(ExecutionContext.HTTP_CONNECTION)); - Mockito.verify(this.httpprocessor).process(request, this.exchangeContext); - - Mockito.verify(this.conn).submitRequest(request); - Mockito.verify(this.exchangeHandler).requestCompleted(this.exchangeContext); + Mockito.verify(this.exchangeHandler).requestCompleted(); Assert.assertEquals(MessageState.COMPLETED, state.getRequestState()); } @@ -202,7 +184,7 @@ public class TestHttpAsyncRequestExecuto Mockito.verify(this.exchangeHandler).generateRequest(); Assert.assertSame(request, state.getRequest()); Mockito.verify(this.conn).submitRequest(request); - Mockito.verify(this.exchangeHandler, Mockito.never()).requestCompleted(this.exchangeContext); + Mockito.verify(this.exchangeHandler, Mockito.never()).requestCompleted(); Assert.assertEquals(MessageState.BODY_STREAM, state.getRequestState()); } @@ -223,7 +205,7 @@ public class TestHttpAsyncRequestExecuto Mockito.verify(this.conn).submitRequest(request); Mockito.verify(this.conn).setSocketTimeout(3000); Assert.assertEquals(1000, state.getTimeout()); - Mockito.verify(this.exchangeHandler, Mockito.never()).requestCompleted(this.exchangeContext); + Mockito.verify(this.exchangeHandler, Mockito.never()).requestCompleted(); Assert.assertEquals(MessageState.ACK_EXPECTED, state.getRequestState()); } @@ -250,7 +232,7 @@ public class TestHttpAsyncRequestExecuto this.protocolHandler.outputReady(this.conn, this.encoder); Mockito.verify(this.exchangeHandler).produceContent(this.encoder, this.conn); - Mockito.verify(this.exchangeHandler).requestCompleted(this.exchangeContext); + Mockito.verify(this.exchangeHandler).requestCompleted(); Assert.assertEquals(MessageState.COMPLETED, state.getRequestState()); } @@ -282,8 +264,6 @@ public class TestHttpAsyncRequestExecuto Assert.assertSame(response, state.getResponse()); Assert.assertEquals(MessageState.BODY_STREAM, state.getResponseState()); - Assert.assertSame(response, this.exchangeContext.getAttribute(ExecutionContext.HTTP_RESPONSE)); - Mockito.verify(this.httpprocessor).process(response, this.exchangeContext); } @Test @@ -397,7 +377,7 @@ public class TestHttpAsyncRequestExecuto this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler); final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); Mockito.when(this.conn.getHttpResponse()).thenReturn(response); - Mockito.when(this.reuseStrategy.keepAlive(response, this.exchangeContext)).thenReturn(Boolean.TRUE); + Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.TRUE); this.protocolHandler.responseReceived(this.conn); @@ -419,7 +399,7 @@ public class TestHttpAsyncRequestExecuto this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler); final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); Mockito.when(this.conn.getHttpResponse()).thenReturn(response); - Mockito.when(this.reuseStrategy.keepAlive(response, this.exchangeContext)).thenReturn(Boolean.TRUE); + Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.TRUE); this.protocolHandler.responseReceived(this.conn); @@ -443,7 +423,7 @@ public class TestHttpAsyncRequestExecuto HttpStatus.SC_NOT_MODIFIED, "Not modified"); response.setEntity(new BasicHttpEntity()); Mockito.when(this.conn.getHttpResponse()).thenReturn(response); - Mockito.when(this.reuseStrategy.keepAlive(response, this.exchangeContext)).thenReturn(Boolean.TRUE); + Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.TRUE); this.protocolHandler.responseReceived(this.conn); @@ -479,7 +459,7 @@ public class TestHttpAsyncRequestExecuto state.setResponse(response); this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_EXCHANGE_STATE, state); this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler); - Mockito.when(this.reuseStrategy.keepAlive(response, this.exchangeContext)).thenReturn(Boolean.TRUE); + Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.TRUE); Mockito.when(this.decoder.isCompleted()).thenReturn(Boolean.TRUE); this.protocolHandler.inputReady(this.conn, this.decoder); @@ -487,7 +467,7 @@ public class TestHttpAsyncRequestExecuto Assert.assertEquals(MessageState.READY, state.getRequestState()); Assert.assertEquals(MessageState.READY, state.getResponseState()); Mockito.verify(this.exchangeHandler).consumeContent(this.decoder, this.conn); - Mockito.verify(this.exchangeHandler).responseCompleted(this.exchangeContext); + Mockito.verify(this.exchangeHandler).responseCompleted(); Mockito.verify(this.conn, Mockito.never()).close(); } @@ -509,7 +489,6 @@ public class TestHttpAsyncRequestExecuto Assert.assertEquals(MessageState.READY, state.getResponseState()); Mockito.verify(this.exchangeHandler).consumeContent(this.decoder, this.conn); Mockito.verify(this.conn).close(); - Mockito.verify(this.exchangeHandler, Mockito.never()).getConnectionReuseStrategy(); } @Test @@ -521,7 +500,7 @@ public class TestHttpAsyncRequestExecuto state.setResponse(response); this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_EXCHANGE_STATE, state); this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler); - Mockito.when(this.reuseStrategy.keepAlive(response, this.exchangeContext)).thenReturn(Boolean.FALSE); + Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.FALSE); Mockito.when(this.decoder.isCompleted()).thenReturn(Boolean.TRUE); this.protocolHandler.inputReady(this.conn, this.decoder); @@ -529,7 +508,7 @@ public class TestHttpAsyncRequestExecuto Assert.assertEquals(MessageState.READY, state.getRequestState()); Assert.assertEquals(MessageState.READY, state.getResponseState()); Mockito.verify(this.exchangeHandler).consumeContent(this.decoder, this.conn); - Mockito.verify(this.exchangeHandler).responseCompleted(this.exchangeContext); + Mockito.verify(this.exchangeHandler).responseCompleted(); Mockito.verify(this.conn).close(); } Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequester.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequester.java?rev=1439908&r1=1439907&r2=1439908&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequester.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequester.java Tue Jan 29 14:34:57 2013 @@ -280,14 +280,14 @@ public class TestHttpAsyncRequester { final BasicNIOPoolEntry entry = new BasicNIOPoolEntry("id", host, this.conn); connRequestCallback.completed(entry); - final BasicAsyncRequestExecutionHandler exchangeHandler = (BasicAsyncRequestExecutionHandler) this.connContext.getAttribute( + final BasicAsyncClientExchangeHandler exchangeHandler = (BasicAsyncClientExchangeHandler) this.connContext.getAttribute( HttpAsyncRequestExecutor.HTTP_HANDLER); Assert.assertNotNull(exchangeHandler); Mockito.verify(this.conn).requestOutput(); final Object result = new Object(); Mockito.when(this.responseConsumer.getResult()).thenReturn(result); - exchangeHandler.responseCompleted(this.exchangeContext); + exchangeHandler.responseCompleted(); Mockito.verify(this.callback).completed(result); Mockito.verify(this.responseConsumer).close(); Mockito.verify(this.requestProducer).close(); @@ -313,7 +313,7 @@ public class TestHttpAsyncRequester { final BasicNIOPoolEntry entry = new BasicNIOPoolEntry("id", host, this.conn); connRequestCallback.completed(entry); - final BasicAsyncRequestExecutionHandler exchangeHandler = (BasicAsyncRequestExecutionHandler) this.connContext.getAttribute( + final BasicAsyncClientExchangeHandler exchangeHandler = (BasicAsyncClientExchangeHandler) this.connContext.getAttribute( HttpAsyncRequestExecutor.HTTP_HANDLER); Assert.assertNotNull(exchangeHandler); Mockito.verify(this.conn).requestOutput(); @@ -346,7 +346,7 @@ public class TestHttpAsyncRequester { final BasicNIOPoolEntry entry = new BasicNIOPoolEntry("id", host, this.conn); connRequestCallback.completed(entry); - final BasicAsyncRequestExecutionHandler exchangeHandler = (BasicAsyncRequestExecutionHandler) this.connContext.getAttribute( + final BasicAsyncClientExchangeHandler exchangeHandler = (BasicAsyncClientExchangeHandler) this.connContext.getAttribute( HttpAsyncRequestExecutor.HTTP_HANDLER); Assert.assertNotNull(exchangeHandler); Mockito.verify(this.conn).requestOutput();