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/11/30 17:06:17 UTC
svn commit: r1208477 [1/3] - in /httpcomponents/httpcore/trunk:
httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/
httpcore-nio/src/main/java/org/apache/http/nio/protocol/
httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/ httpcore-...
Author: olegk
Date: Wed Nov 30 16:06:12 2011
New Revision: 1208477
URL: http://svn.apache.org/viewvc?rev=1208477&view=rev
Log:
Refactored HttpAsyncServiceHandler API; code cleanup in HttpAsyncServiceHandler and HttpAsyncClientProtocolHandler
Added:
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceExchange.java (contents, props changed)
- copied, changed from r1207880, httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncResponseTrigger.java
Removed:
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncContinueTrigger.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncResponseTrigger.java
Modified:
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOReactorConfig.java (contents, props changed)
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BufferingAsyncRequestHandler.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/ErrorResponseProducer.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncExpectationVerifier.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullRequestHandler.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlers.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncClientProtocolHandler.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncServiceHandler.java
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/integration/TestSyncHttp.java
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOReactorConfig.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOReactorConfig.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
(empty)
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOReactorConfig.java
('svn:executable' removed)
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BufferingAsyncRequestHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BufferingAsyncRequestHandler.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BufferingAsyncRequestHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BufferingAsyncRequestHandler.java Wed Nov 30 16:06:12 2011
@@ -31,14 +31,8 @@ import java.io.IOException;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpResponseFactory;
-import org.apache.http.HttpStatus;
-import org.apache.http.HttpVersion;
-import org.apache.http.ProtocolVersion;
import org.apache.http.annotation.Immutable;
import org.apache.http.concurrent.Cancellable;
-import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
@@ -49,24 +43,13 @@ import org.apache.http.protocol.HttpRequ
public class BufferingAsyncRequestHandler implements HttpAsyncRequestHandler<HttpRequest> {
private final HttpRequestHandler handler;
- private final HttpResponseFactory responseFactory;
- public BufferingAsyncRequestHandler(
- final HttpRequestHandler handler,
- final HttpResponseFactory responseFactory) {
+ public BufferingAsyncRequestHandler(final HttpRequestHandler handler) {
super();
if (handler == null) {
throw new IllegalArgumentException("Request handler may not be null");
}
- if (responseFactory == null) {
- throw new IllegalArgumentException("Response factory may not be null");
- }
this.handler = handler;
- this.responseFactory = responseFactory;
- }
-
- public BufferingAsyncRequestHandler(final HttpRequestHandler handler) {
- this(handler, new DefaultHttpResponseFactory());
}
public HttpAsyncRequestConsumer<HttpRequest> processRequest(final HttpRequest request,
@@ -74,15 +57,12 @@ public class BufferingAsyncRequestHandle
return new BasicAsyncRequestConsumer();
}
- public Cancellable handle(final HttpRequest request, final HttpAsyncResponseTrigger trigger,
+ public Cancellable handle(
+ final HttpRequest request,
+ final HttpAsyncServiceExchange httpexchange,
final HttpContext context) throws HttpException, IOException {
- ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
- if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
- ver = HttpVersion.HTTP_1_1;
- }
- HttpResponse response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
- this.handler.handle(request, response, context);
- trigger.submitResponse(new BasicAsyncResponseProducer(response));
+ this.handler.handle(request, httpexchange.getResponse(), context);
+ httpexchange.submitResponse();
return null;
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/ErrorResponseProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/ErrorResponseProducer.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/ErrorResponseProducer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/ErrorResponseProducer.java Wed Nov 30 16:06:12 2011
@@ -28,13 +28,9 @@
package org.apache.http.nio.protocol;
import java.io.IOException;
-import java.util.Locale;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
-import org.apache.http.HttpVersion;
-import org.apache.http.impl.EnglishReasonPhraseCatalog;
-import org.apache.http.message.BasicHttpResponse;
import org.apache.http.nio.ContentEncoder;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.entity.EntityAsyncContentProducer;
@@ -44,20 +40,17 @@ import org.apache.http.protocol.HttpCont
class ErrorResponseProducer implements HttpAsyncResponseProducer {
- private final HttpVersion version;
- private final int status;
+ private final HttpResponse response;
private final HttpEntity entity;
private final HttpAsyncContentProducer contentProducer;
private final boolean keepAlive;
ErrorResponseProducer(
- final HttpVersion version,
- final int status,
+ final HttpResponse response,
final HttpEntity entity,
final boolean keepAlive) {
super();
- this.version = version;
- this.status = status;
+ this.response = response;
this.entity = entity;
if (entity instanceof HttpAsyncContentProducer) {
this.contentProducer = (HttpAsyncContentProducer) entity;
@@ -68,8 +61,6 @@ class ErrorResponseProducer implements H
}
public HttpResponse generateResponse() {
- BasicHttpResponse response = new BasicHttpResponse(this.version, this.status,
- EnglishReasonPhraseCatalog.INSTANCE.getReason(this.status, Locale.US));
if (this.keepAlive) {
response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
} else {
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientProtocolHandler.java Wed Nov 30 16:06:12 2011
@@ -59,21 +59,22 @@ public class HttpAsyncClientProtocolHand
}
public void connected(final NHttpClientConnection conn, final Object attachment) {
- HttpExchangeState state = new HttpExchangeState();
+ State state = new State();
HttpContext context = conn.getContext();
- context.setAttribute(HTTP_EXCHANGE, state);
+ context.setAttribute(HTTP_EXCHANGE_STATE, state);
requestReady(conn);
}
public void closed(final NHttpClientConnection conn) {
- HttpExchangeState state = getHttpExchange(conn);
+ State state = getState(conn);
if (state != null) {
- state.clear();
+ closeHandler(state);
+ state.reset();
}
}
public void exception(final NHttpClientConnection conn, final HttpException ex) {
- HttpExchangeState state = getHttpExchange(conn);
+ State state = getState(conn);
if (state != null) {
handleProtocolFailure(conn, state, ex);
} else {
@@ -83,7 +84,7 @@ public class HttpAsyncClientProtocolHand
}
public void exception(final NHttpClientConnection conn, final IOException ex) {
- HttpExchangeState state = getHttpExchange(conn);
+ State state = getState(conn);
if (state != null) {
handleFailure(conn, state, ex);
} else {
@@ -93,13 +94,14 @@ public class HttpAsyncClientProtocolHand
}
public void requestReady(final NHttpClientConnection conn) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
if (state.getRequestState() != MessageState.READY) {
return;
}
HttpAsyncClientExchangeHandler<?> handler = state.getHandler();
if (handler != null && handler.isDone()) {
- state.clear();
+ closeHandler(state);
+ state.reset();
handler = null;
}
if (handler == null) {
@@ -137,13 +139,13 @@ public class HttpAsyncClientProtocolHand
} catch (IOException ex) {
handleFailure(conn, state, ex);
} catch (RuntimeException ex) {
- handleFailure(conn, state, ex);
+ terminate(conn, state);
throw ex;
}
}
public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
HttpAsyncClientExchangeHandler<?> handler = ensureNotNull(state.getHandler());
try {
if (state.getRequestState() == MessageState.ACK_EXPECTED) {
@@ -160,13 +162,13 @@ public class HttpAsyncClientProtocolHand
} catch (IOException ex) {
handleFailure(conn, state, ex);
} catch (RuntimeException ex) {
- handleFailure(conn, state, ex);
+ terminate(conn, state);
throw ex;
}
}
public void responseReceived(final NHttpClientConnection conn) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
HttpAsyncClientExchangeHandler<?> handler = ensureNotNull(state.getHandler());
try {
HttpResponse response = conn.getHttpResponse();
@@ -211,13 +213,13 @@ public class HttpAsyncClientProtocolHand
} catch (IOException ex) {
handleFailure(conn, state, ex);
} catch (RuntimeException ex) {
- handleFailure(conn, state, ex);
+ terminate(conn, state);
throw ex;
}
}
public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
HttpAsyncClientExchangeHandler<?> handler = ensureNotNull(state.getHandler());
try {
handler.consumeContent(decoder, conn);
@@ -228,13 +230,13 @@ public class HttpAsyncClientProtocolHand
} catch (IOException ex) {
handleFailure(conn, state, ex);
} catch (RuntimeException ex) {
- handleFailure(conn, state, ex);
+ terminate(conn, state);
throw ex;
}
}
public void timeout(final NHttpClientConnection conn) {
- HttpExchangeState state = getHttpExchange(conn);
+ State state = getState(conn);
if (state != null) {
if (state.getRequestState() == MessageState.ACK_EXPECTED) {
int timeout = state.getTimeout();
@@ -243,7 +245,7 @@ public class HttpAsyncClientProtocolHand
state.setRequestState(MessageState.BODY_STREAM);
return;
} else {
- shutdownHttpExchange(state, new SocketTimeoutException());
+ closeHandler(state, new SocketTimeoutException());
}
}
try {
@@ -281,15 +283,15 @@ public class HttpAsyncClientProtocolHand
}
}
- private HttpExchangeState getHttpExchange(final NHttpConnection conn) {
- return (HttpExchangeState) conn.getContext().getAttribute(HTTP_EXCHANGE);
+ private State getState(final NHttpConnection conn) {
+ return (State) conn.getContext().getAttribute(HTTP_EXCHANGE_STATE);
}
- private HttpExchangeState ensureNotNull(final HttpExchangeState httpExchange) {
- if (httpExchange == null) {
- throw new IllegalStateException("HTTP exchange is null");
+ private State ensureNotNull(final State state) {
+ if (state == null) {
+ throw new IllegalStateException("HTTP exchange state is null");
}
- return httpExchange;
+ return state;
}
private HttpAsyncClientExchangeHandler<?> ensureNotNull(final HttpAsyncClientExchangeHandler<?> handler) {
@@ -299,12 +301,9 @@ public class HttpAsyncClientProtocolHand
return handler;
}
- private void shutdownHttpExchange(
- final HttpExchangeState state,
- final Exception ex) {
+ private void closeHandler(final State state, final Exception ex) {
HttpAsyncClientExchangeHandler<?> handler = state.getHandler();
if (handler != null) {
- state.setHandler(null);
try {
handler.failed(ex);
} finally {
@@ -317,25 +316,46 @@ public class HttpAsyncClientProtocolHand
}
}
+ private void closeHandler(final State state) {
+ HttpAsyncClientExchangeHandler<?> handler = state.getHandler();
+ if (handler != null) {
+ try {
+ handler.close();
+ } catch (IOException ioex) {
+ onException(ioex);
+ }
+ }
+ }
+
+ private void terminate(
+ final NHttpClientConnection conn,
+ final State state) {
+ shutdownConnection(conn);
+ closeHandler(state);
+ state.reset();
+ }
+
private void handleFailure(
final NHttpClientConnection conn,
- final HttpExchangeState state,
+ final State state,
final Exception ex) {
shutdownConnection(conn);
- shutdownHttpExchange(state, ex);
+ closeHandler(state, ex);
+ state.reset();
}
private void handleProtocolFailure(
final NHttpClientConnection conn,
- final HttpExchangeState state,
+ final State state,
final HttpException ex) {
closeConnection(conn);
- shutdownHttpExchange(state, ex);
+ closeHandler(state, ex);
+ state.reset();
}
private void processResponse(
final NHttpClientConnection conn,
- final HttpExchangeState state,
+ final State state,
final HttpAsyncClientExchangeHandler<?> handler) throws IOException {
HttpContext context = handler.getContext();
if (state.isValid()) {
@@ -373,9 +393,9 @@ public class HttpAsyncClientProtocolHand
&& status != HttpStatus.SC_RESET_CONTENT;
}
- static final String HTTP_EXCHANGE = "http.nio.exchange";
+ static final String HTTP_EXCHANGE_STATE = "http.nio.http-exchange-state";
- class HttpExchangeState {
+ static class State {
private volatile HttpAsyncClientExchangeHandler<?> handler;
private volatile MessageState requestState;
@@ -385,7 +405,7 @@ public class HttpAsyncClientProtocolHand
private volatile boolean valid;
private volatile int timeout;
- HttpExchangeState() {
+ State() {
super();
this.valid = true;
this.requestState = MessageState.READY;
@@ -440,23 +460,12 @@ public class HttpAsyncClientProtocolHand
this.timeout = timeout;
}
- public void clear() {
- if (this.handler != null) {
- try {
- this.handler.close();
- } catch (IOException ex) {
- onException(ex);
- }
- this.handler = null;
- }
- reset();
- }
-
public void reset() {
this.responseState = MessageState.READY;
this.requestState = MessageState.READY;
this.response = null;
this.request = null;
+ this.handler = null;
this.timeout = 0;
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncExpectationVerifier.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncExpectationVerifier.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncExpectationVerifier.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncExpectationVerifier.java Wed Nov 30 16:06:12 2011
@@ -30,7 +30,6 @@ package org.apache.http.nio.protocol;
import java.io.IOException;
import org.apache.http.HttpException;
-import org.apache.http.HttpRequest;
import org.apache.http.concurrent.Cancellable;
import org.apache.http.protocol.HttpContext;
@@ -40,8 +39,7 @@ import org.apache.http.protocol.HttpCont
public interface HttpAsyncExpectationVerifier {
Cancellable verify(
- HttpRequest request,
- HttpAsyncContinueTrigger trigger,
+ HttpAsyncServiceExchange httpExchange,
HttpContext context) throws HttpException, IOException;
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandler.java Wed Nov 30 16:06:12 2011
@@ -45,7 +45,7 @@ public interface HttpAsyncRequestHandler
Cancellable handle(
T data,
- HttpAsyncResponseTrigger trigger,
+ HttpAsyncServiceExchange httpExchange,
HttpContext context) throws HttpException, IOException;
}
Copied: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceExchange.java (from r1207880, httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncResponseTrigger.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceExchange.java?p2=httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceExchange.java&p1=httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncResponseTrigger.java&r1=1207880&r2=1208477&rev=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncResponseTrigger.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceExchange.java Wed Nov 30 16:06:12 2011
@@ -27,13 +27,22 @@
package org.apache.http.nio.protocol;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+
/**
* @since 4.2
*/
-public interface HttpAsyncResponseTrigger {
+public interface HttpAsyncServiceExchange {
+
+ HttpRequest getRequest();
+
+ HttpResponse getResponse();
void submitResponse(HttpAsyncResponseProducer responseProducer);
- boolean isTriggered();
+ void submitResponse();
+
+ boolean isCompleted();
}
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceExchange.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceExchange.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceExchange.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java Wed Nov 30 16:06:12 2011
@@ -36,18 +36,17 @@ import org.apache.http.HttpEntityEnclosi
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseFactory;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.ProtocolException;
-import org.apache.http.ProtocolVersion;
import org.apache.http.UnsupportedHttpVersionException;
import org.apache.http.annotation.Immutable;
import org.apache.http.concurrent.Cancellable;
-import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.ContentEncoder;
-import org.apache.http.nio.IOControl;
import org.apache.http.nio.NHttpConnection;
import org.apache.http.nio.NHttpServerConnection;
import org.apache.http.nio.NHttpServiceHandler;
@@ -65,64 +64,71 @@ import org.apache.http.protocol.HttpProc
@Immutable // provided injected dependencies are immutable
public class HttpAsyncServiceHandler implements NHttpServiceHandler {
- static final String HTTP_EXCHANGE = "http.nio.http-exchange";
+ static final String HTTP_EXCHANGE_STATE = "http.nio.http-exchange-state";
- private final HttpAsyncRequestHandlerResolver handlerResolver;
- private final HttpAsyncExpectationVerifier expectationVerifier;
private final HttpProcessor httpProcessor;
private final ConnectionReuseStrategy connStrategy;
+ private final HttpResponseFactory responseFactory;
+ private final HttpAsyncRequestHandlerResolver handlerResolver;
+ private final HttpAsyncExpectationVerifier expectationVerifier;
private final HttpParams params;
public HttpAsyncServiceHandler(
- final HttpAsyncRequestHandlerResolver handlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier,
final HttpProcessor httpProcessor,
final ConnectionReuseStrategy connStrategy,
+ final HttpResponseFactory responseFactory,
+ final HttpAsyncRequestHandlerResolver handlerResolver,
+ final HttpAsyncExpectationVerifier expectationVerifier,
final HttpParams params) {
super();
- if (handlerResolver == null) {
- throw new IllegalArgumentException("Handler resolver may not be null.");
- }
if (httpProcessor == null) {
throw new IllegalArgumentException("HTTP processor may not be null.");
}
if (connStrategy == null) {
throw new IllegalArgumentException("Connection reuse strategy may not be null");
}
+ if (responseFactory == null) {
+ throw new IllegalArgumentException("Response factory may not be null");
+ }
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
- this.handlerResolver = handlerResolver;
- this.expectationVerifier = expectationVerifier;
this.httpProcessor = httpProcessor;
this.connStrategy = connStrategy;
+ this.responseFactory = responseFactory;
+ this.handlerResolver = handlerResolver;
+ this.expectationVerifier = expectationVerifier;
this.params = params;
}
public HttpAsyncServiceHandler(
- final HttpAsyncRequestHandlerResolver handlerResolver,
final HttpProcessor httpProcessor,
final ConnectionReuseStrategy connStrategy,
+ final HttpAsyncRequestHandlerResolver handlerResolver,
final HttpParams params) {
- this(handlerResolver, null, httpProcessor, connStrategy, params);
+ this(httpProcessor, connStrategy, new DefaultHttpResponseFactory(),
+ handlerResolver, null, params);
}
public void connected(final NHttpServerConnection conn) {
- HttpExchangeState state = new HttpExchangeState();
- conn.getContext().setAttribute(HTTP_EXCHANGE, state);
+ State state = new State();
+ conn.getContext().setAttribute(HTTP_EXCHANGE_STATE, state);
}
public void closed(final NHttpServerConnection conn) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
- Cancellable asyncProcess = state.getAsyncProcess();
- state.clear();
- if (asyncProcess != null) {
- asyncProcess.cancel();
+ State state = getState(conn);
+ if (state != null) {
+ closeHandlers(state);
+ Cancellable asyncProcess = state.getAsyncProcess();
+ if (asyncProcess != null) {
+ asyncProcess.cancel();
+ }
+ state.reset();
}
}
public void exception(final NHttpServerConnection conn, final HttpException httpex) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
if (state != null) {
handleProtocolFailure(conn, state, httpex);
} else {
@@ -132,7 +138,7 @@ public class HttpAsyncServiceHandler imp
}
public void exception(final NHttpServerConnection conn, final IOException ex) {
- HttpExchangeState state = getHttpExchange(conn);
+ State state = getState(conn);
if (state != null) {
handleFailure(conn, state, ex);
} else {
@@ -142,7 +148,7 @@ public class HttpAsyncServiceHandler imp
}
public void requestReceived(final NHttpServerConnection conn) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
try {
HttpRequest request = conn.getHttpRequest();
HttpContext context = state.getContext();
@@ -163,14 +169,16 @@ public class HttpAsyncServiceHandler imp
if (request instanceof HttpEntityEnclosingRequest) {
if (((HttpEntityEnclosingRequest) request).expectContinue()) {
state.setRequestState(MessageState.ACK_EXPECTED);
+ HttpResponse ack = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
+ HttpStatus.SC_CONTINUE, context);
if (this.expectationVerifier != null) {
conn.suspendInput();
- HttpAsyncContinueTrigger trigger = new ContinueTriggerImpl(state, conn);
- Cancellable asyncProcess = this.expectationVerifier.verify(request, trigger, context);
+ HttpAsyncServiceExchange httpex = new Exchange(
+ request, ack, state, conn);
+ Cancellable asyncProcess = this.expectationVerifier.verify(httpex, context);
state.setAsyncProcess(asyncProcess);
} else {
- HttpResponse response = create100Continue(request);
- conn.submitResponse(response);
+ conn.submitResponse(ack);
state.setRequestState(MessageState.BODY_STREAM);
}
} else {
@@ -187,13 +195,13 @@ public class HttpAsyncServiceHandler imp
} catch (IOException ex) {
handleFailure(conn, state, ex);
} catch (RuntimeException ex) {
- handleFailure(conn, state, ex);
+ terminate(conn, state);
throw ex;
}
}
public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
try {
HttpAsyncRequestConsumer<?> consumer = ensureNotNull(state.getRequestConsumer());
consumer.consumeContent(decoder, conn);
@@ -207,13 +215,13 @@ public class HttpAsyncServiceHandler imp
} catch (IOException ex) {
handleFailure(conn, state, ex);
} catch (RuntimeException ex) {
- handleFailure(conn, state, ex);
+ terminate(conn, state);
throw ex;
}
}
public void responseReady(final NHttpServerConnection conn) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
try {
if (state.getResponse() != null) {
return;
@@ -259,13 +267,13 @@ public class HttpAsyncServiceHandler imp
} catch (IOException ex) {
handleFailure(conn, state, ex);
} catch (RuntimeException ex) {
- handleFailure(conn, state, ex);
+ terminate(conn, state);
throw ex;
}
}
public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {
- HttpExchangeState state = ensureNotNull(getHttpExchange(conn));
+ State state = ensureNotNull(getState(conn));
try {
HttpAsyncResponseProducer responseProducer = state.getResponseProducer();
HttpContext context = state.getContext();
@@ -280,20 +288,21 @@ public class HttpAsyncServiceHandler imp
} else {
conn.requestInput();
}
- state.clear();
+ closeHandlers(state);
+ state.reset();
}
} catch (IOException ex) {
handleFailure(conn, state, ex);
} catch (RuntimeException ex) {
- handleFailure(conn, state, ex);
+ terminate(conn, state);
throw ex;
}
}
public void timeout(final NHttpServerConnection conn) {
- HttpExchangeState state = getHttpExchange(conn);
+ State state = getState(conn);
if (state != null) {
- shutdownHttpExchange(state, new SocketTimeoutException());
+ closeHandlers(state, new SocketTimeoutException());
}
try {
if (conn.getStatus() == NHttpConnection.ACTIVE) {
@@ -311,13 +320,13 @@ public class HttpAsyncServiceHandler imp
}
}
- private HttpExchangeState getHttpExchange(final NHttpConnection conn) {
- return (HttpExchangeState) conn.getContext().getAttribute(HTTP_EXCHANGE);
+ private State getState(final NHttpConnection conn) {
+ return (State) conn.getContext().getAttribute(HTTP_EXCHANGE_STATE);
}
- private HttpExchangeState ensureNotNull(final HttpExchangeState state) {
+ private State ensureNotNull(final State state) {
if (state == null) {
- throw new IllegalStateException("HTTP exchange is null");
+ throw new IllegalStateException("HTTP exchange state is null");
}
return state;
}
@@ -348,10 +357,9 @@ public class HttpAsyncServiceHandler imp
}
}
- private void shutdownHttpExchange(final HttpExchangeState state, final Exception ex) {
+ private void closeHandlers(final State state, final Exception ex) {
HttpAsyncRequestConsumer<Object> consumer = state.getRequestConsumer();
if (consumer != null) {
- state.setRequestConsumer(null);
try {
consumer.failed(ex);
} finally {
@@ -364,7 +372,6 @@ public class HttpAsyncServiceHandler imp
}
HttpAsyncResponseProducer producer = state.getResponseProducer();
if (producer != null) {
- state.setResponseProducer(null);
try {
producer.failed(ex);
} finally {
@@ -375,10 +382,29 @@ public class HttpAsyncServiceHandler imp
}
}
}
- state.setRequestHandler(null);
}
- protected HttpAsyncResponseProducer handleException(final Exception ex) {
+ private void closeHandlers(final State state) {
+ HttpAsyncRequestConsumer<Object> consumer = state.getRequestConsumer();
+ if (consumer != null) {
+ try {
+ consumer.close();
+ } catch (IOException ioex) {
+ onException(ioex);
+ }
+ }
+ HttpAsyncResponseProducer producer = state.getResponseProducer();
+ if (producer != null) {
+ try {
+ producer.close();
+ } catch (IOException ioex) {
+ onException(ioex);
+ }
+ }
+ }
+
+ protected HttpAsyncResponseProducer handleException(
+ final Exception ex, final HttpContext context) {
int code = HttpStatus.SC_INTERNAL_SERVER_ERROR;
if (ex instanceof MethodNotSupportedException) {
code = HttpStatus.SC_NOT_IMPLEMENTED;
@@ -391,17 +417,9 @@ public class HttpAsyncServiceHandler imp
if (message == null) {
message = ex.toString();
}
- return new ErrorResponseProducer(
- HttpVersion.HTTP_1_0, code, NStringEntity.create(message), false);
- }
-
- private HttpResponse create100Continue(final HttpRequest request) {
- ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
- if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
- // Downgrade protocol version if greater than HTTP/1.1
- ver = HttpVersion.HTTP_1_1;
- }
- return new BasicHttpResponse(ver, HttpStatus.SC_CONTINUE, "Continue");
+ HttpResponse response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
+ code, context);
+ return new ErrorResponseProducer(response, NStringEntity.create(message), false);
}
private boolean canResponseHaveBody(final HttpRequest request, final HttpResponse response) {
@@ -415,25 +433,35 @@ public class HttpAsyncServiceHandler imp
&& status != HttpStatus.SC_RESET_CONTENT;
}
+ private void terminate(
+ final NHttpServerConnection conn,
+ final State state) {
+ shutdownConnection(conn);
+ closeHandlers(state);
+ state.reset();
+ }
+
private void handleFailure(
final NHttpServerConnection conn,
- final HttpExchangeState state,
+ final State state,
final Exception ex) {
shutdownConnection(conn);
- shutdownHttpExchange(state, ex);
+ closeHandlers(state, ex);
+ state.reset();
}
private void handleProtocolFailure(
final NHttpServerConnection conn,
- final HttpExchangeState state,
+ final State state,
final HttpException httpex) {
- shutdownHttpExchange(state, httpex);
+ closeHandlers(state, httpex);
if (conn.isResponseSubmitted() || state.getResponseState() != MessageState.READY) {
// There is not much that we can do if a response
// has already been submitted
closeConnection(conn);
} else {
- HttpAsyncResponseProducer responseProducer = handleException(httpex);
+ HttpContext context = state.getContext();
+ HttpAsyncResponseProducer responseProducer = handleException(httpex, context);
state.setResponseProducer(responseProducer);
try {
HttpResponse response = responseProducer.generateResponse();
@@ -450,7 +478,7 @@ public class HttpAsyncServiceHandler imp
private void processRequest(
final NHttpServerConnection conn,
- final HttpExchangeState state) throws HttpException, IOException {
+ final State state) throws HttpException, IOException {
HttpAsyncRequestHandler<Object> handler = state.getRequestHandler();
HttpContext context = state.getContext();
HttpAsyncRequestConsumer<?> consumer = state.getRequestConsumer();
@@ -458,17 +486,21 @@ public class HttpAsyncServiceHandler imp
state.setRequestState(MessageState.COMPLETED);
Exception exception = consumer.getException();
if (exception != null) {
- HttpAsyncResponseProducer responseProducer = handleException(exception);
+ HttpAsyncResponseProducer responseProducer = handleException(exception, context);
state.setResponseProducer(responseProducer);
conn.requestOutput();
} else {
+ HttpRequest request = state.getRequest();
Object result = consumer.getResult();
- HttpAsyncResponseTrigger trigger = new ResponseTriggerImpl(state, conn);
+ HttpResponse response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
+ HttpStatus.SC_OK, context);
+ HttpAsyncServiceExchange httpex = new Exchange(
+ request, response, state, conn);
try {
- Cancellable asyncProcess = handler.handle(result, trigger, context);
+ Cancellable asyncProcess = handler.handle(result, httpex, context);
state.setAsyncProcess(asyncProcess);
} catch (HttpException ex) {
- HttpAsyncResponseProducer responseProducer = handleException(ex);
+ HttpAsyncResponseProducer responseProducer = handleException(ex, context);
state.setResponseProducer(responseProducer);
conn.requestOutput();
}
@@ -477,7 +509,7 @@ public class HttpAsyncServiceHandler imp
private void commitFinalResponse(
final NHttpServerConnection conn,
- final HttpExchangeState state) throws IOException, HttpException {
+ final State state) throws IOException, HttpException {
HttpContext context = state.getContext();
HttpRequest request = state.getRequest();
HttpResponse response = state.getResponse();
@@ -503,7 +535,8 @@ public class HttpAsyncServiceHandler imp
// Ready to process new request
conn.requestInput();
}
- state.clear();
+ closeHandlers(state);
+ state.reset();
} else {
state.setResponseState(MessageState.BODY_STREAM);
}
@@ -522,7 +555,7 @@ public class HttpAsyncServiceHandler imp
return handler;
}
- class HttpExchangeState {
+ static class State {
private final BasicHttpContext context;
private volatile HttpAsyncRequestHandler<Object> requestHandler;
@@ -534,7 +567,7 @@ public class HttpAsyncServiceHandler imp
private volatile HttpResponse response;
private volatile Cancellable asyncProcess;
- HttpExchangeState() {
+ State() {
super();
this.context = new BasicHttpContext();
this.requestState = MessageState.READY;
@@ -609,25 +642,11 @@ public class HttpAsyncServiceHandler imp
this.asyncProcess = asyncProcess;
}
- public void clear() {
+ public void reset() {
this.responseState = MessageState.READY;
this.requestState = MessageState.READY;
this.requestHandler = null;
- if (this.requestConsumer != null) {
- try {
- this.requestConsumer.close();
- } catch (IOException ex) {
- onException(ex);
- }
- }
this.requestConsumer = null;
- if (this.responseProducer != null) {
- try {
- this.responseProducer.close();
- } catch (IOException ex) {
- onException(ex);
- }
- }
this.responseProducer = null;
this.request = null;
this.response = null;
@@ -656,74 +675,53 @@ public class HttpAsyncServiceHandler imp
}
- class ResponseTriggerImpl implements HttpAsyncResponseTrigger {
+ static class Exchange implements HttpAsyncServiceExchange {
- private final HttpExchangeState state;
- private final IOControl iocontrol;
-
- private volatile boolean triggered;
-
- public ResponseTriggerImpl(final HttpExchangeState state, final IOControl iocontrol) {
+ private final HttpRequest request;
+ private final HttpResponse response;
+ private final State state;
+ private final NHttpServerConnection conn;
+
+ private volatile boolean completed;
+
+ public Exchange(
+ final HttpRequest request,
+ final HttpResponse response,
+ final State state,
+ final NHttpServerConnection conn) {
super();
+ this.request = request;
+ this.response = response;
this.state = state;
- this.iocontrol = iocontrol;
- }
-
- public synchronized void submitResponse(final HttpAsyncResponseProducer responseProducer) {
- if (responseProducer == null) {
- throw new IllegalArgumentException("Response producer may not be null");
- }
- if (this.triggered) {
- throw new IllegalStateException("Response already triggered");
- }
- this.triggered = true;
- this.state.setResponseProducer(responseProducer);
- this.iocontrol.requestOutput();
- }
-
- public boolean isTriggered() {
- return this.triggered;
+ this.conn = conn;
}
- }
-
- class ContinueTriggerImpl implements HttpAsyncContinueTrigger {
-
- private final HttpExchangeState state;
- private final IOControl iocontrol;
-
- private volatile boolean triggered;
-
- public ContinueTriggerImpl(final HttpExchangeState state, final IOControl iocontrol) {
- super();
- this.state = state;
- this.iocontrol = iocontrol;
+ public HttpRequest getRequest() {
+ return this.request;
}
- public synchronized void continueRequest() {
- if (this.triggered) {
- throw new IllegalStateException("Response already triggered");
- }
- this.triggered = true;
- HttpResponse ack = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_CONTINUE, "Continue");
- this.state.setResponseProducer(new BasicAsyncResponseProducer(ack));
- this.iocontrol.requestOutput();
+ public HttpResponse getResponse() {
+ return this.response;
}
public synchronized void submitResponse(final HttpAsyncResponseProducer responseProducer) {
if (responseProducer == null) {
throw new IllegalArgumentException("Response producer may not be null");
}
- if (this.triggered) {
- throw new IllegalStateException("Response already triggered");
+ if (this.completed) {
+ throw new IllegalStateException("Response already submitted");
}
- this.triggered = true;
+ this.completed = true;
this.state.setResponseProducer(responseProducer);
- this.iocontrol.requestOutput();
+ this.conn.requestOutput();
+ }
+
+ public void submitResponse() {
+ submitResponse(new BasicAsyncResponseProducer(this.response));
}
- public boolean isTriggered() {
- return this.triggered;
+ public boolean isCompleted() {
+ return this.completed;
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullRequestHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullRequestHandler.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullRequestHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullRequestHandler.java Wed Nov 30 16:06:12 2011
@@ -28,8 +28,8 @@
package org.apache.http.nio.protocol;
import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
-import org.apache.http.HttpVersion;
import org.apache.http.concurrent.Cancellable;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.protocol.HttpContext;
@@ -47,11 +47,12 @@ class NullRequestHandler implements Http
public Cancellable handle(
final Object obj,
- final HttpAsyncResponseTrigger trigger,
+ final HttpAsyncServiceExchange httpexchange,
final HttpContext context) {
- trigger.submitResponse(new ErrorResponseProducer(
- HttpVersion.HTTP_1_0, HttpStatus.SC_NOT_IMPLEMENTED,
- NStringEntity.create("Service not implemented"), true));
+ HttpResponse response = httpexchange.getResponse();
+ response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
+ httpexchange.submitResponse(new ErrorResponseProducer(
+ response, NStringEntity.create("Service not implemented"), true));
return null;
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java Wed Nov 30 16:06:12 2011
@@ -115,9 +115,9 @@ public class TestDefaultIOReactors exten
HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
- registry,
this.serverHttpProc,
new DefaultConnectionReuseStrategy(),
+ registry,
this.serverParams) {
@Override
@@ -199,9 +199,9 @@ public class TestDefaultIOReactors exten
HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
registry.register("*", new BufferingAsyncRequestHandler(requestHandler));
HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
- registry,
this.serverHttpProc,
new DefaultConnectionReuseStrategy(),
+ registry,
this.serverParams);
HttpAsyncClientProtocolHandler clientHandler = new HttpAsyncClientProtocolHandler();
this.server.start(serviceHandler);
@@ -270,9 +270,9 @@ public class TestDefaultIOReactors exten
HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
registry.register("*", new BufferingAsyncRequestHandler(requestHandler));
HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
- registry,
this.serverHttpProc,
new DefaultConnectionReuseStrategy(),
+ registry,
this.serverParams);
HttpAsyncClientProtocolHandler clientHandler = new HttpAsyncClientProtocolHandler();
this.server.setExceptionHandler(exceptionHandler);
@@ -342,9 +342,9 @@ public class TestDefaultIOReactors exten
HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
registry.register("*", new BufferingAsyncRequestHandler(requestHandler));
HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
- registry,
this.serverHttpProc,
new DefaultConnectionReuseStrategy(),
+ registry,
this.serverParams);
HttpAsyncClientProtocolHandler clientHandler = new HttpAsyncClientProtocolHandler();
this.server.setExceptionHandler(exceptionHandler);
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java Wed Nov 30 16:06:12 2011
@@ -69,9 +69,9 @@ public class TestDefaultListeningIOReact
new ResponseConnControl()
});
HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
- new HttpAsyncRequestHandlerRegistry(),
httpproc,
new DefaultConnectionReuseStrategy(),
+ new HttpAsyncRequestHandlerRegistry(),
params);
return new DefaultServerIODispatch(serviceHandler, params);
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlers.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlers.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlers.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlers.java Wed Nov 30 16:06:12 2011
@@ -46,6 +46,7 @@ import org.apache.http.ProtocolVersion;
import org.apache.http.concurrent.Cancellable;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.message.BasicHttpResponse;
@@ -59,13 +60,12 @@ import org.apache.http.nio.protocol.Basi
import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
import org.apache.http.nio.protocol.BufferingAsyncRequestHandler;
import org.apache.http.nio.protocol.HttpAsyncClientProtocolHandler;
-import org.apache.http.nio.protocol.HttpAsyncContinueTrigger;
import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
import org.apache.http.nio.protocol.HttpAsyncRequestHandlerRegistry;
import org.apache.http.nio.protocol.HttpAsyncRequestHandlerResolver;
-import org.apache.http.nio.protocol.HttpAsyncResponseTrigger;
+import org.apache.http.nio.protocol.HttpAsyncServiceExchange;
import org.apache.http.nio.protocol.HttpAsyncServiceHandler;
import org.apache.http.nio.reactor.IOReactorStatus;
import org.apache.http.nio.reactor.ListenerEndpoint;
@@ -113,10 +113,11 @@ public class TestHttpAsyncHandlers exten
final HttpAsyncRequestHandlerResolver requestHandlerResolver,
final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
- requestHandlerResolver,
- expectationVerifier,
this.serverHttpProc,
new DefaultConnectionReuseStrategy(),
+ new DefaultHttpResponseFactory(),
+ requestHandlerResolver,
+ expectationVerifier,
this.serverParams);
HttpAsyncClientProtocolHandler clientHandler = new HttpAsyncClientProtocolHandler();
this.server.start(serviceHandler);
@@ -391,9 +392,9 @@ public class TestHttpAsyncHandlers exten
HttpAsyncExpectationVerifier expectationVerifier = new HttpAsyncExpectationVerifier() {
public Cancellable verify(
- final HttpRequest request,
- final HttpAsyncContinueTrigger trigger,
+ final HttpAsyncServiceExchange httpexchange,
final HttpContext context) throws HttpException {
+ HttpRequest request = httpexchange.getRequest();
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
String s = request.getRequestLine().getUri();
if (!s.equals("AAAAAx10")) {
@@ -403,9 +404,9 @@ public class TestHttpAsyncHandlers exten
BasicHttpResponse response = new BasicHttpResponse(ver,
HttpStatus.SC_EXPECTATION_FAILED, "Expectation failed");
response.setEntity(NStringEntity.create("Expectation failed"));
- trigger.submitResponse(new BasicAsyncResponseProducer(response));
+ httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
} else {
- trigger.continueRequest();
+ httpexchange.submitResponse();
}
return null;
}
@@ -475,7 +476,7 @@ public class TestHttpAsyncHandlers exten
public Cancellable handle(
final HttpRequest request,
- final HttpAsyncResponseTrigger trigger,
+ final HttpAsyncServiceExchange httpexchange,
final HttpContext context) throws HttpException, IOException {
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
@@ -493,7 +494,7 @@ public class TestHttpAsyncHandlers exten
} catch (Exception ex) {
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
- trigger.submitResponse(new BasicAsyncResponseProducer(response));
+ httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
}
}.start();
return null;
@@ -536,8 +537,7 @@ public class TestHttpAsyncHandlers exten
HttpAsyncExpectationVerifier expectationVerifier = new HttpAsyncExpectationVerifier() {
public Cancellable verify(
- final HttpRequest request,
- final HttpAsyncContinueTrigger trigger,
+ final HttpAsyncServiceExchange httpexchange,
final HttpContext context) throws HttpException {
new Thread() {
@Override
@@ -545,6 +545,7 @@ public class TestHttpAsyncHandlers exten
// Wait a bit, to make sure this is delayed.
try { Thread.sleep(100); } catch(InterruptedException ie) {}
// Set the entity after delaying...
+ HttpRequest request = httpexchange.getRequest();
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
String s = request.getRequestLine().getUri();
if (!s.equals("AAAAAx10")) {
@@ -554,9 +555,9 @@ public class TestHttpAsyncHandlers exten
BasicHttpResponse response = new BasicHttpResponse(ver,
HttpStatus.SC_EXPECTATION_FAILED, "Expectation failed");
response.setEntity(NStringEntity.create("Expectation failed"));
- trigger.submitResponse(new BasicAsyncResponseProducer(response));
+ httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
} else {
- trigger.continueRequest();
+ httpexchange.submitResponse();
}
}
}.start();
@@ -625,7 +626,7 @@ public class TestHttpAsyncHandlers exten
public Cancellable handle(
final HttpRequest request,
- final HttpAsyncResponseTrigger trigger,
+ final HttpAsyncServiceExchange httpexchange,
final HttpContext context) throws HttpException, IOException {
throw new HttpException("Boom");
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestTruncatedChunks.java Wed Nov 30 16:06:12 2011
@@ -194,9 +194,9 @@ public class TestTruncatedChunks extends
HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
registry.register("*", new BufferingAsyncRequestHandler(new SimpleRequestHandler(true)));
HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
- registry,
this.serverHttpProc,
new DefaultConnectionReuseStrategy(),
+ registry,
this.serverParams);
HttpAsyncClientProtocolHandler clientHandler = new HttpAsyncClientProtocolHandler();
this.server.start(serviceHandler);
@@ -275,9 +275,9 @@ public class TestTruncatedChunks extends
HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
registry.register("*", new BufferingAsyncRequestHandler(new SimpleRequestHandler(true)));
HttpAsyncServiceHandler serviceHandler = new HttpAsyncServiceHandler(
- registry,
this.serverHttpProc,
new DefaultConnectionReuseStrategy(),
+ registry,
this.serverParams);
HttpAsyncClientProtocolHandler clientHandler = new HttpAsyncClientProtocolHandler();
this.server.start(serviceHandler);
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java Wed Nov 30 16:06:12 2011
@@ -33,7 +33,6 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.concurrent.Cancellable;
-import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.message.BasicRequestLine;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
@@ -41,7 +40,6 @@ import org.apache.http.protocol.HttpRequ
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
public class TestBufferingAsyncRequestHandler {
@@ -50,16 +48,19 @@ public class TestBufferingAsyncRequestHa
private BufferingAsyncRequestHandler asyncRequestHandler;
private HttpContext context;
private HttpRequest request;
- private HttpAsyncResponseTrigger trigger;
+ private HttpResponse response;
+ private HttpAsyncServiceExchange httpexchange;
@Before
public void setUp() throws Exception {
this.requestHandler = Mockito.mock(HttpRequestHandler.class);
- this.asyncRequestHandler = new BufferingAsyncRequestHandler(this.requestHandler,
- new DefaultHttpResponseFactory());
+ this.asyncRequestHandler = new BufferingAsyncRequestHandler(this.requestHandler);
this.context = new BasicHttpContext();
this.request = Mockito.mock(HttpRequest.class);
- this.trigger = Mockito.mock(HttpAsyncResponseTrigger.class);
+ this.response = Mockito.mock(HttpResponse.class);
+ this.httpexchange = Mockito.mock(HttpAsyncServiceExchange.class);
+ Mockito.when(this.httpexchange.getRequest()).thenReturn(this.request);
+ Mockito.when(this.httpexchange.getResponse()).thenReturn(this.response);
}
@After
@@ -70,12 +71,7 @@ public class TestBufferingAsyncRequestHa
@Test
public void testInvalidConstruction() throws Exception {
try {
- new BufferingAsyncRequestHandler(null, new DefaultHttpResponseFactory());
- Assert.fail("IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
- }
- try {
- new BufferingAsyncRequestHandler(this.requestHandler, null);
+ new BufferingAsyncRequestHandler(null);
Assert.fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
}
@@ -92,30 +88,12 @@ public class TestBufferingAsyncRequestHa
public void testHandleRequest() throws Exception {
Mockito.when(this.request.getRequestLine()).thenReturn(new BasicRequestLine("GET", "/", HttpVersion.HTTP_1_0));
- Cancellable cancellable = this.asyncRequestHandler.handle(this.request, this.trigger, this.context);
-
- Assert.assertNull(cancellable);
- ArgumentCaptor<HttpResponse> argCaptor = ArgumentCaptor.forClass(HttpResponse.class);
- Mockito.verify(this.requestHandler).handle(
- Mockito.eq(this.request), argCaptor.capture(), Mockito.eq(this.context));
- HttpResponse response = argCaptor.getValue();
- Assert.assertEquals(HttpVersion.HTTP_1_0, response.getProtocolVersion());
- Mockito.verify(this.trigger).submitResponse(Mockito.any(BasicAsyncResponseProducer.class));
- }
-
- @Test
- public void testHandleRequestUnsupportedVersion() throws Exception {
- Mockito.when(this.request.getRequestLine()).thenReturn(new BasicRequestLine("GET", "/", new HttpVersion(234, 456)));
-
- Cancellable cancellable = this.asyncRequestHandler.handle(this.request, this.trigger, this.context);
+ Cancellable cancellable = this.asyncRequestHandler.handle(this.request, this.httpexchange, this.context);
Assert.assertNull(cancellable);
- ArgumentCaptor<HttpResponse> argCaptor = ArgumentCaptor.forClass(HttpResponse.class);
Mockito.verify(this.requestHandler).handle(
- Mockito.eq(this.request), argCaptor.capture(), Mockito.eq(this.context));
- HttpResponse response = argCaptor.getValue();
- Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
- Mockito.verify(this.trigger).submitResponse(Mockito.any(BasicAsyncResponseProducer.class));
+ Mockito.eq(this.request), Mockito.eq(this.response), Mockito.eq(this.context));
+ Mockito.verify(this.httpexchange).submitResponse();
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java?rev=1208477&r1=1208476&r2=1208477&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java Wed Nov 30 16:06:12 2011
@@ -32,21 +32,23 @@ import junit.framework.Assert;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HTTP;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
public class TestErrorResponseProducer {
private ErrorResponseProducer erp;
- @Mock private HttpEntity entity;
+ private HttpResponse response;
+ private HttpEntity entity;
@Before
public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
+ response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ entity = new StringEntity("stuff");
}
@After
@@ -55,7 +57,7 @@ public class TestErrorResponseProducer {
@Test
public void testGenerateResponseKeepAlive() {
- erp = new ErrorResponseProducer(HttpVersion.HTTP_1_1, 200, entity, true);
+ erp = new ErrorResponseProducer(response, entity, true);
HttpResponse res = erp.generateResponse();
Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, res.getFirstHeader(HTTP.CONN_DIRECTIVE).getValue());
@@ -65,7 +67,7 @@ public class TestErrorResponseProducer {
@Test
public void testGenerateResponseClose() {
- erp = new ErrorResponseProducer(HttpVersion.HTTP_1_1, 200, entity, false);
+ erp = new ErrorResponseProducer(response, entity, false);
HttpResponse res = erp.generateResponse();
Assert.assertEquals(HTTP.CONN_CLOSE, res.getFirstHeader(HTTP.CONN_DIRECTIVE).getValue());