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/02 12:25:12 UTC
svn commit: r1164464 - in /httpcomponents/httpcore/trunk/httpcore-nio/src:
main/java/org/apache/http/impl/nio/ main/java/org/apache/http/impl/nio/ssl/
test/java/org/apache/http/testserver/
Author: olegk
Date: Fri Sep 2 10:25:12 2011
New Revision: 1164464
URL: http://svn.apache.org/viewvc?rev=1164464&view=rev
Log:
Refactoring of the default i/o event dispatch implementations. Reduced code duplication
Added:
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/AbstractIOEventDispatch.java
- copied, changed from r1164217, httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java
Modified:
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/testserver/HttpSSLServer.java
Copied: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/AbstractIOEventDispatch.java (from r1164217, httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/AbstractIOEventDispatch.java?p2=httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/AbstractIOEventDispatch.java&p1=httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java&r1=1164217&r2=1164464&rev=1164464&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/AbstractIOEventDispatch.java Fri Sep 2 10:25:12 2011
@@ -29,124 +29,51 @@ package org.apache.http.impl.nio;
import java.io.IOException;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpResponseFactory;
import org.apache.http.annotation.Immutable;
-import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.nio.reactor.SSLIOSession;
-import org.apache.http.nio.NHttpClientIOTarget;
-import org.apache.http.nio.NHttpClientHandler;
+import org.apache.http.nio.NHttpConnection;
import org.apache.http.nio.reactor.IOEventDispatch;
import org.apache.http.nio.reactor.IOSession;
-import org.apache.http.nio.util.ByteBufferAllocator;
-import org.apache.http.nio.util.HeapByteBufferAllocator;
-import org.apache.http.params.HttpConnectionParams;
-import org.apache.http.params.HttpParams;
import org.apache.http.protocol.ExecutionContext;
/**
- * Default implementation of {@link IOEventDispatch} interface for plain
- * (unencrypted) client-side HTTP connections.
- * <p>
- * The following parameters can be used to customize the behavior of this
- * class:
- * <ul>
- * <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
- * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
- * <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
- * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
- * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
- * </ul>
+ * Abstract {@link IOEventDispatch} implementation that supports both plain (unencrypted)
+ * and SSL encrypted HTTP connections.
*
- * @since 4.0
+ * @since 4.2
*/
@Immutable // provided injected dependencies are immutable
-public class DefaultClientIOEventDispatch implements IOEventDispatch {
+public abstract class AbstractIOEventDispatch<T extends NHttpConnection> implements IOEventDispatch {
- protected final NHttpClientHandler handler;
- protected final ByteBufferAllocator allocator;
- protected final HttpParams params;
-
- /**
- * Creates a new instance of this class to be used for dispatching I/O event
- * notifications to the given protocol handler.
- *
- * @param handler the client protocol handler.
- * @param params HTTP parameters.
- */
- public DefaultClientIOEventDispatch(
- final NHttpClientHandler handler,
- final HttpParams params) {
- super();
- if (handler == null) {
- throw new IllegalArgumentException("HTTP client handler may not be null");
- }
- if (params == null) {
- throw new IllegalArgumentException("HTTP parameters may not be null");
- }
- this.allocator = createByteBufferAllocator();
- this.handler = handler;
- this.params = params;
- }
-
- /**
- * Creates an instance of {@link HeapByteBufferAllocator} to be used
- * by HTTP connections for allocating {@link java.nio.ByteBuffer} objects.
- * <p>
- * This method can be overridden in a super class in order to provide
- * a different implementation of the {@link ByteBufferAllocator} interface.
- *
- * @return byte buffer allocator.
- */
- protected ByteBufferAllocator createByteBufferAllocator() {
- return new HeapByteBufferAllocator();
- }
-
- /**
- * Creates an instance of {@link DefaultHttpResponseFactory} to be used
- * by HTTP connections for creating {@link HttpResponse} objects.
- * <p>
- * This method can be overridden in a super class in order to provide
- * a different implementation of the {@link HttpResponseFactory} interface.
- *
- * @return HTTP response factory.
- */
- protected HttpResponseFactory createHttpResponseFactory() {
- return new DefaultHttpResponseFactory();
- }
-
- /**
- * Creates an instance of {@link DefaultNHttpClientConnection} based on the
- * given {@link IOSession}.
- * <p>
- * This method can be overridden in a super class in order to provide
- * a different implementation of the {@link NHttpClientIOTarget} interface.
- *
- * @param session the underlying I/O session.
- *
- * @return newly created HTTP connection.
- */
- protected NHttpClientIOTarget createConnection(final IOSession session) {
- return new DefaultNHttpClientConnection(
- session,
- createHttpResponseFactory(),
- this.allocator,
- this.params);
+ protected abstract T createConnection(IOSession session);
+
+ protected abstract void onConnected(T conn);
+
+ protected abstract void onClosed(T conn);
+
+ protected abstract void onException(T conn, IOException ex);
+
+ protected abstract void onInputReady(T conn);
+
+ protected abstract void onOutputReady(T conn);
+
+ protected abstract void onTimeout(T conn);
+
+ private void ensureNotNull(final T conn) {
+ if (conn == null) {
+ throw new IllegalStateException("HTTP connection is null");
+ }
}
public void connected(final IOSession session) {
+ @SuppressWarnings("unchecked")
+ T conn = (T) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
try {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
if (conn == null) {
conn = createConnection(session);
session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
}
- int timeout = HttpConnectionParams.getSoTimeout(this.params);
- conn.setSocketTimeout(timeout);
-
- Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
- this.handler.connected(conn, attachment);
+ onConnected(conn);
} catch (RuntimeException ex) {
session.shutdown();
throw ex;
@@ -154,36 +81,30 @@ public class DefaultClientIOEventDispatc
}
public void disconnected(final IOSession session) {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
+ @SuppressWarnings("unchecked")
+ T conn = (T) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
if (conn != null) {
- this.handler.closed(conn);
- }
- }
-
- private void ensureNotNull(final NHttpClientIOTarget conn) {
- if (conn == null) {
- throw new IllegalStateException("HTTP connection is null");
+ onClosed(conn);
}
}
public void inputReady(final IOSession session) {
+ @SuppressWarnings("unchecked")
+ T conn = (T) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
try {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
ensureNotNull(conn);
SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
IOSession.SSL_SESSION_KEY);
if (ssliosession == null) {
- conn.consumeInput(this.handler);
+ onInputReady(conn);
} else {
try {
if (ssliosession.isAppInputReady()) {
- conn.consumeInput(this.handler);
+ onInputReady(conn);
}
ssliosession.inboundTransport();
} catch (IOException ex) {
- this.handler.exception(conn, ex);
+ onException(conn, ex);
ssliosession.shutdown();
}
}
@@ -194,22 +115,22 @@ public class DefaultClientIOEventDispatc
}
public void outputReady(final IOSession session) {
+ @SuppressWarnings("unchecked")
+ T conn = (T) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
try {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
ensureNotNull(conn);
SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
IOSession.SSL_SESSION_KEY);
if (ssliosession == null) {
- conn.produceOutput(this.handler);
+ onOutputReady(conn);
} else {
try {
if (ssliosession.isAppOutputReady()) {
- conn.produceOutput(this.handler);
+ onOutputReady(conn);
}
ssliosession.outboundTransport();
} catch (IOException ex) {
- this.handler.exception(conn, ex);
+ onException(conn, ex);
ssliosession.shutdown();
}
}
@@ -220,16 +141,16 @@ public class DefaultClientIOEventDispatc
}
public void timeout(final IOSession session) {
+ @SuppressWarnings("unchecked")
+ T conn = (T) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
try {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
IOSession.SSL_SESSION_KEY);
ensureNotNull(conn);
if (ssliosession == null) {
- this.handler.timeout(conn);
+ onTimeout(conn);
} else {
- this.handler.timeout(conn);
+ onTimeout(conn);
synchronized (ssliosession) {
if (ssliosession.isOutboundDone() && !ssliosession.isInboundDone()) {
// The session failed to terminate cleanly
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java?rev=1164464&r1=1164463&r2=1164464&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java Fri Sep 2 10:25:12 2011
@@ -33,7 +33,6 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseFactory;
import org.apache.http.annotation.Immutable;
import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.reactor.SSLIOSession;
import org.apache.http.nio.NHttpClientIOTarget;
import org.apache.http.nio.NHttpClientHandler;
import org.apache.http.nio.reactor.IOEventDispatch;
@@ -42,7 +41,6 @@ import org.apache.http.nio.util.ByteBuff
import org.apache.http.nio.util.HeapByteBufferAllocator;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
-import org.apache.http.protocol.ExecutionContext;
/**
* Default implementation of {@link IOEventDispatch} interface for plain
@@ -61,7 +59,7 @@ import org.apache.http.protocol.Executio
* @since 4.0
*/
@Immutable // provided injected dependencies are immutable
-public class DefaultClientIOEventDispatch implements IOEventDispatch {
+public class DefaultClientIOEventDispatch extends AbstractIOEventDispatch<NHttpClientIOTarget> {
protected final NHttpClientHandler handler;
protected final ByteBufferAllocator allocator;
@@ -126,7 +124,8 @@ public class DefaultClientIOEventDispatc
*
* @return newly created HTTP connection.
*/
- protected NHttpClientIOTarget createConnection(final IOSession session) {
+ @Override
+ protected NHttpClientIOTarget createConnection(final IOSession session) {
return new DefaultNHttpClientConnection(
session,
createHttpResponseFactory(),
@@ -134,113 +133,38 @@ public class DefaultClientIOEventDispatc
this.params);
}
- public void connected(final IOSession session) {
- try {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- if (conn == null) {
- conn = createConnection(session);
- session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
- }
- int timeout = HttpConnectionParams.getSoTimeout(this.params);
- conn.setSocketTimeout(timeout);
-
- Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
- this.handler.connected(conn, attachment);
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
- }
-
- public void disconnected(final IOSession session) {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- if (conn != null) {
- this.handler.closed(conn);
- }
- }
-
- private void ensureNotNull(final NHttpClientIOTarget conn) {
- if (conn == null) {
- throw new IllegalStateException("HTTP connection is null");
- }
- }
-
- public void inputReady(final IOSession session) {
- try {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- ensureNotNull(conn);
- SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
- IOSession.SSL_SESSION_KEY);
- if (ssliosession == null) {
- conn.consumeInput(this.handler);
- } else {
- try {
- if (ssliosession.isAppInputReady()) {
- conn.consumeInput(this.handler);
- }
- ssliosession.inboundTransport();
- } catch (IOException ex) {
- this.handler.exception(conn, ex);
- ssliosession.shutdown();
- }
- }
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
- }
-
- public void outputReady(final IOSession session) {
- try {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- ensureNotNull(conn);
- SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
- IOSession.SSL_SESSION_KEY);
- if (ssliosession == null) {
- conn.produceOutput(this.handler);
- } else {
- try {
- if (ssliosession.isAppOutputReady()) {
- conn.produceOutput(this.handler);
- }
- ssliosession.outboundTransport();
- } catch (IOException ex) {
- this.handler.exception(conn, ex);
- ssliosession.shutdown();
- }
- }
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
- }
-
- public void timeout(final IOSession session) {
- try {
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
- IOSession.SSL_SESSION_KEY);
- ensureNotNull(conn);
- if (ssliosession == null) {
- this.handler.timeout(conn);
- } else {
- this.handler.timeout(conn);
- synchronized (ssliosession) {
- if (ssliosession.isOutboundDone() && !ssliosession.isInboundDone()) {
- // The session failed to terminate cleanly
- ssliosession.shutdown();
- }
- }
- }
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
- }
+ @Override
+ protected void onConnected(final NHttpClientIOTarget conn) {
+ int timeout = HttpConnectionParams.getSoTimeout(this.params);
+ conn.setSocketTimeout(timeout);
+
+ Object attachment = conn.getContext().getAttribute(IOSession.ATTACHMENT_KEY);
+ this.handler.connected(conn, attachment);
+ }
+
+ @Override
+ protected void onClosed(final NHttpClientIOTarget conn) {
+ this.handler.closed(conn);
+ }
+
+ @Override
+ protected void onException(final NHttpClientIOTarget conn, IOException ex) {
+ this.handler.exception(conn, ex);
+ }
+
+ @Override
+ protected void onInputReady(final NHttpClientIOTarget conn) {
+ conn.consumeInput(this.handler);
+ }
+
+ @Override
+ protected void onOutputReady(final NHttpClientIOTarget conn) {
+ conn.produceOutput(this.handler);
+ }
+
+ @Override
+ protected void onTimeout(final NHttpClientIOTarget conn) {
+ this.handler.timeout(conn);
+ }
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java?rev=1164464&r1=1164463&r2=1164464&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java Fri Sep 2 10:25:12 2011
@@ -33,7 +33,6 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestFactory;
import org.apache.http.annotation.Immutable;
import org.apache.http.impl.DefaultHttpRequestFactory;
-import org.apache.http.impl.nio.reactor.SSLIOSession;
import org.apache.http.nio.NHttpServerIOTarget;
import org.apache.http.nio.NHttpServiceHandler;
import org.apache.http.nio.reactor.IOEventDispatch;
@@ -42,7 +41,6 @@ import org.apache.http.nio.util.ByteBuff
import org.apache.http.nio.util.HeapByteBufferAllocator;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
-import org.apache.http.protocol.ExecutionContext;
/**
* Default implementation of {@link IOEventDispatch} interface for plain
@@ -61,7 +59,7 @@ import org.apache.http.protocol.Executio
* @since 4.0
*/
@Immutable // provided injected dependencies are immutable
-public class DefaultServerIOEventDispatch implements IOEventDispatch {
+public class DefaultServerIOEventDispatch extends AbstractIOEventDispatch<NHttpServerIOTarget> {
protected final ByteBufferAllocator allocator;
protected final NHttpServiceHandler handler;
@@ -126,7 +124,8 @@ public class DefaultServerIOEventDispatc
*
* @return newly created HTTP connection.
*/
- protected NHttpServerIOTarget createConnection(final IOSession session) {
+ @Override
+ protected NHttpServerIOTarget createConnection(final IOSession session) {
return new DefaultNHttpServerConnection(
session,
createHttpRequestFactory(),
@@ -134,109 +133,36 @@ public class DefaultServerIOEventDispatc
this.params);
}
- public void connected(final IOSession session) {
- try {
- NHttpServerIOTarget conn = createConnection(session);
- session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-
- int timeout = HttpConnectionParams.getSoTimeout(this.params);
- session.setSocketTimeout(timeout);
-
- this.handler.connected(conn);
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
- }
-
- public void disconnected(final IOSession session) {
- NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- if (conn != null) {
- this.handler.closed(conn);
- }
- }
-
- private void ensureNotNull(final NHttpServerIOTarget conn) {
- if (conn == null) {
- throw new IllegalStateException("HTTP connection is null");
- }
- }
-
- public void inputReady(final IOSession session) {
- try {
- NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- ensureNotNull(conn);
- SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
- IOSession.SSL_SESSION_KEY);
- if (ssliosession == null) {
- conn.consumeInput(this.handler);
- } else {
- try {
- if (ssliosession.isAppInputReady()) {
- conn.consumeInput(this.handler);
- }
- ssliosession.inboundTransport();
- } catch (IOException ex) {
- this.handler.exception(conn, ex);
- ssliosession.shutdown();
- }
- }
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
- }
-
- public void outputReady(final IOSession session) {
- try {
- NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- ensureNotNull(conn);
- SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
- IOSession.SSL_SESSION_KEY);
- if (ssliosession == null) {
- conn.produceOutput(this.handler);
- } else {
- try {
- if (ssliosession.isAppOutputReady()) {
- conn.produceOutput(this.handler);
- }
- ssliosession.outboundTransport();
- } catch (IOException ex) {
- this.handler.exception(conn, ex);
- ssliosession.shutdown();
- }
- }
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
- }
-
- public void timeout(final IOSession session) {
- try {
- NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
- ensureNotNull(conn);
- SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
- IOSession.SSL_SESSION_KEY);
- if (ssliosession == null) {
- this.handler.timeout(conn);
- } else {
- this.handler.timeout(conn);
- synchronized (ssliosession) {
- if (ssliosession.isOutboundDone() && !ssliosession.isInboundDone()) {
- // The session failed to terminate cleanly
- ssliosession.shutdown();
- }
- }
- }
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
- }
+ @Override
+ protected void onConnected(final NHttpServerIOTarget conn) {
+ int timeout = HttpConnectionParams.getSoTimeout(this.params);
+ conn.setSocketTimeout(timeout);
+ this.handler.connected(conn);
+ }
+
+ @Override
+ protected void onClosed(final NHttpServerIOTarget conn) {
+ this.handler.closed(conn);
+ }
+
+ @Override
+ protected void onException(final NHttpServerIOTarget conn, IOException ex) {
+ this.handler.exception(conn, ex);
+ }
+
+ @Override
+ protected void onInputReady(final NHttpServerIOTarget conn) {
+ conn.consumeInput(this.handler);
+ }
+
+ @Override
+ protected void onOutputReady(final NHttpServerIOTarget conn) {
+ conn.produceOutput(this.handler);
+ }
+
+ @Override
+ protected void onTimeout(final NHttpServerIOTarget conn) {
+ this.handler.timeout(conn);
+ }
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java?rev=1164464&r1=1164463&r2=1164464&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java Fri Sep 2 10:25:12 2011
@@ -41,7 +41,6 @@ import org.apache.http.nio.reactor.IOEve
import org.apache.http.nio.reactor.IOSession;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
-import org.apache.http.protocol.ExecutionContext;
/**
* Default implementation of {@link IOEventDispatch} interface for SSL
@@ -128,40 +127,31 @@ public class SSLClientIOEventDispatch ex
return new SSLIOSession(session, sslcontext, sslHandler);
}
+ protected NHttpClientIOTarget createSSLConnection(final SSLIOSession ssliosession) {
+ return super.createConnection(ssliosession);
+ }
+
@Override
- public void connected(final IOSession session) {
+ protected NHttpClientIOTarget createConnection(final IOSession session) {
+ SSLIOSession ssliosession = createSSLIOSession(session, this.sslcontext, this.sslHandler);
+ session.setAttribute(IOSession.SSL_SESSION_KEY, ssliosession);
+ NHttpClientIOTarget conn = createSSLConnection(ssliosession);
try {
+ ssliosession.bind(SSLMode.CLIENT, this.params);
+ } catch (SSLException ex) {
+ this.handler.exception(conn, ex);
+ ssliosession.shutdown();
+ }
+ return conn;
+ }
- NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
- ExecutionContext.HTTP_CONNECTION);
+ @Override
+ public void onConnected(final NHttpClientIOTarget conn) {
+ int timeout = HttpConnectionParams.getSoTimeout(this.params);
+ conn.setSocketTimeout(timeout);
- if (conn == null) {
- SSLIOSession ssliosession = createSSLIOSession(
- session,
- this.sslcontext,
- this.sslHandler);
- session.setAttribute(IOSession.SSL_SESSION_KEY, ssliosession);
- conn = createConnection(ssliosession);
- session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-
- try {
- ssliosession.bind(SSLMode.CLIENT, this.params);
- } catch (SSLException ex) {
- this.handler.exception(conn, ex);
- ssliosession.shutdown();
- }
- }
-
- int timeout = HttpConnectionParams.getSoTimeout(this.params);
- conn.setSocketTimeout(timeout);
-
- Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
- this.handler.connected(conn, attachment);
-
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
- }
+ Object attachment = conn.getContext().getAttribute(IOSession.ATTACHMENT_KEY);
+ this.handler.connected(conn, attachment);
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java?rev=1164464&r1=1164463&r2=1164464&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java Fri Sep 2 10:25:12 2011
@@ -35,13 +35,13 @@ import org.apache.http.impl.nio.DefaultS
import org.apache.http.impl.nio.reactor.SSLIOSession;
import org.apache.http.impl.nio.reactor.SSLMode;
import org.apache.http.impl.nio.reactor.SSLSetupHandler;
+import org.apache.http.nio.NHttpClientIOTarget;
import org.apache.http.nio.NHttpServerIOTarget;
import org.apache.http.nio.NHttpServiceHandler;
import org.apache.http.nio.reactor.IOEventDispatch;
import org.apache.http.nio.reactor.IOSession;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
-import org.apache.http.protocol.ExecutionContext;
/**
* Default implementation of {@link IOEventDispatch} interface for SSL
@@ -128,32 +128,29 @@ public class SSLServerIOEventDispatch ex
return new SSLIOSession(session, sslcontext, sslHandler);
}
- public void connected(final IOSession session) {
- try {
+ protected NHttpServerIOTarget createSSLConnection(final SSLIOSession ssliosession) {
+ return super.createConnection(ssliosession);
+ }
- SSLIOSession ssliosession = createSSLIOSession(
- session,
- this.sslcontext,
- this.sslHandler);
- session.setAttribute(IOSession.SSL_SESSION_KEY, ssliosession);
- NHttpServerIOTarget conn = createConnection(ssliosession);
- session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-
- int timeout = HttpConnectionParams.getSoTimeout(this.params);
- conn.setSocketTimeout(timeout);
-
- this.handler.connected(conn);
-
- try {
- ssliosession.bind(SSLMode.SERVER, this.params);
- } catch (SSLException ex) {
- this.handler.exception(conn, ex);
- ssliosession.shutdown();
- }
- } catch (RuntimeException ex) {
- session.shutdown();
- throw ex;
+ @Override
+ protected NHttpServerIOTarget createConnection(final IOSession session) {
+ SSLIOSession ssliosession = createSSLIOSession(session, this.sslcontext, this.sslHandler);
+ session.setAttribute(IOSession.SSL_SESSION_KEY, ssliosession);
+ NHttpServerIOTarget conn = createSSLConnection(ssliosession);
+ try {
+ ssliosession.bind(SSLMode.SERVER, this.params);
+ } catch (SSLException ex) {
+ this.handler.exception(conn, ex);
+ ssliosession.shutdown();
}
+ return conn;
+ }
+
+ @Override
+ public void onConnected(final NHttpServerIOTarget conn) {
+ int timeout = HttpConnectionParams.getSoTimeout(this.params);
+ conn.setSocketTimeout(timeout);
+ this.handler.connected(conn);
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/testserver/HttpSSLServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/testserver/HttpSSLServer.java?rev=1164464&r1=1164463&r2=1164464&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/testserver/HttpSSLServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/testserver/HttpSSLServer.java Fri Sep 2 10:25:12 2011
@@ -40,13 +40,13 @@ import javax.net.ssl.SSLContext;
import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
import org.apache.http.impl.nio.reactor.ExceptionEvent;
+import org.apache.http.impl.nio.reactor.SSLIOSession;
import org.apache.http.impl.nio.ssl.SSLServerIOEventDispatch;
import org.apache.http.nio.NHttpServerIOTarget;
import org.apache.http.nio.NHttpServiceHandler;
import org.apache.http.nio.reactor.IOEventDispatch;
import org.apache.http.nio.reactor.IOReactorExceptionHandler;
import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.IOSession;
import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.params.HttpParams;
@@ -115,7 +115,7 @@ public class HttpSSLServer {
return new SSLServerIOEventDispatch(serviceHandler, sslcontext, params) {
@Override
- protected NHttpServerIOTarget createConnection(final IOSession session) {
+ protected NHttpServerIOTarget createSSLConnection(final SSLIOSession session) {
return new LoggingNHttpServerConnection(
session,
createHttpRequestFactory(),