You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ad...@apache.org on 2007/12/05 19:06:12 UTC
svn commit: r601453 - in
/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc:
AsyncHttpClient.java codec/HttpIoHandler.java
Author: adc
Date: Wed Dec 5 10:06:11 2007
New Revision: 601453
URL: http://svn.apache.org/viewvc?rev=601453&view=rev
Log:
Need way to pass in scheduler, if desired
Modified:
geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java?rev=601453&r1=601452&r2=601453&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java Wed Dec 5 10:06:11 2007
@@ -19,21 +19,28 @@
*/
package org.apache.ahc;
+import java.net.InetSocketAddress;
+import java.security.GeneralSecurityException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import javax.net.ssl.SSLContext;
+
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoFuture;
+import org.apache.mina.common.IoFutureListener;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.RuntimeIOException;
+import org.apache.mina.filter.SSLFilter;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.transport.socket.nio.SocketConnector;
+import org.apache.mina.transport.socket.nio.SocketSessionConfig;
+
import org.apache.ahc.codec.HttpIoHandler;
import org.apache.ahc.codec.HttpProtocolCodecFactory;
import org.apache.ahc.codec.HttpRequestMessage;
import org.apache.ahc.ssl.TrustManagerFactoryImpl;
import org.apache.ahc.util.AsyncHttpClientException;
-import org.apache.mina.common.*;
-import org.apache.mina.filter.SSLFilter;
-import org.apache.mina.filter.codec.ProtocolCodecFilter;
-import org.apache.mina.transport.socket.nio.SocketConnector;
-import org.apache.mina.transport.socket.nio.SocketSessionConfig;
-import javax.net.ssl.SSLContext;
-import java.net.InetSocketAddress;
-import java.security.GeneralSecurityException;
-import java.util.concurrent.ExecutorService;
/**
* Main class to use for sending asynchronous HTTP requests to servers.
@@ -79,13 +86,13 @@
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
/** The connector. */
- private SocketConnector connector;
+ private final SocketConnector connector;
/** The thread pool. */
private final ExecutorService threadPool;
/** The HttpIoHandler handler. */
- private HttpIoHandler handler = new HttpIoHandler();
+ private final HttpIoHandler handler;
/** The ssl filter. */
private SSLFilter sslFilter;
@@ -116,7 +123,7 @@
/**
* Checks if is reuse address.
- *
+ *
* @return true, if is reuse address
*/
public boolean isReuseAddress() {
@@ -125,7 +132,7 @@
/**
* Sets the reuse address.
- *
+ *
* @param reuseAddress the new reuse address
*/
public void setReuseAddress(boolean reuseAddress) {
@@ -134,7 +141,7 @@
/**
* Gets the receive buffer size.
- *
+ *
* @return the receive buffer size
*/
public int getReceiveBufferSize() {
@@ -143,7 +150,7 @@
/**
* Sets the receive buffer size.
- *
+ *
* @param receiveBufferSize the new receive buffer size
*/
public void setReceiveBufferSize(int receiveBufferSize) {
@@ -152,7 +159,7 @@
/**
* Gets the send buffer size.
- *
+ *
* @return the send buffer size
*/
public int getSendBufferSize() {
@@ -161,7 +168,7 @@
/**
* Sets the send buffer size.
- *
+ *
* @param sendBufferSize the new send buffer size
*/
public void setSendBufferSize(int sendBufferSize) {
@@ -170,7 +177,7 @@
/**
* Gets the traffic class.
- *
+ *
* @return the traffic class
*/
public int getTrafficClass() {
@@ -179,7 +186,7 @@
/**
* Sets the traffic class.
- *
+ *
* @param trafficClass the new traffic class
*/
public void setTrafficClass(int trafficClass) {
@@ -188,7 +195,7 @@
/**
* Checks if is keep alive.
- *
+ *
* @return true, if is keep alive
*/
public boolean isKeepAlive() {
@@ -197,7 +204,7 @@
/**
* Sets the keep alive.
- *
+ *
* @param keepAlive the new keep alive
*/
public void setKeepAlive(boolean keepAlive) {
@@ -206,7 +213,7 @@
/**
* Checks if is oob inline.
- *
+ *
* @return true, if is oob inline
*/
public boolean isOobInline() {
@@ -215,7 +222,7 @@
/**
* Sets the oob inline.
- *
+ *
* @param oobInline the new oob inline
*/
public void setOobInline(boolean oobInline) {
@@ -224,7 +231,7 @@
/**
* Gets the so linger.
- *
+ *
* @return the so linger
*/
public int getSoLinger() {
@@ -233,7 +240,7 @@
/**
* Sets the so linger.
- *
+ *
* @param soLinger the new so linger
*/
public void setSoLinger(int soLinger) {
@@ -242,7 +249,7 @@
/**
* Checks if is tcp no delay.
- *
+ *
* @return true, if is tcp no delay
*/
public boolean isTcpNoDelay() {
@@ -251,7 +258,7 @@
/**
* Sets the tcp no delay.
- *
+ *
* @param tcpNoDelay the new tcp no delay
*/
public void setTcpNoDelay(boolean tcpNoDelay) {
@@ -263,41 +270,46 @@
* use in one-off connections.
*/
public AsyncHttpClient() {
- this(DEFAULT_CONNECTION_TIMEOUT, null);
+ this(DEFAULT_CONNECTION_TIMEOUT, null, null);
}
/**
* Instantiates a new AsyncHttpClient. This will take a thread pool (ExecutorService) to use
* for processing connections.
- *
- * @param executor the executor
+ *
+ * @param executor the executor
+ * @param scheduler the scheduler to use to track timeouts
*/
- public AsyncHttpClient(ExecutorService executor) {
- this(DEFAULT_CONNECTION_TIMEOUT, executor);
+ public AsyncHttpClient(ExecutorService executor, ScheduledExecutorService scheduler) {
+ this(DEFAULT_CONNECTION_TIMEOUT, executor, scheduler);
}
/**
* Instantiates a new AsyncHttpClient. Uses a single thread model by default and allows you to specify
* a connection timeout.
- *
+ *
* @param connectionTimeout the connection timeout in milliseconds.
*/
public AsyncHttpClient(int connectionTimeout) {
- this(connectionTimeout, null);
+ this(connectionTimeout, null, null);
}
/**
* Instantiates a new AsyncHttpClient. Allows you to specify a connection timeout and an ExecutorService.
- *
+ *
* @param connectionTimeout the connection timeout in milliseconds.
* @param executor the ExceutorService to use to process connections.
+ * @param scheduler the scheduler to use to track timeouts
*/
- public AsyncHttpClient(int connectionTimeout, ExecutorService executor) {
+ public AsyncHttpClient(int connectionTimeout, ExecutorService executor, ScheduledExecutorService scheduler) {
this.connectionTimeout = connectionTimeout;
threadPool = executor;
- handler = new HttpIoHandler();
+ if (scheduler == null)
+ handler = new HttpIoHandler();
+ else
+ handler = new HttpIoHandler(scheduler);
if (threadPool == null)
connector = new SocketConnector();
@@ -311,9 +323,8 @@
/**
* Sends a request.
- *
+ *
* @param message the <code>HttpRequestMessage</code> to send to the remote server.
- *
* @see HttpRequestMessage
*/
public void sendRequest(HttpRequestMessage message) {
@@ -328,7 +339,7 @@
/**
* Gets the connection timeout.
- *
+ *
* @return the connection timeout in milliseconds
*/
public int getConnectionTimeout() {
@@ -337,7 +348,7 @@
/**
* Sets the connection timeout.
- *
+ *
* @param connectionTimeout the new connection timeout in milliseconds
*/
public void setConnectionTimeout(int connectionTimeout) {
@@ -377,15 +388,19 @@
*/
class FutureListener implements IoFutureListener {
- /** The request. */
+ /**
+ * The request.
+ */
HttpRequestMessage request;
- /** The client. */
+ /**
+ * The client.
+ */
AsyncHttpClient client;
/**
* Instantiates a new future listener for a connection.
- *
+ *
* @param client the <code>AsyncHttpClient</code> client object
* @param request the <code>HttpRequestMessage</code> request that is to be sent.
*/
@@ -397,11 +412,11 @@
/**
* Event notification that the conection has completed, either by a successful connection or
* by an error.
- *
+ *
* @param future the {@link org.apache.mina.common.IoFuture} representing the <code>ConnectFuture</code>.
- *
* @see org.apache.mina.common.IoFutureListener#operationComplete(org.apache.mina.common.IoFuture)
*/
+ @SuppressWarnings({"UnusedDeclaration"})
public void operationComplete(IoFuture future) {
ConnectFuture connFuture = (ConnectFuture) future;
if (connFuture.isConnected()) {
@@ -457,9 +472,8 @@
/**
* Creates the client ssl context.
- *
+ *
* @return the SSL context
- *
* @throws GeneralSecurityException the general security exception
*/
private SSLContext createClientSSLContext() throws GeneralSecurityException {
Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java?rev=601453&r1=601452&r2=601453&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java Wed Dec 5 10:06:11 2007
@@ -25,6 +25,9 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+
import org.apache.ahc.AsyncHttpClient;
import org.apache.ahc.AsyncHttpClientCallback;
import org.apache.ahc.auth.AuthChallengeParser;
@@ -32,8 +35,7 @@
import org.apache.ahc.auth.AuthScheme;
import org.apache.ahc.auth.AuthState;
import org.apache.ahc.util.NameValuePair;
-import org.apache.mina.common.IoHandlerAdapter;
-import org.apache.mina.common.IoSession;
+
/**
* The Class HttpIoHandler. Implements the MINA IoHandler interface as the primary
@@ -59,13 +61,22 @@
/**
* The scheduler service to handle timeouts.
*/
- private ScheduledExecutorService scheduler;
+ private final ScheduledExecutorService scheduler;
/**
- * Instantiates a new HttpIoHandler.
+ * Instantiates a new HttpIoHandler with a new a single-threaded executor.
*/
public HttpIoHandler() {
- scheduler = Executors.newSingleThreadScheduledExecutor();
+ this(Executors.newSingleThreadScheduledExecutor());
+ }
+
+ /**
+ * Instantiates a new HttpIoHandler with the supplied scheduler.
+ *
+ * @param scheduler the scheduler to use to track timeouts
+ */
+ public HttpIoHandler(ScheduledExecutorService scheduler) {
+ this.scheduler = scheduler;
}
/**
@@ -90,18 +101,20 @@
* @param object the {@link HttpResponseMessage} object
* @see org.apache.mina.common.IoHandlerAdapter#messageReceived(org.apache.mina.common.IoSession,java.lang.Object)
*/
+ @SuppressWarnings({"UnusedDeclaration"})
public void messageReceived(IoSession ioSession, Object object) throws Exception {
- HttpResponseMessage response = (HttpResponseMessage)object;
+ HttpResponseMessage response = (HttpResponseMessage) object;
- HttpRequestMessage request = (HttpRequestMessage)ioSession.getAttribute(CURRENT_REQUEST);
+ HttpRequestMessage request = (HttpRequestMessage) ioSession.getAttribute(CURRENT_REQUEST);
//Check if we are to handle redirects
if ((response.getStatusCode() == 301
- || response.getStatusCode() == 302
- || response.getStatusCode() == 307)
- && request.isFollowRedirects()) {
- AsyncHttpClient client = (AsyncHttpClient)ioSession.getAttachment();
+ || response.getStatusCode() == 302
+ || response.getStatusCode() == 307)
+ && request.isFollowRedirects())
+ {
+ AsyncHttpClient client = (AsyncHttpClient) ioSession.getAttachment();
//Change the request url to the redirect
String query = request.getUrl().getQuery();
@@ -119,7 +132,7 @@
if (response.getChallenges().size() > 0) {
for (NameValuePair nvp : response.getChallenges()) {
AuthState state = request.getAuthState();
- if (state == null){
+ if (state == null) {
String id = AuthChallengeParser.extractScheme(nvp.getValue());
AuthScheme authScheme = AuthPolicy.getAuthScheme(id);
state = new AuthState();
@@ -129,14 +142,14 @@
}
}
- AsyncHttpClient client = (AsyncHttpClient)ioSession.getAttachment();
+ AsyncHttpClient client = (AsyncHttpClient) ioSession.getAttachment();
//Authenticate
int authCount = request.getAuthCount() + 1;
- if (authCount <= 3){
+ if (authCount <= 3) {
request.setAuthCount(authCount);
client.sendRequest(request);
-
+
//Close the current session since we are done with it
ioSession.close();
return;
@@ -161,7 +174,7 @@
//Clean up if any in-proccess decoding was occurring
ioSession.removeAttribute(CURRENT_RESPONSE);
- HttpRequestMessage request = (HttpRequestMessage)ioSession.getAttribute(CURRENT_REQUEST);
+ HttpRequestMessage request = (HttpRequestMessage) ioSession.getAttribute(CURRENT_REQUEST);
cancelTasks(request);
AsyncHttpClientCallback callback = request.getCallback();
@@ -180,7 +193,7 @@
public void sessionClosed(IoSession ioSession) throws Exception {
//Clean up if any in-proccess decoding was occurring
ioSession.removeAttribute(CURRENT_RESPONSE);
- HttpRequestMessage request = (HttpRequestMessage)ioSession.getAttribute(CURRENT_REQUEST);
+ HttpRequestMessage request = (HttpRequestMessage) ioSession.getAttribute(CURRENT_REQUEST);
cancelTasks(request);
AsyncHttpClientCallback callback = request.getCallback();
callback.onClosed();
@@ -195,7 +208,7 @@
* @see org.apache.mina.common.IoHandlerAdapter#messageSent(org.apache.mina.common.IoSession,java.lang.Object)
*/
public void messageSent(IoSession ioSession, Object object) throws Exception {
- HttpRequestMessage msg = (HttpRequestMessage)object;
+ HttpRequestMessage msg = (HttpRequestMessage) object;
//Start the timeout timer now if a timeout is needed and there is not one already in effect for this request
if (msg.getTimeOut() > 0 && msg.getTimeoutHandle() == null) {
@@ -248,7 +261,7 @@
* @see java.lang.Runnable#run()
*/
public void run() {
- HttpRequestMessage request = (HttpRequestMessage)sess.getAttribute(CURRENT_REQUEST);
+ HttpRequestMessage request = (HttpRequestMessage) sess.getAttribute(CURRENT_REQUEST);
AsyncHttpClientCallback callback = request.getCallback();
callback.onTimeout();
//Close the session, its no good since the server is timing out