hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r468682 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: examples/org/apache/http/nio/examples/ main/java/org/apache/http/nio/ main/java/org/apache/http/nio/handler/ main/java/org/apache/http/nio/impl/ main/java/org/apache/http/n...
Date Sat, 28 Oct 2006 12:10:34 GMT
Author: olegk
Date: Sat Oct 28 05:10:33 2006
New Revision: 468682

URL: http://svn.apache.org/viewvc?view=rev&rev=468682
Log:
* First take at the client side implementation of the new event-driven HTTP transport API
* Some minor API refinements

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpClient.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultClientIOEventDispatch.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpClientConnection.java
  (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpClient.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/SessionRequest.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpClientHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpServiceHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/IOSessionImpl.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionRequestImpl.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/codecs/HttpMessageParser.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpClient.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpClient.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpClient.java
Sat Oct 28 05:10:33 2006
@@ -49,21 +49,27 @@
             .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
             .setParameter(HttpProtocolParams.USER_AGENT, "Jakarta-HttpComponents-NIO/1.1");
 
-        final IOEventDispatch ioEventDispatch = new DefaultIoEventDispatch(params);
+        final IOEventDispatch ioEventDispatch = new MyIOEventDispatch(params);
         final IOReactor ioReactor = new DefaultIOReactor(params);
 
-        SessionRequestCallback sessionReqCallback = new DefaultSessionRequestCallback(params);
+        SessionRequestCallback sessionReqCallback = new MySessionRequestCallback(params);
         
         SessionRequest req1 = ioReactor.connect(
-                new InetSocketAddress("www.yahoo.com", 80), null);
+                new InetSocketAddress("www.yahoo.com", 80), 
+                null, 
+                new HttpHost("www.yahoo.com"));
         req1.setCallback(sessionReqCallback);
         
         SessionRequest req2 = ioReactor.connect(
-                new InetSocketAddress("www.google.com", 80), null);
+                new InetSocketAddress("www.google.com", 80), 
+                null,
+                new HttpHost("www.google.ch"));
         req2.setCallback(sessionReqCallback);
         
         SessionRequest req3 = ioReactor.connect(
-                new InetSocketAddress("www.apache.org", 80), null);
+                new InetSocketAddress("www.apache.org", 80), 
+                null,
+                new HttpHost("www.apache.org"));
         req3.setCallback(sessionReqCallback);
         
         Thread ioThread = new Thread(new Runnable() {
@@ -94,11 +100,11 @@
     private static final String TARGET = "TARGET";
     private static final String WORKER = "WORKER";
     
-    static class DefaultSessionRequestCallback implements SessionRequestCallback {
+    static class MySessionRequestCallback implements SessionRequestCallback {
 
         private final HttpParams params;
         
-        public DefaultSessionRequestCallback(final HttpParams params) {
+        public MySessionRequestCallback(final HttpParams params) {
             super();
             if (params == null) {
                 throw new IllegalArgumentException("HTTP parameters may nor be null");
@@ -108,9 +114,7 @@
         
         public void completed(final SessionRequest request) {
             IOSession session = request.getSession();
-            InetSocketAddress address = (InetSocketAddress) request.getRemoteAddress();
-            
-            HttpHost targetHost = new HttpHost(address.getHostName(), address.getPort());

+            HttpHost targetHost = (HttpHost) request.getAttachment(); 
             
             AsyncHttpClientConnection conn = new AsyncHttpClientConnection(session, this.params);

             session.setAttribute(CONNECTION, conn);
@@ -133,9 +137,9 @@
 
     }    
     
-    static class DefaultIoEventDispatch implements IOEventDispatch {
+    static class MyIOEventDispatch implements IOEventDispatch {
 
-        public DefaultIoEventDispatch(final HttpParams params) {
+        public MyIOEventDispatch(final HttpParams params) {
             super();
         }
         

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpClient.java?view=auto&rev=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpClient.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpClient.java
Sat Oct 28 05:10:33 2006
@@ -0,0 +1,234 @@
+package org.apache.http.nio.examples;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
+
+import org.apache.http.ConnectionReuseStrategy;
+import org.apache.http.HttpConnection;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestFactory;
+import org.apache.http.HttpResponse;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpParams;
+import org.apache.http.impl.DefaultHttpRequestFactory;
+import org.apache.http.nio.IOEventDispatch;
+import org.apache.http.nio.IOReactor;
+import org.apache.http.nio.SessionRequest;
+import org.apache.http.nio.handler.ContentDecoder;
+import org.apache.http.nio.handler.ContentEncoder;
+import org.apache.http.nio.handler.NHttpClientConnection;
+import org.apache.http.nio.handler.NHttpClientHandler;
+import org.apache.http.nio.impl.DefaultIOReactor;
+import org.apache.http.nio.impl.handler.DefaultClientIOEventDispatch;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpExecutionContext;
+import org.apache.http.protocol.HttpProcessor;
+import org.apache.http.protocol.RequestConnControl;
+import org.apache.http.protocol.RequestContent;
+import org.apache.http.protocol.RequestExpectContinue;
+import org.apache.http.protocol.RequestTargetHost;
+import org.apache.http.protocol.RequestUserAgent;
+import org.apache.http.util.EntityUtils;
+
+public class NHttpClient {
+
+    public static void main(String[] args) throws Exception {
+        HttpParams params = new DefaultHttpParams(null);
+        params
+            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000)
+            .setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000)
+            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+            .setParameter(HttpProtocolParams.USER_AGENT, "Jakarta-HttpComponents-NIO/1.1");
+
+        IOReactor ioReactor = new DefaultIOReactor(params);
+
+        SessionRequest[] reqs = new SessionRequest[3];
+        reqs[0] = ioReactor.connect(
+                new InetSocketAddress("www.yahoo.com", 80), 
+                null, 
+                new HttpHost("www.yahoo.com"));
+        reqs[1] = ioReactor.connect(
+                new InetSocketAddress("www.google.com", 80), 
+                null,
+                new HttpHost("www.google.ch"));
+        reqs[2] = ioReactor.connect(
+                new InetSocketAddress("www.apache.org", 80), 
+                null,
+                new HttpHost("www.apache.org"));
+
+        NHttpClientHandler handler = new MyNHttpClientHandler(reqs, params);
+        IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler, params);
+        
+        try {
+            ioReactor.execute(ioEventDispatch);
+        } catch (InterruptedIOException ex) {
+            System.err.println("Interrupted");
+        } catch (IOException e) {
+            System.err.println("I/O error: " + e.getMessage());
+        }
+        System.out.println("Shutdown");
+    }
+    
+    static class MyNHttpClientHandler implements NHttpClientHandler {
+
+        private final SessionRequest[] reqs;
+        private final HttpParams params;
+        private final HttpRequestFactory requestFactory; 
+        private final HttpProcessor httpProcessor;
+        private final ByteBuffer inbuf;
+        private final ConnectionReuseStrategy connStrategy;
+        
+        private int connCount = 0;
+        
+        public MyNHttpClientHandler(final SessionRequest[] reqs, final HttpParams params)
{
+            super();
+            this.reqs = reqs;
+            this.params = params;
+            this.requestFactory = new DefaultHttpRequestFactory();
+            BasicHttpProcessor httpproc = new BasicHttpProcessor();
+            httpproc.addInterceptor(new RequestContent());
+            httpproc.addInterceptor(new RequestTargetHost());
+            httpproc.addInterceptor(new RequestConnControl());
+            httpproc.addInterceptor(new RequestUserAgent());
+            httpproc.addInterceptor(new RequestExpectContinue());
+            this.httpProcessor = httpproc;
+            this.inbuf = ByteBuffer.allocateDirect(2048);
+            this.connStrategy = new DefaultConnectionReuseStrategy();
+        }
+        
+        private void shutdownConnection(final HttpConnection conn) {
+            try {
+                conn.shutdown();
+            } catch (IOException ignore) {
+            }
+        }
+        
+        public void connected(final NHttpClientConnection conn, final Object attachment)
{
+            try {
+                HttpContext context = conn.getContext();
+                
+                HttpHost targetHost = (HttpHost) attachment;
+                
+                context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+                context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, targetHost);
+                
+                HttpRequest request = this.requestFactory.newHttpRequest("GET", "/");
+                request.getParams().setDefaults(this.params);
+                
+                this.httpProcessor.process(request, context);
+                
+                conn.submitRequest(request);
+
+                context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
+            
+            } catch (IOException ex) {
+                shutdownConnection(conn);
+                System.err.println("I/O error: " + ex.getMessage());
+            } catch (HttpException ex) {
+                shutdownConnection(conn);
+                System.err.println("Unexpected HTTP protocol error: " + ex.getMessage());
+            }
+        }
+
+        public void closed(final NHttpClientConnection conn) {
+            System.out.println("Connection closed");
+            this.connCount++;
+            if (this.connCount >= this.reqs.length) {
+                System.exit(0);
+            }
+        }
+
+        public void exception(final NHttpClientConnection conn, final HttpException ex) {
+            System.err.println("HTTP protocol error: " + ex.getMessage());
+            shutdownConnection(conn);
+        }
+
+        public void exception(final NHttpClientConnection conn, final IOException ex) {
+            System.err.println("I/O error: " + ex.getMessage());
+            shutdownConnection(conn);
+        }
+
+        public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder)
{
+            HttpResponse response = conn.getHttpResponse();
+            HttpContext context = conn.getContext();
+            HttpHost targetHost = (HttpHost) context
+                .getAttribute(HttpExecutionContext.HTTP_TARGET_HOST);
+            WritableByteChannel channel = (WritableByteChannel) context
+                .getAttribute("in-channel");
+
+            try {
+                while (decoder.read(this.inbuf) > 0) {
+                    this.inbuf.flip();
+                    channel.write(this.inbuf);
+                    this.inbuf.compact();
+                }
+                if (decoder.isCompleted()) {
+                    HttpEntity entity = response.getEntity();
+                    
+                    ByteArrayOutputStream bytestream = (ByteArrayOutputStream) context
+                        .getAttribute("in-buffer");
+                    byte[] content = bytestream.toByteArray();
+                    
+                    String charset = EntityUtils.getContentCharSet(entity);
+                    if (charset == null) {
+                        charset = HTTP.DEFAULT_CONTENT_CHARSET;
+                    }
+                    
+                    System.out.println("--------------");
+                    System.out.println("Target: " + targetHost);
+                    System.out.println("--------------");
+                    System.out.println(response.getStatusLine());
+                    System.out.println("--------------");
+                    System.out.println(new String(content, charset));
+                    System.out.println("--------------");
+
+                    if (!this.connStrategy.keepAlive(response, context)) {
+                        conn.close();
+                    }
+                }
+                
+            } catch (IOException ex) {
+                shutdownConnection(conn);
+                System.err.println("I/O error: " + ex.getMessage());
+            }
+        }
+
+        public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder)
{
+        }
+
+        public void responseReceived(final NHttpClientConnection conn) {
+            HttpResponse response = conn.getHttpResponse();
+            
+            if (response.getStatusLine().getStatusCode() >= 200) {
+                ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
+                WritableByteChannel channel = Channels.newChannel(bytestream);
+                
+                HttpContext context = conn.getContext();
+                context.setAttribute("in-buffer", bytestream);
+                context.setAttribute("in-channel", channel);
+            }
+        }
+
+        public void timeout(final NHttpClientConnection conn) {
+            System.err.println("Timeout");
+            shutdownConnection(conn);
+        }
+        
+    } 
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpClient.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpClient.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
Sat Oct 28 05:10:33 2006
@@ -65,9 +65,11 @@
             .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
             .setParameter(HttpProtocolParams.ORIGIN_SERVER, "Jakarta-HttpComponents-NIO/1.1");
 
-        IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
-                new MyNHttpServiceHandler(args[0]), params);
         IOReactor ioReactor = new DefaultIOReactor(params);
+
+        NHttpServiceHandler handler = new MyNHttpServiceHandler(args[0], params);
+        IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(handler, params);
+        
         try {
             ioReactor.listen(new InetSocketAddress(8080));
             ioReactor.execute(ioEventDispatch);
@@ -82,15 +84,17 @@
     public static class MyNHttpServiceHandler implements NHttpServiceHandler {
 
         private final String docRoot;
+        private final HttpParams params;
         private final HttpResponseFactory responseFactory;
         private final ByteBuffer inbuf;
         private final ByteBuffer outbuf;
         private final HttpProcessor httpProcessor;
         private final ConnectionReuseStrategy connStrategy;
         
-        public MyNHttpServiceHandler(final String docRoot) {
+        public MyNHttpServiceHandler(final String docRoot, final HttpParams params) {
             super();
             this.docRoot = docRoot;
+            this.params = params;
             this.responseFactory = new DefaultHttpResponseFactory();
             this.inbuf = ByteBuffer.allocateDirect(2048);
             this.outbuf = ByteBuffer.allocateDirect(2048);
@@ -131,11 +135,11 @@
                 this.httpProcessor.process(response, conn.getContext());
                 conn.submitResponse(response);
             } catch (HttpException ex) {
-                ex.printStackTrace();
                 shutdownConnection(conn);
+                System.err.println("Unexpected HTTP protocol error: " + ex.getMessage());
             } catch (IOException ex) {
-                ex.printStackTrace();
                 shutdownConnection(conn);
+                System.err.println("I/O error: " + ex.getMessage());
             }
         }
 
@@ -143,6 +147,7 @@
             HttpRequest request = conn.getHttpRequest();
             HttpVersion ver = request.getRequestLine().getHttpVersion();
             HttpResponse response =  this.responseFactory.newHttpResponse(ver, 200);
+            response.getParams().setDefaults(this.params);
 
             String target = request.getRequestLine().getUri();
             try {
@@ -196,8 +201,8 @@
                     try {
                         conn.submitResponse(ack);
                     } catch (HttpException ex) {
-                        ex.printStackTrace();
                         shutdownConnection(conn);
+                        System.err.println("Unexpected HTTP protocol error: " + ex.getMessage());
                         return;
                     }
                 }
@@ -217,6 +222,10 @@
             }
         }
 
+        public void connected(final NHttpServerConnection conn) {
+            System.out.println("New incoming connection");
+        }
+
         public void closed(final NHttpServerConnection conn) {
             System.out.println("Connection closed");
         }
@@ -269,7 +278,7 @@
                 
             } catch (IOException ex) {
                 shutdownConnection(conn);
-                ex.printStackTrace();
+                System.err.println("I/O error: " + ex.getMessage());
             }
         }
 
@@ -298,7 +307,7 @@
             
             } catch (IOException ex) {
                 shutdownConnection(conn);
-                ex.printStackTrace();
+                System.err.println("I/O error: " + ex.getMessage());
             }
 
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
Sat Oct 28 05:10:33 2006
@@ -40,7 +40,8 @@
     void shutdown() 
         throws IOException;
     
-    SessionRequest connect(SocketAddress remoteAddress, SocketAddress localAddress) 
+    SessionRequest connect(SocketAddress remoteAddress, 
+            SocketAddress localAddress, Object attachment) 
         throws IOException;
         
     void listen(SocketAddress address) 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
Sat Oct 28 05:10:33 2006
@@ -29,12 +29,17 @@
 
 package org.apache.http.nio;
 
+import java.net.SocketAddress;
 import java.nio.channels.ByteChannel;
 
 public interface IOSession {
 
     ByteChannel channel();
     
+    SocketAddress getRemoteAddress();    
+    
+    SocketAddress getLocalAddress();    
+
     int getEventMask();
     
     void setEventMask(int ops);

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/SessionRequest.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/SessionRequest.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/SessionRequest.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/SessionRequest.java
Sat Oct 28 05:10:33 2006
@@ -34,7 +34,13 @@
 
 public interface SessionRequest {
 
-    public SocketAddress getRemoteAddress();
+    public static final String ATTRIB_KEY = "http.connection-request";
+
+    SocketAddress getRemoteAddress();
+    
+    SocketAddress getLocalAddress();
+    
+    Object getAttachment();
     
     boolean isCompleted();
     

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpClientHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpClientHandler.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpClientHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpClientHandler.java
Sat Oct 28 05:10:33 2006
@@ -41,6 +41,15 @@
 public interface NHttpClientHandler {
 
     /**
+     * Triggered when a new outgoing connection is created.
+     * 
+     * @param conn closed HTTP connection.
+     * @param attachment an arbitrary object that was attached to the
+     *  session request
+     */
+    void connected(NHttpClientConnection conn, Object attachment);
+    
+    /**
      * Triggered when an HTTP response is received. The connection
      * passed as a parameter to this method is guaranteed to return
      * a valid HTTP response object.

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpServiceHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpServiceHandler.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpServiceHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/handler/NHttpServiceHandler.java
Sat Oct 28 05:10:33 2006
@@ -41,6 +41,13 @@
 public interface NHttpServiceHandler {
 
     /**
+     * Triggered when a new incoming connection is created.
+     * 
+     * @param conn closed HTTP connection.
+     */
+    void connected(NHttpServerConnection conn);
+    
+    /**
      * Triggered when a new HTTP request is received. The connection
      * passed as a parameter to this method is guaranteed to return
      * a valid HTTP request object.

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
Sat Oct 28 05:10:33 2006
@@ -166,6 +166,9 @@
                 SessionHandle handle = new SessionHandle(session); 
                 key.attach(handle);
                 
+                // Add the session request to the session context
+                session.setAttribute(SessionRequest.ATTRIB_KEY, sessionRequest);
+                
                 // Fire the request completion notification first
                 sessionRequest.completed(session);
                 
@@ -286,7 +289,8 @@
 
     public SessionRequest connect(
             final SocketAddress remoteAddress, 
-            final SocketAddress localAddress) throws IOException {
+            final SocketAddress localAddress,
+            final Object attachment) throws IOException {
         SocketChannel socketChannel = SocketChannel.open();
         socketChannel.configureBlocking(false);
         if (localAddress != null) {
@@ -295,7 +299,8 @@
         socketChannel.connect(remoteAddress);
         SelectionKey key = socketChannel.register(this.selector, SelectionKey.OP_CONNECT);
         
-        SessionRequestImpl sessionRequest = new SessionRequestImpl(remoteAddress, key);
+        SessionRequestImpl sessionRequest = new SessionRequestImpl(
+                remoteAddress, localAddress, attachment, key);
         sessionRequest.setConnectTimeout(HttpConnectionParams.getConnectionTimeout(this.params));
 
         SessionRequestHandle requestHandle = new SessionRequestHandle(sessionRequest); 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/IOSessionImpl.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/IOSessionImpl.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/IOSessionImpl.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/IOSessionImpl.java
Sat Oct 28 05:10:33 2006
@@ -30,8 +30,11 @@
 package org.apache.http.nio.impl;
 
 import java.io.IOException;
+import java.net.SocketAddress;
 import java.nio.channels.ByteChannel;
+import java.nio.channels.Channel;
 import java.nio.channels.SelectionKey;
+import java.nio.channels.SocketChannel;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -63,6 +66,24 @@
         return (ByteChannel) this.key.channel();
     }
     
+    public SocketAddress getLocalAddress() {
+        Channel channel = this.key.channel();
+        if (channel instanceof SocketChannel) {
+            return ((SocketChannel)channel).socket().getLocalSocketAddress();
+        } else {
+            return null;
+        }
+    }
+
+    public SocketAddress getRemoteAddress() {
+        Channel channel = this.key.channel();
+        if (channel instanceof SocketChannel) {
+            return ((SocketChannel)channel).socket().getRemoteSocketAddress();
+        } else {
+            return null;
+        }
+    }
+
     public int getEventMask() {
         return this.key.interestOps();
     }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionRequestImpl.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionRequestImpl.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionRequestImpl.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionRequestImpl.java
Sat Oct 28 05:10:33 2006
@@ -43,13 +43,19 @@
 
     private final SelectionKey key;
     private final SocketAddress remoteAddress;
+    private final SocketAddress localAddress;
+    private final Object attachment;
     
     private int connectTimeout;
     private SessionRequestCallback callback;
     private IOSession session = null;
     private IOException exception = null;
     
-    public SessionRequestImpl(final SocketAddress remoteAddress, SelectionKey key) {
+    public SessionRequestImpl(
+            final SocketAddress remoteAddress,
+            final SocketAddress localAddress,
+            final Object attachment,
+            final SelectionKey key) {
         super();
         if (remoteAddress == null) {
             throw new IllegalArgumentException("Remote address may not be null");
@@ -58,12 +64,22 @@
             throw new IllegalArgumentException("Selection key may not be null");
         }
         this.remoteAddress = remoteAddress;
+        this.localAddress = localAddress;
+        this.attachment = attachment;
         this.key = key;
         this.connectTimeout = 0;
     }
     
     public SocketAddress getRemoteAddress() {
         return this.remoteAddress;
+    }
+    
+    public SocketAddress getLocalAddress() {
+        return this.localAddress;
+    }
+    
+    public Object getAttachment() {
+        return this.attachment;
     }
     
     public boolean isCompleted() {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/codecs/HttpMessageParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/codecs/HttpMessageParser.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/codecs/HttpMessageParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/codecs/HttpMessageParser.java
Sat Oct 28 05:10:33 2006
@@ -74,11 +74,12 @@
         this.message = null;
     }
     
-    public void fillBuffer(final ReadableByteChannel channel) throws IOException {
+    public int fillBuffer(final ReadableByteChannel channel) throws IOException {
         int bytesRead = this.buffer.fill(channel);
         if (bytesRead == -1) {
             this.endOfStream = true;
         }
+        return bytesRead;
     }
     
     protected abstract HttpMessage createMessage(CharArrayBuffer buffer) 

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultClientIOEventDispatch.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultClientIOEventDispatch.java?view=auto&rev=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultClientIOEventDispatch.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultClientIOEventDispatch.java
Sat Oct 28 05:10:33 2006
@@ -0,0 +1,65 @@
+package org.apache.http.nio.impl.handler;
+
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.nio.IOEventDispatch;
+import org.apache.http.nio.IOSession;
+import org.apache.http.nio.SessionRequest;
+import org.apache.http.nio.handler.NHttpClientHandler;
+import org.apache.http.params.HttpParams;
+
+public class DefaultClientIOEventDispatch implements IOEventDispatch {
+
+    private static final String NHTTP_CONN = "NHTTP_CONN";
+    
+    private final NHttpClientHandler handler;
+    private final HttpParams params;
+    
+    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.handler = handler;
+        this.params = params;
+    }
+    
+    public void connected(final IOSession session) {
+        DefaultNHttpClientConnection conn = new DefaultNHttpClientConnection(
+                session, 
+                new DefaultHttpResponseFactory(),
+                this.params); 
+        session.setAttribute(NHTTP_CONN, conn);
+        
+        SessionRequest request = (SessionRequest) session.getAttribute(SessionRequest.ATTRIB_KEY);
+        
+        this.handler.connected(conn, request.getAttachment());
+    }
+
+    public void disconnected(final IOSession session) {
+        DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session.getAttribute(
+                NHTTP_CONN);
+        this.handler.closed(conn);
+    }
+
+    public void inputReady(final IOSession session) {
+        DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session.getAttribute(
+                NHTTP_CONN);
+        conn.consumeInput(this.handler);
+    }
+
+    public void outputReady(final IOSession session) {
+        DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session.getAttribute(
+                NHTTP_CONN);
+        conn.produceOutput(this.handler);
+    }
+
+    public void timeout(final IOSession session) {
+        DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session.getAttribute(
+                NHTTP_CONN);
+        this.handler.timeout(conn);
+    }
+
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultClientIOEventDispatch.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultClientIOEventDispatch.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpClientConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpClientConnection.java?view=auto&rev=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpClientConnection.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpClientConnection.java
Sat Oct 28 05:10:33 2006
@@ -0,0 +1,178 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.impl.handler;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseFactory;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.message.BasicRequestLine;
+import org.apache.http.message.BufferedHeader;
+import org.apache.http.nio.EventMask;
+import org.apache.http.nio.IOSession;
+import org.apache.http.nio.handler.NHttpClientConnection;
+import org.apache.http.nio.handler.NHttpClientHandler;
+import org.apache.http.nio.impl.codecs.HttpResponseParser;
+import org.apache.http.params.HttpParams;
+
+public class DefaultNHttpClientConnection 
+    extends NHttpConnectionBase implements NHttpClientConnection {
+
+    private HttpResponseParser responseParser;
+    
+    public DefaultNHttpClientConnection(
+            final IOSession session,
+            final HttpResponseFactory responseFactory,
+            final HttpParams params) {
+        super(session, params);
+        if (responseFactory == null) {
+            throw new IllegalArgumentException("Response factory may not be null");
+        }
+        this.responseParser = new HttpResponseParser(this.inbuf, responseFactory);
+    }
+
+    private void resetInput() {
+        this.response = null;
+        this.contentDecoder = null;
+        this.responseParser.reset();
+    }
+    
+    private void resetOutput() {
+        this.request = null;
+        this.contentEncoder = null;
+    }
+    
+    public void consumeInput(final NHttpClientHandler handler) {
+        if (this.closed) {
+            this.session.clearEvent(EventMask.READ);
+            return;
+        }
+        try {
+            if (this.response == null) {
+                int bytesRead = this.responseParser.fillBuffer(this.session.channel());
+                this.response = (HttpResponse) this.responseParser.parse(); 
+                if (this.response != null) {
+                    handler.responseReceived(this);
+                    
+                    if (this.response.getStatusLine().getStatusCode() >= 200) {
+                        HttpEntity entity = prepareDecoder(this.response);
+                        this.response.setEntity(entity);
+                    } else {
+                        // Discard the intermediate response
+                        this.response = null;
+                    }
+                }
+                if (bytesRead == -1) {
+                    close();
+                }
+            }
+            if (this.contentDecoder != null) {
+                handler.inputReady(this, this.contentDecoder);
+                if (this.contentDecoder.isCompleted()) {
+                    // Response entity received
+                    // Ready to receive a new response
+                    resetInput();
+                }
+            }
+        } catch (IOException ex) {
+            handler.exception(this, ex);
+        } catch (HttpException ex) {
+            handler.exception(this, ex);
+        }
+    }
+
+    public void produceOutput(final NHttpClientHandler handler) {
+        try {
+            if (this.outbuf.hasData()) {
+                this.outbuf.flush(this.session.channel());
+            }
+            if (!this.outbuf.hasData()) {
+                if (this.closed) {
+                    this.session.close();
+                } else {
+                    if (this.contentEncoder != null) {
+                        handler.outputReady(this, this.contentEncoder);
+                        if (this.contentEncoder.isCompleted()) {
+                            resetOutput();
+                        }
+                    }
+                }
+                if (this.contentEncoder == null) {
+                    this.session.clearEvent(EventMask.WRITE);
+                }
+            }
+        } catch (IOException ex) {
+            handler.exception(this, ex);
+        }
+    }
+    
+    public void submitRequest(final HttpRequest request) throws HttpException {
+        if (request == null) {
+            throw new IllegalArgumentException("HTTP request may not be null");
+        }
+        if (this.request != null) {
+            throw new HttpException("Request already submitted");
+        }
+        this.lineBuffer.clear();
+        BasicRequestLine.format(this.lineBuffer, request.getRequestLine());
+        this.outbuf.writeLine(this.lineBuffer);
+        for (Iterator it = request.headerIterator(); it.hasNext(); ) {
+            Header header = (Header) it.next();
+            if (header instanceof BufferedHeader) {
+                // If the header is backed by a buffer, re-use the buffer
+                this.outbuf.writeLine(((BufferedHeader)header).getBuffer());
+            } else {
+                this.lineBuffer.clear();
+                BasicHeader.format(this.lineBuffer, header);
+                this.outbuf.writeLine(this.lineBuffer);
+            }
+        }
+        this.lineBuffer.clear();
+        this.outbuf.writeLine(this.lineBuffer);
+
+        if (request instanceof HttpEntityEnclosingRequest) {
+            prepareEncoder(request);
+        }
+        this.request = request;
+        this.session.setEvent(EventMask.WRITE);
+    }
+
+    public boolean isRequestSubmitted() {
+        return this.request != null;
+    }
+
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpClientConnection.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpClientConnection.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
Sat Oct 28 05:10:33 2006
@@ -83,7 +83,7 @@
         }
         try {
             if (this.request == null) {
-                this.requestParser.fillBuffer(this.session.channel());
+                int bytesRead = this.requestParser.fillBuffer(this.session.channel());
                 this.request = (HttpRequest) this.requestParser.parse(); 
                 if (this.request != null) {
                     handler.requestReceived(this);
@@ -97,6 +97,9 @@
                         resetInput();
                     }
                 }
+                if (bytesRead == -1) {
+                    close();
+                }
             }
             if (this.contentDecoder != null) {
                 handler.inputReady(this, this.contentDecoder);
@@ -138,10 +141,6 @@
         }
     }
     
-    public boolean isResponseCommitted() {
-        return this.response != null;
-    }
-
     public void submitResponse(final HttpResponse response) throws HttpException {
         if (response == null) {
             throw new IllegalArgumentException("HTTP response may not be null");

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java?view=diff&rev=468682&r1=468681&r2=468682
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
Sat Oct 28 05:10:33 2006
@@ -31,6 +31,7 @@
                 new DefaultHttpRequestFactory(),
                 this.params); 
         session.setAttribute(NHTTP_CONN, conn);
+        this.handler.connected(conn);
     }
 
     public void disconnected(final IOSession session) {



Mime
View raw message