You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/09/22 22:03:37 UTC
svn commit: r1174357 [1/2] - in /httpcomponents/httpasyncclient/trunk: ./
httpasyncclient/src/examples/org/apache/http/examples/nio/client/
httpasyncclient/src/main/java/org/apache/http/impl/nio/client/
httpasyncclient/src/main/java/org/apache/http/imp...
Author: olegk
Date: Thu Sep 22 20:03:35 2011
New Revision: 1174357
URL: http://svn.apache.org/viewvc?rev=1174357&view=rev
Log:
Merged protocol handler refactoring branch
Added:
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/LoggingClientProtocolHandler.java
- copied unchanged from r1174316, httpcomponents/httpasyncclient/branches/protocol-handler-refactoring/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/LoggingClientProtocolHandler.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpNIOConnPoolFactory.java
- copied unchanged from r1174316, httpcomponents/httpasyncclient/branches/protocol-handler-refactoring/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpNIOConnPoolFactory.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/HttpAsyncTestBase.java
- copied unchanged from r1174316, httpcomponents/httpasyncclient/branches/protocol-handler-refactoring/httpasyncclient/src/test/java/org/apache/http/HttpAsyncTestBase.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/SSLTestContexts.java
- copied unchanged from r1174316, httpcomponents/httpasyncclient/branches/protocol-handler-refactoring/httpasyncclient/src/test/java/org/apache/http/SSLTestContexts.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpsRedirects.java
- copied unchanged from r1174316, httpcomponents/httpasyncclient/branches/protocol-handler-refactoring/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpsRedirects.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestStatefulConnManagement.java
- copied unchanged from r1174316, httpcomponents/httpasyncclient/branches/protocol-handler-refactoring/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestStatefulConnManagement.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/localserver/HttpServerNio.java
- copied unchanged from r1174316, httpcomponents/httpasyncclient/branches/protocol-handler-refactoring/httpasyncclient/src/test/java/org/apache/http/localserver/HttpServerNio.java
Removed:
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/HttpExchange.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalClientEventDispatch.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MessageState.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/HttpAsyncExchangeHandler.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/HttpAsyncRequestProducer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/HttpAsyncResponseConsumer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AbstractHttpAsyncResponseConsumer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BasicHttpAsyncResponseConsumer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncRequestProducerImpl.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/localserver/AsyncHttpTestBase.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/localserver/LocalTestServer.java
Modified:
httpcomponents/httpasyncclient/trunk/ (props changed)
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultResultCallback.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/ResultCallback.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpNIOConnPool.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/ManagedClientConnectionImpl.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncEntityRequestProducer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncRequestProducer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/OperatedClientConnection.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpsAsync.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestRedirects.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/localserver/ (props changed)
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/localserver/EchoHandler.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/localserver/RandomHandler.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestAsyncConsumers.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java
httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/resources/test.keystore (props changed)
httpcomponents/httpasyncclient/trunk/pom.xml
Propchange: httpcomponents/httpasyncclient/trunk/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Sep 22 20:03:35 2011
@@ -1 +1,2 @@
/httpcomponents/httpasyncclient/branches/conn-mgmt-redesign:1155312-1159070
+/httpcomponents/httpasyncclient/branches/protocol-handler-refactoring:1167093-1174316
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java Thu Sep 22 20:03:35 2011
@@ -73,7 +73,7 @@ public class AsyncClientHttpExchangeStre
}
@Override
- protected void onCleanup() {
+ protected void releaseResources() {
}
@Override
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java Thu Sep 22 20:03:35 2011
@@ -46,6 +46,7 @@ import org.apache.http.client.ClientProt
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.RedirectStrategy;
+import org.apache.http.client.UserTokenHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.AuthPolicy;
import org.apache.http.client.params.CookiePolicy;
@@ -67,22 +68,24 @@ import org.apache.http.impl.client.Defau
import org.apache.http.impl.client.DefaultProxyAuthenticationHandler;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.DefaultTargetAuthenticationHandler;
+import org.apache.http.impl.client.DefaultUserTokenHandler;
import org.apache.http.impl.cookie.BestMatchSpecFactory;
import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
import org.apache.http.impl.cookie.IgnoreSpecFactory;
import org.apache.http.impl.cookie.NetscapeDraftSpecFactory;
import org.apache.http.impl.cookie.RFC2109SpecFactory;
import org.apache.http.impl.cookie.RFC2965SpecFactory;
+import org.apache.http.impl.nio.DefaultClientIODispatch;
import org.apache.http.impl.nio.conn.DefaultHttpAsyncRoutePlanner;
import org.apache.http.impl.nio.conn.PoolingClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.client.HttpAsyncClient;
-import org.apache.http.nio.client.HttpAsyncExchangeHandler;
-import org.apache.http.nio.client.HttpAsyncRequestProducer;
-import org.apache.http.nio.client.HttpAsyncResponseConsumer;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.nio.conn.ClientConnectionManager;
+import org.apache.http.nio.protocol.HttpAsyncClientExchangeHandler;
+import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
+import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
import org.apache.http.nio.reactor.IOEventDispatch;
import org.apache.http.nio.reactor.IOReactorException;
import org.apache.http.nio.reactor.IOReactorStatus;
@@ -98,7 +101,7 @@ public abstract class AbstractHttpAsyncC
private final Log log = LogFactory.getLog(getClass());;
private final ClientConnectionManager connmgr;
- private final Queue<HttpAsyncExchangeHandler<?>> queue;
+ private final Queue<HttpAsyncClientExchangeHandler<?>> queue;
private Thread reactorThread;
private BasicHttpProcessor mutableProcessor;
@@ -113,6 +116,7 @@ public abstract class AbstractHttpAsyncC
private AuthenticationHandler proxyAuthHandler;
private CredentialsProvider credsProvider;
private HttpRoutePlanner routePlanner;
+ private UserTokenHandler userTokenHandler;
private HttpParams params;
private volatile boolean terminated;
@@ -120,7 +124,7 @@ public abstract class AbstractHttpAsyncC
protected AbstractHttpAsyncClient(final ClientConnectionManager connmgr) {
super();
this.connmgr = connmgr;
- this.queue = new ConcurrentLinkedQueue<HttpAsyncExchangeHandler<?>>();
+ this.queue = new ConcurrentLinkedQueue<HttpAsyncClientExchangeHandler<?>>();
}
protected AbstractHttpAsyncClient(final IOReactorConfig config) throws IOReactorException {
@@ -128,7 +132,7 @@ public abstract class AbstractHttpAsyncC
DefaultConnectingIOReactor defaultioreactor = new DefaultConnectingIOReactor(config);
defaultioreactor.setExceptionHandler(new InternalIOReactorExceptionHandler(this.log));
this.connmgr = new PoolingClientConnectionManager(defaultioreactor);
- this.queue = new ConcurrentLinkedQueue<HttpAsyncExchangeHandler<?>>();
+ this.queue = new ConcurrentLinkedQueue<HttpAsyncClientExchangeHandler<?>>();
}
protected abstract HttpParams createHttpParams();
@@ -223,6 +227,10 @@ public abstract class AbstractHttpAsyncC
return new DefaultHttpAsyncRoutePlanner(getConnectionManager().getSchemeRegistry());
}
+ protected UserTokenHandler createUserTokenHandler() {
+ return new DefaultUserTokenHandler();
+ }
+
public synchronized final HttpParams getParams() {
if (this.params == null) {
this.params = createHttpParams();
@@ -350,6 +358,18 @@ public abstract class AbstractHttpAsyncC
this.routePlanner = routePlanner;
}
+ public synchronized final UserTokenHandler getUserTokenHandler() {
+ if (this.userTokenHandler == null) {
+ this.userTokenHandler = createUserTokenHandler();
+ }
+ return this.userTokenHandler;
+ }
+
+
+ public synchronized void setUserTokenHandler(final UserTokenHandler userTokenHandler) {
+ this.userTokenHandler = userTokenHandler;
+ }
+
protected synchronized final BasicHttpProcessor getHttpProcessor() {
if (this.mutableProcessor == null) {
this.mutableProcessor = createHttpProcessor();
@@ -434,17 +454,17 @@ public abstract class AbstractHttpAsyncC
}
private void doExecute() {
- NHttpClientProtocolHandler handler = new NHttpClientProtocolHandler();
+ LoggingClientProtocolHandler handler = new LoggingClientProtocolHandler();
try {
- IOEventDispatch ioEventDispatch = new InternalClientEventDispatch(handler);
+ IOEventDispatch ioEventDispatch = new DefaultClientIODispatch(handler, getParams());
this.connmgr.execute(ioEventDispatch);
} catch (Exception ex) {
this.log.error("I/O reactor terminated abnormally", ex);
} finally {
this.terminated = true;
while (!this.queue.isEmpty()) {
- HttpAsyncResponseConsumer<?> responseConsumer = this.queue.remove();
- responseConsumer.cancel();
+ HttpAsyncClientExchangeHandler<?> exchangeHandler = this.queue.remove();
+ exchangeHandler.cancel();
}
}
}
@@ -509,6 +529,7 @@ public abstract class AbstractHttpAsyncC
getRedirectStrategy(),
getTargetAuthenticationHandler(),
getProxyAuthenticationHandler(),
+ getUserTokenHandler(),
getParams());
}
this.queue.add(httpexchange);
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java Thu Sep 22 20:03:35 2011
@@ -57,6 +57,7 @@ import org.apache.http.client.Credential
import org.apache.http.client.NonRepeatableRequestException;
import org.apache.http.client.RedirectException;
import org.apache.http.client.RedirectStrategy;
+import org.apache.http.client.UserTokenHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.HttpClientParams;
@@ -76,12 +77,13 @@ import org.apache.http.message.BasicHttp
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.ContentEncoder;
import org.apache.http.nio.IOControl;
-import org.apache.http.nio.client.HttpAsyncExchangeHandler;
-import org.apache.http.nio.client.HttpAsyncRequestProducer;
-import org.apache.http.nio.client.HttpAsyncResponseConsumer;
import org.apache.http.nio.conn.ClientConnectionManager;
import org.apache.http.nio.conn.ManagedClientConnection;
import org.apache.http.nio.conn.scheme.Scheme;
+import org.apache.http.nio.protocol.HttpAsyncClientExchangeHandler;
+import org.apache.http.nio.protocol.HttpAsyncClientProtocolHandler;
+import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
+import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
@@ -89,9 +91,7 @@ import org.apache.http.protocol.Executio
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpProcessor;
-class DefaultAsyncRequestDirector<T> implements HttpAsyncExchangeHandler<T> {
-
- public static final String HTTP_EXCHANGE_HANDLER = "http.nio.async-exchange-handler";
+class DefaultAsyncRequestDirector<T> implements HttpAsyncClientExchangeHandler<T> {
private final Log log;
@@ -108,6 +108,7 @@ class DefaultAsyncRequestDirector<T> imp
private final RedirectStrategy redirectStrategy;
private final AuthenticationHandler targetAuthHandler;
private final AuthenticationHandler proxyAuthHandler;
+ private final UserTokenHandler userTokenHandler;
private final AuthState targetAuthState;
private final AuthState proxyAuthState;
private final HttpParams clientParams;
@@ -141,6 +142,7 @@ class DefaultAsyncRequestDirector<T> imp
final RedirectStrategy redirectStrategy,
final AuthenticationHandler targetAuthHandler,
final AuthenticationHandler proxyAuthHandler,
+ final UserTokenHandler userTokenHandler,
final HttpParams clientParams) {
super();
this.log = log;
@@ -157,11 +159,16 @@ class DefaultAsyncRequestDirector<T> imp
this.routeDirector = new BasicRouteDirector();
this.targetAuthHandler = targetAuthHandler;
this.proxyAuthHandler = proxyAuthHandler;
+ this.userTokenHandler = userTokenHandler;
this.targetAuthState = new AuthState();
this.proxyAuthState = new AuthState();
this.clientParams = clientParams;
}
+ public synchronized void close() throws IOException {
+ releaseResources();
+ }
+
public synchronized void start() {
try {
HttpHost target = this.requestProducer.getTarget();
@@ -262,6 +269,10 @@ class DefaultAsyncRequestDirector<T> imp
}
}
+ public void requestCompleted(final HttpContext context) {
+ this.requestProducer.requestCompleted(context);
+ }
+
public boolean isRepeatable() {
return this.requestProducer.isRepeatable();
}
@@ -297,6 +308,15 @@ class DefaultAsyncRequestDirector<T> imp
if (this.followup == null) {
this.finalResponse = response;
}
+
+ Object userToken = this.localContext.getAttribute(ClientContext.USER_TOKEN);
+ if (managedConn != null && userToken == null) {
+ userToken = userTokenHandler.getUserToken(this.localContext);
+ if (userToken != null) {
+ this.localContext.setAttribute(ClientContext.USER_TOKEN, userToken);
+ managedConn.setState(userToken);
+ }
+ }
}
if (this.finalResponse != null) {
this.responseConsumer.responseReceived(response);
@@ -319,7 +339,7 @@ class DefaultAsyncRequestDirector<T> imp
private void releaseConnection() {
if (this.managedConn != null) {
try {
- this.managedConn.getContext().removeAttribute(HTTP_EXCHANGE_HANDLER);
+ this.managedConn.getContext().removeAttribute(HttpAsyncClientProtocolHandler.HTTP_HANDLER);
this.managedConn.releaseConnection();
} catch (IOException ioex) {
this.log.debug("I/O error releasing connection", ioex);
@@ -356,11 +376,7 @@ class DefaultAsyncRequestDirector<T> imp
}
}
- public synchronized boolean keepAlive(final HttpResponse response) {
- return this.reuseStrategy.keepAlive(response, this.localContext);
- }
-
- public synchronized void responseCompleted() {
+ public synchronized void responseCompleted(final HttpContext context) {
this.log.debug("Response fully read");
try {
if (this.resultCallback.isDone()) {
@@ -380,15 +396,14 @@ class DefaultAsyncRequestDirector<T> imp
}
this.managedConn.setIdleDuration(duration, TimeUnit.MILLISECONDS);
} else {
- this.managedConn.unmarkReusable();
- releaseConnection();
invalidateAuthIfSuccessful(this.proxyAuthState);
invalidateAuthIfSuccessful(this.targetAuthState);
}
if (this.finalResponse != null) {
- this.responseConsumer.responseCompleted();
+ this.responseConsumer.responseCompleted(this.localContext);
this.log.debug("Response processed");
+ releaseConnection();
T result = this.responseConsumer.getResult();
Exception ex = this.responseConsumer.getException();
if (ex == null) {
@@ -396,7 +411,6 @@ class DefaultAsyncRequestDirector<T> imp
} else {
this.resultCallback.failed(ex, this);
}
- releaseConnection();
} else {
if (this.followup != null) {
HttpRoute actualRoute = this.mainRequest.getRoute();
@@ -406,6 +420,9 @@ class DefaultAsyncRequestDirector<T> imp
}
this.mainRequest = this.followup;
}
+ if (this.managedConn != null && !this.managedConn.isOpen()) {
+ releaseConnection();
+ }
if (this.managedConn != null) {
this.managedConn.requestOutput();
} else {
@@ -421,10 +438,10 @@ class DefaultAsyncRequestDirector<T> imp
}
}
- public synchronized void cancel() {
+ public synchronized boolean cancel() {
this.log.debug("HTTP exchange cancelled");
try {
- this.responseConsumer.cancel();
+ boolean cancelled = this.responseConsumer.cancel();
T result = this.responseConsumer.getResult();
Exception ex = this.responseConsumer.getException();
@@ -435,6 +452,7 @@ class DefaultAsyncRequestDirector<T> imp
} else {
this.resultCallback.cancelled(this);
}
+ return cancelled;
} catch (RuntimeException runex) {
this.resultCallback.failed(runex, this);
throw runex;
@@ -465,7 +483,7 @@ class DefaultAsyncRequestDirector<T> imp
conn.open(route, this.localContext, this.params);
}
this.managedConn = conn;
- this.managedConn.getContext().setAttribute(HTTP_EXCHANGE_HANDLER, this);
+ this.managedConn.getContext().setAttribute(HttpAsyncClientProtocolHandler.HTTP_HANDLER, this);
this.managedConn.requestOutput();
this.routeEstablished = route.equals(conn.getRoute());
} catch (IOException ex) {
@@ -832,4 +850,12 @@ class DefaultAsyncRequestDirector<T> imp
}
}
+ public HttpContext getContext() {
+ return this.localContext;
+ }
+
+ public ConnectionReuseStrategy getConnectionReuseStrategy() {
+ return this.reuseStrategy;
+ }
+
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultResultCallback.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultResultCallback.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultResultCallback.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultResultCallback.java Thu Sep 22 20:03:35 2011
@@ -29,31 +29,31 @@ package org.apache.http.impl.nio.client;
import java.util.Queue;
import org.apache.http.concurrent.BasicFuture;
-import org.apache.http.nio.client.HttpAsyncExchangeHandler;
+import org.apache.http.nio.protocol.HttpAsyncClientExchangeHandler;
class DefaultResultCallback<T> implements ResultCallback<T> {
private final BasicFuture<T> future;
- private final Queue<HttpAsyncExchangeHandler<?>> queue;
+ private final Queue<HttpAsyncClientExchangeHandler<?>> queue;
DefaultResultCallback(
- final BasicFuture<T> future, final Queue<HttpAsyncExchangeHandler<?>> queue) {
+ final BasicFuture<T> future, final Queue<HttpAsyncClientExchangeHandler<?>> queue) {
super();
this.future = future;
this.queue = queue;
}
- public void completed(final T result, final HttpAsyncExchangeHandler<T> handler) {
+ public void completed(final T result, final HttpAsyncClientExchangeHandler<T> handler) {
this.future.completed(result);
this.queue.remove(handler);
}
- public void failed(final Exception ex, final HttpAsyncExchangeHandler<T> handler) {
+ public void failed(final Exception ex, final HttpAsyncClientExchangeHandler<T> handler) {
this.future.failed(ex);
this.queue.remove(handler);
}
- public void cancelled(final HttpAsyncExchangeHandler<T> handler) {
+ public void cancelled(final HttpAsyncClientExchangeHandler<T> handler) {
this.future.cancel(true);
this.queue.remove(handler);
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/ResultCallback.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/ResultCallback.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/ResultCallback.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/ResultCallback.java Thu Sep 22 20:03:35 2011
@@ -26,15 +26,15 @@
*/
package org.apache.http.impl.nio.client;
-import org.apache.http.nio.client.HttpAsyncExchangeHandler;
+import org.apache.http.nio.protocol.HttpAsyncClientExchangeHandler;
interface ResultCallback<T> {
- void completed(T result, HttpAsyncExchangeHandler<T> handler);
+ void completed(T result, HttpAsyncClientExchangeHandler<T> handler);
- void failed(Exception ex, HttpAsyncExchangeHandler<T> handler);
+ void failed(Exception ex, HttpAsyncClientExchangeHandler<T> handler);
- void cancelled(HttpAsyncExchangeHandler<T> handler);
+ void cancelled(HttpAsyncClientExchangeHandler<T> handler);
boolean isDone();
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java Thu Sep 22 20:03:35 2011
@@ -37,7 +37,6 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseFactory;
import org.apache.http.impl.nio.DefaultNHttpClientConnection;
-import org.apache.http.impl.nio.reactor.SSLIOSession;
import org.apache.http.nio.NHttpMessageParser;
import org.apache.http.nio.NHttpMessageWriter;
import org.apache.http.nio.conn.OperatedClientConnection;
@@ -55,7 +54,6 @@ public class DefaultClientConnection
private final Log log;
private String id;
- private SSLIOSession ssliosession;
public DefaultClientConnection(
final String id,
@@ -69,11 +67,6 @@ public class DefaultClientConnection
if (this.log.isDebugEnabled() || this.wirelog.isDebugEnabled()) {
this.session = new LoggingIOSession(iosession, this.id, this.log, this.wirelog);
}
- if (iosession instanceof SSLIOSession) {
- this.ssliosession = (SSLIOSession) iosession;
- } else {
- this.ssliosession = null;
- }
}
public void upgrade(final IOSession iosession) {
@@ -85,15 +78,10 @@ public class DefaultClientConnection
this.session = iosession;
}
this.session.setBufferStatus(this);
- if (iosession instanceof SSLIOSession) {
- this.ssliosession = (SSLIOSession) iosession;
- } else {
- this.ssliosession = null;
- }
}
- public SSLIOSession getSSLIOSession() {
- return this.ssliosession;
+ public IOSession getIOSession() {
+ return this.session;
}
@Override
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpNIOConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpNIOConnPool.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpNIOConnPool.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpNIOConnPool.java Thu Sep 22 20:03:35 2011
@@ -26,14 +26,12 @@
*/
package org.apache.http.impl.nio.conn;
-import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.logging.Log;
-import org.apache.http.HttpConnection;
import org.apache.http.HttpHost;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.nio.conn.scheme.Scheme;
@@ -41,7 +39,6 @@ import org.apache.http.nio.conn.scheme.S
import org.apache.http.nio.pool.AbstractNIOConnPool;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.nio.reactor.IOSession;
-import org.apache.http.protocol.ExecutionContext;
class HttpNIOConnPool extends AbstractNIOConnPool<HttpRoute, IOSession, HttpPoolEntry> {
@@ -57,7 +54,7 @@ class HttpNIOConnPool extends AbstractNI
final ConnectingIOReactor ioreactor,
final SchemeRegistry schemeRegistry,
long connTimeToLive, final TimeUnit tunit) {
- super(ioreactor, 2, 20);
+ super(ioreactor, new HttpNIOConnPoolFactory(), 2, 20);
this.log = log;
this.schemeRegistry = schemeRegistry;
this.connTimeToLive = connTimeToLive;
@@ -90,23 +87,4 @@ class HttpNIOConnPool extends AbstractNI
return new HttpPoolEntry(this.log, id, route, session, this.connTimeToLive, this.tunit);
}
- @Override
- protected void closeEntry(final HttpPoolEntry entry) {
- IOSession session = entry.getConnection();
- HttpConnection conn = (HttpConnection) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- try {
- conn.shutdown();
- } catch (IOException ex) {
- if (this.log.isDebugEnabled()) {
- this.log.debug("I/O error shutting down connection", ex);
- }
- }
- }
-
- @Override
- protected IOSession createConnection(final HttpRoute route, final IOSession session) {
- return session;
- }
-
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java Thu Sep 22 20:03:35 2011
@@ -26,14 +26,17 @@
*/
package org.apache.http.impl.nio.conn;
+import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.RouteTracker;
+import org.apache.http.nio.conn.OperatedClientConnection;
import org.apache.http.nio.reactor.IOSession;
import org.apache.http.pool.PoolEntry;
+import org.apache.http.protocol.ExecutionContext;
class HttpPoolEntry extends PoolEntry<HttpRoute, IOSession> {
@@ -56,6 +59,29 @@ class HttpPoolEntry extends PoolEntry<Ht
return expired;
}
+ public OperatedClientConnection getOperatedClientConnection() {
+ IOSession session = getConnection();
+ return (OperatedClientConnection) session.getAttribute(
+ ExecutionContext.HTTP_CONNECTION);
+ }
+
+ @Override
+ public void close() {
+ try {
+ getOperatedClientConnection().shutdown();
+ } catch (IOException ex) {
+ if (this.log.isDebugEnabled()) {
+ this.log.debug("I/O error shutting down connection", ex);
+ }
+ }
+ }
+
+ @Override
+ public boolean isClosed() {
+ IOSession session = getConnection();
+ return session.isClosed();
+ }
+
HttpRoute getPlannedRoute() {
return super.getRoute();
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/ManagedClientConnectionImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/ManagedClientConnectionImpl.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/ManagedClientConnectionImpl.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/ManagedClientConnectionImpl.java Thu Sep 22 20:03:35 2011
@@ -42,14 +42,13 @@ import org.apache.http.conn.routing.Http
import org.apache.http.conn.routing.RouteTracker;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.conn.ConnectionShutdownException;
-import org.apache.http.impl.nio.reactor.SSLIOSession;
-import org.apache.http.impl.nio.reactor.SSLMode;
import org.apache.http.nio.conn.ClientConnectionManager;
import org.apache.http.nio.conn.ManagedClientConnection;
import org.apache.http.nio.conn.OperatedClientConnection;
import org.apache.http.nio.conn.scheme.LayeringStrategy;
import org.apache.http.nio.conn.scheme.Scheme;
import org.apache.http.nio.reactor.IOSession;
+import org.apache.http.nio.reactor.ssl.SSLIOSession;
import org.apache.http.nio.util.ByteBufferAllocator;
import org.apache.http.nio.util.HeapByteBufferAllocator;
import org.apache.http.params.HttpParams;
@@ -239,7 +238,7 @@ class ManagedClientConnectionImpl implem
public boolean isSecure() {
OperatedClientConnection conn = ensureConnection();
- return conn.getSSLIOSession() != null;
+ return conn.getIOSession() instanceof SSLIOSession;
}
public HttpRoute getRoute() {
@@ -249,8 +248,12 @@ class ManagedClientConnectionImpl implem
public SSLSession getSSLSession() {
OperatedClientConnection conn = ensureConnection();
- SSLIOSession iosession = conn.getSSLIOSession();
- return iosession != null ? iosession.getSSLSession() : null;
+ IOSession iosession = conn.getIOSession();
+ if (iosession instanceof SSLIOSession) {
+ return ((SSLIOSession) iosession).getSSLSession();
+ } else {
+ return null;
+ }
}
public Object getState() {
@@ -308,9 +311,7 @@ class ManagedClientConnectionImpl implem
Scheme scheme = this.manager.getSchemeRegistry().getScheme(target);
LayeringStrategy layeringStrategy = scheme.getLayeringStrategy();
if (layeringStrategy != null) {
- SSLIOSession ssliosession = (SSLIOSession) layeringStrategy.layer(iosession);
- ssliosession.bind(SSLMode.CLIENT, params);
- iosession = ssliosession;
+ iosession = layeringStrategy.layer(iosession);
}
}
@@ -323,7 +324,7 @@ class ManagedClientConnectionImpl implem
iosession.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
if (proxy == null) {
- tracker.connectTarget(conn.getSSLIOSession() != null);
+ tracker.connectTarget(conn.getIOSession() instanceof SSLIOSession);
} else {
tracker.connectProxy(proxy, false);
}
@@ -373,12 +374,9 @@ class ManagedClientConnectionImpl implem
" scheme does not provider support for protocol layering");
}
IOSession iosession = entry.getConnection();
- SSLIOSession ssliosession = (SSLIOSession) layeringStrategy.layer(iosession);
- ssliosession.bind(SSLMode.CLIENT, params);
-
OperatedClientConnection conn = (OperatedClientConnection) iosession.getAttribute(
ExecutionContext.HTTP_CONNECTION);
- conn.upgrade(ssliosession);
+ conn.upgrade((SSLIOSession) layeringStrategy.layer(iosession));
tracker.layerProtocol(layeringStrategy.isSecure());
}
@@ -406,4 +404,13 @@ class ManagedClientConnectionImpl implem
this.poolEntry = null;
}
+ @Override
+ public synchronized String toString() {
+ if (this.poolEntry != null) {
+ return this.poolEntry.toString();
+ } else {
+ return "released";
+ }
+ }
+
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java Thu Sep 22 20:03:35 2011
@@ -261,6 +261,18 @@ public class PoolingClientConnectionMana
this.pool.setMaxPerRoute(route, max);
}
+ public int getMaxTotal() {
+ return this.pool.getMaxTotal();
+ }
+
+ public int getDefaultMaxPerRoute() {
+ return this.pool.getDefaultMaxPerRoute();
+ }
+
+ public int getMaxPerRoute(final HttpRoute route) {
+ return this.pool.getMaxPerRoute(route);
+ }
+
public void closeIdleConnections(long idleTimeout, final TimeUnit tunit) {
if (log.isDebugEnabled()) {
log.debug("Closing connections idle longer than " + idleTimeout + " " + tunit);
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java Thu Sep 22 20:03:35 2011
@@ -34,6 +34,8 @@ import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.nio.conn.ClientConnectionManager;
+import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
+import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
import org.apache.http.nio.reactor.IOReactorStatus;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java Thu Sep 22 20:03:35 2011
@@ -31,8 +31,9 @@ import java.nio.ByteBuffer;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
+import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer;
-public abstract class AsyncByteConsumer<T> extends AbstractHttpAsyncResponseConsumer<T> {
+public abstract class AsyncByteConsumer<T> extends AbstractAsyncResponseConsumer<T> {
private final int bufSize;
private ByteBuffer bbuf;
@@ -67,9 +68,8 @@ public abstract class AsyncByteConsumer<
}
@Override
- void releaseResources() {
+ protected void releaseResources() {
this.bbuf = null;
- super.releaseResources();
}
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java Thu Sep 22 20:03:35 2011
@@ -41,9 +41,10 @@ import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
+import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer;
import org.apache.http.protocol.HTTP;
-public abstract class AsyncCharConsumer<T> extends AbstractHttpAsyncResponseConsumer<T> {
+public abstract class AsyncCharConsumer<T> extends AbstractAsyncResponseConsumer<T> {
private final int bufSize;
private ContentType contentType;
@@ -119,12 +120,11 @@ public abstract class AsyncCharConsumer<
}
@Override
- void releaseResources() {
+ protected void releaseResources() {
this.charset = null;
this.chardecoder = null;
this.bbuf = null;
this.cbuf = null;
- super.releaseResources();
}
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncEntityRequestProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncEntityRequestProducer.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncEntityRequestProducer.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncEntityRequestProducer.java Thu Sep 22 20:03:35 2011
@@ -26,7 +26,6 @@
*/
package org.apache.http.nio.client.methods;
-import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
@@ -39,14 +38,15 @@ import org.apache.http.HttpRequest;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.nio.ContentEncoder;
import org.apache.http.nio.IOControl;
-import org.apache.http.nio.client.HttpAsyncRequestProducer;
import org.apache.http.nio.entity.NByteArrayEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.entity.ProducingNHttpEntity;
+import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
import org.apache.http.protocol.HTTP;
+import org.apache.http.protocol.HttpContext;
@Deprecated
-abstract class BaseHttpAsyncEntityRequestProducer implements HttpAsyncRequestProducer, Closeable {
+abstract class BaseHttpAsyncEntityRequestProducer implements HttpAsyncRequestProducer {
private final URI requestURI;
private final ProducingNHttpEntity producer;
@@ -103,6 +103,9 @@ abstract class BaseHttpAsyncEntityReques
}
}
+ public void requestCompleted(final HttpContext context) {
+ }
+
public synchronized boolean isRepeatable() {
return this.producer.isRepeatable();
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncRequestProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncRequestProducer.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncRequestProducer.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseHttpAsyncRequestProducer.java Thu Sep 22 20:03:35 2011
@@ -35,7 +35,8 @@ import org.apache.http.HttpRequest;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.nio.ContentEncoder;
import org.apache.http.nio.IOControl;
-import org.apache.http.nio.client.HttpAsyncRequestProducer;
+import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
+import org.apache.http.protocol.HttpContext;
@Deprecated
abstract class BaseHttpAsyncRequestProducer implements HttpAsyncRequestProducer {
@@ -64,6 +65,9 @@ abstract class BaseHttpAsyncRequestProdu
final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
}
+ public void requestCompleted(final HttpContext context) {
+ }
+
public boolean isRepeatable() {
return true;
}
@@ -71,4 +75,7 @@ abstract class BaseHttpAsyncRequestProdu
public void resetRequest() {
}
+ public void close() throws IOException {
+ }
+
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java Thu Sep 22 20:03:35 2011
@@ -26,7 +26,6 @@
*/
package org.apache.http.nio.client.methods;
-import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -45,9 +44,10 @@ import org.apache.http.nio.ContentEncode
import org.apache.http.nio.ContentEncoderChannel;
import org.apache.http.nio.FileContentEncoder;
import org.apache.http.nio.IOControl;
-import org.apache.http.nio.client.HttpAsyncRequestProducer;
+import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
+import org.apache.http.protocol.HttpContext;
-abstract class BaseZeroCopyRequestProducer implements HttpAsyncRequestProducer, Closeable {
+abstract class BaseZeroCopyRequestProducer implements HttpAsyncRequestProducer {
private final URI requestURI;
private final File file;
@@ -112,6 +112,9 @@ abstract class BaseZeroCopyRequestProduc
}
}
+ public void requestCompleted(final HttpContext context) {
+ }
+
public synchronized boolean isRepeatable() {
return true;
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java Thu Sep 22 20:03:35 2011
@@ -30,6 +30,7 @@ import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URI;
+import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
@@ -42,10 +43,13 @@ import org.apache.http.client.methods.Ht
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.ContentType;
-import org.apache.http.nio.client.HttpAsyncRequestProducer;
-import org.apache.http.nio.client.HttpAsyncResponseConsumer;
import org.apache.http.nio.entity.NByteArrayEntity;
import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.nio.entity.ProducingNHttpEntity;
+import org.apache.http.nio.protocol.BasicAsyncRequestProducer;
+import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
+import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
+import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
public final class HttpAsyncMethods {
@@ -56,7 +60,7 @@ public final class HttpAsyncMethods {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
- return new HttpAsyncRequestProducerImpl(target, request);
+ return new RequestProducerImpl(target, request);
}
public static HttpAsyncRequestProducer create(final HttpUriRequest request) {
@@ -64,7 +68,7 @@ public final class HttpAsyncMethods {
throw new IllegalArgumentException("HTTP request may not be null");
}
HttpHost target = URIUtils.extractHost(request.getURI());
- return new HttpAsyncRequestProducerImpl(target, request);
+ return new RequestProducerImpl(target, request);
}
public static HttpAsyncRequestProducer createGet(final URI requestURI) {
@@ -115,7 +119,7 @@ public final class HttpAsyncMethods {
NStringEntity entity = new NStringEntity(content, contentType);
httppost.setEntity(entity);
HttpHost target = URIUtils.extractHost(requestURI);
- return new HttpAsyncRequestProducerImpl(target, httppost, entity);
+ return new RequestProducerImpl(target, httppost, entity);
}
public static HttpAsyncRequestProducer createPost(
@@ -132,7 +136,7 @@ public final class HttpAsyncMethods {
HttpPost httppost = new HttpPost(requestURI);
NByteArrayEntity entity = new NByteArrayEntity(content, contentType);
HttpHost target = URIUtils.extractHost(requestURI);
- return new HttpAsyncRequestProducerImpl(target, httppost, entity);
+ return new RequestProducerImpl(target, httppost, entity);
}
public static HttpAsyncRequestProducer createPost(
@@ -150,7 +154,7 @@ public final class HttpAsyncMethods {
NStringEntity entity = new NStringEntity(content, contentType);
httpput.setEntity(entity);
HttpHost target = URIUtils.extractHost(requestURI);
- return new HttpAsyncRequestProducerImpl(target, httpput, entity);
+ return new RequestProducerImpl(target, httpput, entity);
}
public static HttpAsyncRequestProducer createPut(
@@ -167,7 +171,7 @@ public final class HttpAsyncMethods {
HttpPut httpput = new HttpPut(requestURI);
NByteArrayEntity entity = new NByteArrayEntity(content, contentType);
HttpHost target = URIUtils.extractHost(requestURI);
- return new HttpAsyncRequestProducerImpl(target, httpput, entity);
+ return new RequestProducerImpl(target, httpput, entity);
}
public static HttpAsyncRequestProducer createPut(
@@ -206,7 +210,7 @@ public final class HttpAsyncMethods {
}
public static HttpAsyncResponseConsumer<HttpResponse> createConsumer() {
- return new BasicHttpAsyncResponseConsumer();
+ return new BasicAsyncResponseConsumer();
}
public static HttpAsyncResponseConsumer<HttpResponse> createZeroCopyConsumer(final File file) {
@@ -220,4 +224,19 @@ public final class HttpAsyncMethods {
};
}
+ static class RequestProducerImpl extends BasicAsyncRequestProducer {
+
+ protected RequestProducerImpl(
+ final HttpHost target,
+ final HttpEntityEnclosingRequest request,
+ final ProducingNHttpEntity producer) {
+ super(target, request, producer);
+ }
+
+ public RequestProducerImpl(final HttpHost target, final HttpRequest request) {
+ super(target, request);
+ }
+
+ }
+
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java Thu Sep 22 20:03:35 2011
@@ -37,9 +37,11 @@ import org.apache.http.nio.ContentDecode
import org.apache.http.nio.ContentDecoderChannel;
import org.apache.http.nio.FileContentDecoder;
import org.apache.http.nio.IOControl;
+import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer;
import org.apache.http.protocol.HTTP;
+import org.apache.http.protocol.HttpContext;
-public abstract class ZeroCopyConsumer<T> extends AbstractHttpAsyncResponseConsumer<T> {
+public abstract class ZeroCopyConsumer<T> extends AbstractAsyncResponseConsumer<T> {
private final File file;
@@ -88,7 +90,7 @@ public abstract class ZeroCopyConsumer<T
protected abstract T process(final HttpResponse response, final File file) throws Exception;
@Override
- protected T buildResult() throws Exception {
+ protected T buildResult(final HttpContext context) throws Exception {
FileEntity entity = new FileEntity(this.file);
entity.setContentType(this.response.getFirstHeader(HTTP.CONTENT_TYPE));
this.response.setEntity(entity);
@@ -96,7 +98,7 @@ public abstract class ZeroCopyConsumer<T
}
@Override
- protected void onCleanup() {
+ protected void releaseResources() {
if (this.fileChannel != null) {
try {
this.fileChannel.close();
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/OperatedClientConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/OperatedClientConnection.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/OperatedClientConnection.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/OperatedClientConnection.java Thu Sep 22 20:03:35 2011
@@ -27,7 +27,6 @@
package org.apache.http.nio.conn;
import org.apache.http.HttpInetConnection;
-import org.apache.http.impl.nio.reactor.SSLIOSession;
import org.apache.http.nio.NHttpClientConnection;
import org.apache.http.nio.NHttpClientIOTarget;
import org.apache.http.nio.reactor.IOSession;
@@ -37,6 +36,6 @@ public interface OperatedClientConnectio
void upgrade(IOSession iosession);
- SSLIOSession getSSLIOSession();
+ IOSession getIOSession();
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java Thu Sep 22 20:03:35 2011
@@ -48,11 +48,11 @@ import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
-import org.apache.http.impl.nio.reactor.SSLIOSession;
-import org.apache.http.impl.nio.reactor.SSLSetupHandler;
import org.apache.http.nio.conn.scheme.LayeringStrategy;
import org.apache.http.nio.reactor.IOSession;
-import org.apache.http.params.HttpParams;
+import org.apache.http.nio.reactor.ssl.SSLIOSession;
+import org.apache.http.nio.reactor.ssl.SSLMode;
+import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
public class SSLLayeringStrategy implements LayeringStrategy {
@@ -187,7 +187,10 @@ public class SSLLayeringStrategy impleme
}
public SSLIOSession layer(final IOSession iosession) {
- return new SSLIOSession(iosession, this.sslContext, new InternalSSLSetupHandler());
+ SSLIOSession ssliosession = new SSLIOSession(iosession, SSLMode.CLIENT, this.sslContext,
+ new InternalSSLSetupHandler());
+ iosession.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
+ return ssliosession;
}
protected void initializeEngine(final SSLEngine engine) {
@@ -196,8 +199,7 @@ public class SSLLayeringStrategy impleme
class InternalSSLSetupHandler implements SSLSetupHandler {
public void initalize(
- final SSLEngine sslengine,
- final HttpParams params) throws SSLException {
+ final SSLEngine sslengine) throws SSLException {
initializeEngine(sslengine);
}
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java Thu Sep 22 20:03:35 2011
@@ -26,14 +26,20 @@
package org.apache.http.impl.nio.client;
import java.io.IOException;
+import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
+import org.apache.http.HttpAsyncTestBase;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
+import org.apache.http.HttpVersion;
+import org.apache.http.ProtocolVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -41,45 +47,105 @@ import org.apache.http.client.Credential
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
-import org.apache.http.entity.StringEntity;
+import org.apache.http.concurrent.Cancellable;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultTargetAuthenticationHandler;
-import org.apache.http.localserver.AsyncHttpTestBase;
+import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
import org.apache.http.localserver.BasicAuthTokenExtractor;
-import org.apache.http.localserver.LocalTestServer;
import org.apache.http.localserver.RequestBasicAuth;
import org.apache.http.localserver.ResponseBasicUnauthorized;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.nio.NHttpConnectionFactory;
+import org.apache.http.nio.NHttpServerIOTarget;
import org.apache.http.nio.entity.NByteArrayEntity;
import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
+import org.apache.http.nio.protocol.BufferingAsyncRequestHandler;
+import org.apache.http.nio.protocol.HttpAsyncContinueTrigger;
+import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
+import org.apache.http.nio.protocol.HttpAsyncRequestHandlerRegistry;
+import org.apache.http.nio.protocol.HttpAsyncRequestHandlerResolver;
+import org.apache.http.nio.protocol.HttpAsyncServiceHandler;
+import org.apache.http.nio.reactor.IOReactorStatus;
+import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.HttpExpectationVerifier;
import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.ImmutableHttpProcessor;
import org.apache.http.protocol.ResponseConnControl;
import org.apache.http.protocol.ResponseContent;
import org.apache.http.protocol.ResponseDate;
import org.apache.http.protocol.ResponseServer;
+import org.junit.After;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
-public class TestClientAuthentication extends AsyncHttpTestBase {
+public class TestClientAuthentication extends HttpAsyncTestBase {
+
+ @Before
+ public void setUp() throws Exception {
+ initServer();
+ initClient();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ shutDownClient();
+ shutDownServer();
+ }
+
+ @Override
+ public void initServer() throws Exception {
+ super.initServer();
+ this.serverHttpProc = new ImmutableHttpProcessor(
+ new HttpRequestInterceptor[] {
+ new RequestBasicAuth()
+ },
+ new HttpResponseInterceptor[] {
+ new ResponseDate(),
+ new ResponseServer(),
+ new ResponseContent(),
+ new ResponseConnControl(),
+ new ResponseBasicUnauthorized()
+ }
+ );
+ }
+
+ @Override
+ protected NHttpConnectionFactory<NHttpServerIOTarget> createServerConnectionFactory(
+ final HttpParams params) throws Exception {
+ return new DefaultNHttpServerConnectionFactory(params);
+ }
@Override
- protected LocalTestServer createServer() throws Exception {
- BasicHttpProcessor httpproc = new BasicHttpProcessor();
- httpproc.addInterceptor(new ResponseDate());
- httpproc.addInterceptor(new ResponseServer());
- httpproc.addInterceptor(new ResponseContent());
- httpproc.addInterceptor(new ResponseConnControl());
- httpproc.addInterceptor(new RequestBasicAuth());
- httpproc.addInterceptor(new ResponseBasicUnauthorized());
-
- LocalTestServer localServer = new LocalTestServer(httpproc, null);
- localServer.register("*", new AuthHandler());
- return localServer;
+ protected String getSchemeName() {
+ return "http";
+ }
+
+ private HttpHost start(
+ final HttpAsyncRequestHandlerResolver requestHandlerResolver,
+ final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
+ HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
+ requestHandlerResolver,
+ expectationVerifier,
+ this.serverHttpProc,
+ new DefaultConnectionReuseStrategy(),
+ this.serverParams);
+ this.server.start(serviceHandler);
+ this.httpclient.start();
+
+ ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+ endpoint.waitFor();
+
+ Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
+ InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
+ HttpHost target = new HttpHost("localhost", address.getPort(), getSchemeName());
+ return target;
}
static class AuthHandler implements HttpRequestHandler {
@@ -93,7 +159,7 @@ public class TestClientAuthentication ex
response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
} else {
response.setStatusCode(HttpStatus.SC_OK);
- StringEntity entity = new StringEntity("success", HTTP.ASCII);
+ NStringEntity entity = new NStringEntity("success", HTTP.ASCII);
response.setEntity(entity);
}
}
@@ -130,7 +196,7 @@ public class TestClientAuthentication ex
}
- static class AuthExpectationVerifier implements HttpExpectationVerifier {
+ static class AuthExpectationVerifier implements HttpAsyncExpectationVerifier {
private final BasicAuthTokenExtractor authTokenExtractor;
@@ -139,16 +205,22 @@ public class TestClientAuthentication ex
this.authTokenExtractor = new BasicAuthTokenExtractor();
}
- public void verify(
+ public Cancellable verify(
final HttpRequest request,
- final HttpResponse response,
- final HttpContext context) throws HttpException {
+ final HttpAsyncContinueTrigger trigger,
+ final HttpContext context) throws HttpException, IOException {
+ ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+ if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
+ ver = HttpVersion.HTTP_1_1;
+ }
String creds = this.authTokenExtractor.extract(request);
if (creds == null || !creds.equals("test:test")) {
- response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
+ HttpResponse response = new BasicHttpResponse(ver, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ trigger.submitResponse(new BasicAsyncResponseProducer(response));
} else {
- response.setStatusCode(HttpStatus.SC_CONTINUE);
+ trigger.continueRequest();
}
+ return null;
}
}
@@ -182,11 +254,15 @@ public class TestClientAuthentication ex
@Test
public void testBasicAuthenticationNoCreds() throws Exception {
+ HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+ registry.register("*", new BufferingAsyncRequestHandler(new AuthHandler()));
+ HttpHost target = start(registry, null);
+
TestCredentialsProvider credsProvider = new TestCredentialsProvider(null);
this.httpclient.setCredentialsProvider(credsProvider);
HttpGet httpget = new HttpGet("/");
- Future<HttpResponse> future = this.httpclient.execute(this.target, httpget, null);
+ Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
@@ -197,12 +273,16 @@ public class TestClientAuthentication ex
@Test
public void testBasicAuthenticationFailure() throws Exception {
+ HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+ registry.register("*", new BufferingAsyncRequestHandler(new AuthHandler()));
+ HttpHost target = start(registry, null);
+
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "all-wrong"));
this.httpclient.setCredentialsProvider(credsProvider);
HttpGet httpget = new HttpGet("/");
- Future<HttpResponse> future = this.httpclient.execute(this.target, httpget, null);
+ Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
@@ -213,12 +293,16 @@ public class TestClientAuthentication ex
@Test
public void testBasicAuthenticationSuccess() throws Exception {
+ HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+ registry.register("*", new BufferingAsyncRequestHandler(new AuthHandler()));
+ HttpHost target = start(registry, null);
+
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
this.httpclient.setCredentialsProvider(credsProvider);
HttpGet httpget = new HttpGet("/");
- Future<HttpResponse> future = this.httpclient.execute(this.target, httpget, null);
+ Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
@@ -229,21 +313,10 @@ public class TestClientAuthentication ex
@Test
public void testBasicAuthenticationSuccessWithNonRepeatableExpectContinue() throws Exception {
- stopServer();
-
- BasicHttpProcessor httpproc = new BasicHttpProcessor();
- httpproc.addInterceptor(new ResponseDate());
- httpproc.addInterceptor(new ResponseServer());
- httpproc.addInterceptor(new ResponseContent());
- httpproc.addInterceptor(new ResponseConnControl());
- httpproc.addInterceptor(new RequestBasicAuth());
- httpproc.addInterceptor(new ResponseBasicUnauthorized());
- this.localServer = new LocalTestServer(
- httpproc, null, null, new AuthExpectationVerifier(), null, null);
- this.localServer.start();
- this.localServer.register("*", new AuthHandler());
- int port = this.localServer.getServiceAddress().getPort();
- this.target = new HttpHost("localhost", port);
+ HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+ registry.register("*", new BufferingAsyncRequestHandler(new AuthHandler()));
+ AuthExpectationVerifier expectationVerifier = new AuthExpectationVerifier();
+ HttpHost target = start(registry, expectationVerifier);
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
@@ -264,7 +337,7 @@ public class TestClientAuthentication ex
httpput.setEntity(entity);
httpput.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
- Future<HttpResponse> future = this.httpclient.execute(this.target, httpput, null);
+ Future<HttpResponse> future = this.httpclient.execute(target, httpput, null);
HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
@@ -272,6 +345,10 @@ public class TestClientAuthentication ex
@Test(expected=ExecutionException.class)
public void testBasicAuthenticationFailureWithNonRepeatableEntityExpectContinueOff() throws Exception {
+ HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+ registry.register("*", new BufferingAsyncRequestHandler(new AuthHandler()));
+ HttpHost target = start(registry, null);
+
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
@@ -292,7 +369,7 @@ public class TestClientAuthentication ex
httpput.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
try {
- Future<HttpResponse> future = this.httpclient.execute(this.target, httpput, null);
+ Future<HttpResponse> future = this.httpclient.execute(target, httpput, null);
future.get();
Assert.fail("ExecutionException should have been thrown");
} catch (ExecutionException ex) {
@@ -304,6 +381,10 @@ public class TestClientAuthentication ex
@Test
public void testBasicAuthenticationSuccessOnRepeatablePost() throws Exception {
+ HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+ registry.register("*", new BufferingAsyncRequestHandler(new AuthHandler()));
+ HttpHost target = start(registry, null);
+
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
@@ -312,7 +393,7 @@ public class TestClientAuthentication ex
HttpPost httppost = new HttpPost("/");
httppost.setEntity(new NStringEntity("some important stuff", HTTP.ISO_8859_1));
- Future<HttpResponse> future = this.httpclient.execute(this.target, httppost, null);
+ Future<HttpResponse> future = this.httpclient.execute(target, httppost, null);
HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
@@ -323,6 +404,10 @@ public class TestClientAuthentication ex
@Test
public void testBasicAuthenticationCredentialsCaching() throws Exception {
+ HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+ registry.register("*", new BufferingAsyncRequestHandler(new AuthHandler()));
+ HttpHost target = start(registry, null);
+
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("test", "test"));
@@ -335,13 +420,13 @@ public class TestClientAuthentication ex
HttpContext context = new BasicHttpContext();
HttpGet httpget1 = new HttpGet("/");
- Future<HttpResponse> future1 = this.httpclient.execute(this.target, httpget1, context, null);
+ Future<HttpResponse> future1 = this.httpclient.execute(target, httpget1, context, null);
HttpResponse response1 = future1.get();
Assert.assertNotNull(response1);
Assert.assertEquals(HttpStatus.SC_OK, response1.getStatusLine().getStatusCode());
HttpGet httpget2 = new HttpGet("/");
- Future<HttpResponse> future2 = this.httpclient.execute(this.target, httpget2, context, null);
+ Future<HttpResponse> future2 = this.httpclient.execute(target, httpget2, context, null);
HttpResponse response2 = future2.get();
Assert.assertNotNull(response2);
Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java?rev=1174357&r1=1174356&r2=1174357&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java Thu Sep 22 20:03:35 2011
@@ -27,33 +27,103 @@
package org.apache.http.impl.nio.client;
import java.io.IOException;
+import java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
+import org.apache.http.HttpAsyncTestBase;
import org.apache.http.HttpEntity;
+import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
-import org.apache.http.localserver.AsyncHttpTestBase;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
+import org.apache.http.localserver.EchoHandler;
+import org.apache.http.localserver.RandomHandler;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
-import org.apache.http.nio.client.HttpAsyncRequestProducer;
-import org.apache.http.nio.client.methods.BasicHttpAsyncResponseConsumer;
+import org.apache.http.nio.NHttpConnectionFactory;
+import org.apache.http.nio.NHttpServerIOTarget;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.nio.entity.NByteArrayEntity;
+import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
+import org.apache.http.nio.protocol.BufferingAsyncRequestHandler;
+import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
+import org.apache.http.nio.protocol.HttpAsyncRequestHandlerRegistry;
+import org.apache.http.nio.protocol.HttpAsyncRequestHandlerResolver;
+import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
+import org.apache.http.nio.protocol.HttpAsyncServiceHandler;
+import org.apache.http.nio.reactor.IOReactorStatus;
+import org.apache.http.nio.reactor.ListenerEndpoint;
+import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
+import org.junit.After;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
-public class TestHttpAsync extends AsyncHttpTestBase {
+public class TestHttpAsync extends HttpAsyncTestBase {
+
+ @Before
+ public void setUp() throws Exception {
+ initServer();
+ initClient();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ shutDownClient();
+ shutDownServer();
+ }
+
+ @Override
+ protected NHttpConnectionFactory<NHttpServerIOTarget> createServerConnectionFactory(
+ final HttpParams params) throws Exception {
+ return new DefaultNHttpServerConnectionFactory(params);
+ }
+
+ @Override
+ protected String getSchemeName() {
+ return "http";
+ }
+
+ private HttpHost start(
+ final HttpAsyncRequestHandlerResolver requestHandlerResolver,
+ final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
+ HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
+ requestHandlerResolver,
+ expectationVerifier,
+ this.serverHttpProc,
+ new DefaultConnectionReuseStrategy(),
+ this.serverParams);
+ this.server.start(serviceHandler);
+ this.httpclient.start();
+
+ ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+ endpoint.waitFor();
+
+ Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
+ InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
+ HttpHost target = new HttpHost("localhost", address.getPort(), getSchemeName());
+ return target;
+ }
+
+ private HttpHost start() throws Exception {
+ HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+ registry.register("/echo/*", new BufferingAsyncRequestHandler(new EchoHandler()));
+ registry.register("/random/*", new BufferingAsyncRequestHandler(new RandomHandler()));
+ return start(registry, null);
+ }
@Test
public void testSingleGet() throws Exception {
+ HttpHost target = start();
HttpGet httpget = new HttpGet("/random/2048");
- Future<HttpResponse> future = this.httpclient.execute(this.target, httpget, null);
+ Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
@@ -61,6 +131,7 @@ public class TestHttpAsync extends Async
@Test
public void testSinglePost() throws Exception {
+ HttpHost target = start();
byte[] b1 = new byte[1024];
Random rnd = new Random(System.currentTimeMillis());
rnd.nextBytes(b1);
@@ -68,7 +139,7 @@ public class TestHttpAsync extends Async
HttpPost httppost = new HttpPost("/echo/stuff");
httppost.setEntity(new NByteArrayEntity(b1));
- Future<HttpResponse> future = this.httpclient.execute(this.target, httppost, null);
+ Future<HttpResponse> future = this.httpclient.execute(target, httppost, null);
HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
@@ -80,21 +151,22 @@ public class TestHttpAsync extends Async
@Test
public void testMultiplePostsOverMultipleConnections() throws Exception {
+ HttpHost target = start();
byte[] b1 = new byte[1024];
Random rnd = new Random(System.currentTimeMillis());
rnd.nextBytes(b1);
int reqCount = 20;
- this.sessionManager.setDefaultMaxPerRoute(reqCount);
- this.sessionManager.setMaxTotal(100);
+ this.connMgr.setDefaultMaxPerRoute(reqCount);
+ this.connMgr.setMaxTotal(100);
Queue<Future<HttpResponse>> queue = new LinkedList<Future<HttpResponse>>();
for (int i = 0; i < reqCount; i++) {
HttpPost httppost = new HttpPost("/echo/stuff");
httppost.setEntity(new NByteArrayEntity(b1));
- queue.add(this.httpclient.execute(this.target, httppost, null));
+ queue.add(this.httpclient.execute(target, httppost, null));
}
while (!queue.isEmpty()) {
@@ -111,21 +183,22 @@ public class TestHttpAsync extends Async
@Test
public void testMultiplePostsOverSingleConnection() throws Exception {
+ HttpHost target = start();
byte[] b1 = new byte[1024];
Random rnd = new Random(System.currentTimeMillis());
rnd.nextBytes(b1);
int reqCount = 20;
- this.sessionManager.setDefaultMaxPerRoute(1);
- this.sessionManager.setMaxTotal(100);
+ this.connMgr.setDefaultMaxPerRoute(1);
+ this.connMgr.setMaxTotal(100);
Queue<Future<HttpResponse>> queue = new LinkedList<Future<HttpResponse>>();
for (int i = 0; i < reqCount; i++) {
HttpPost httppost = new HttpPost("/echo/stuff");
httppost.setEntity(new NByteArrayEntity(b1));
- queue.add(this.httpclient.execute(this.target, httppost, null));
+ queue.add(this.httpclient.execute(target, httppost, null));
}
while (!queue.isEmpty()) {
@@ -142,9 +215,10 @@ public class TestHttpAsync extends Async
@Test
public void testRequestFailure() throws Exception {
+ HttpHost target = start();
HttpGet httpget = new HttpGet("/random/2048");
- HttpAsyncRequestProducer requestProducer = HttpAsyncMethods.create(this.target, httpget) ;
- BasicHttpAsyncResponseConsumer responseConsumer = new BasicHttpAsyncResponseConsumer() {
+ HttpAsyncRequestProducer requestProducer = HttpAsyncMethods.create(target, httpget) ;
+ BasicAsyncResponseConsumer responseConsumer = new BasicAsyncResponseConsumer() {
@Override
public void consumeContent(final ContentDecoder decoder, final IOControl ioctrl)