hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r507209 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol: BufferingHttpClientHandler.java BufferingHttpServiceHandler.java ThrottlingHttpServiceHandler.java
Date Tue, 13 Feb 2007 20:51:07 GMT
Author: olegk
Date: Tue Feb 13 12:51:03 2007
New Revision: 507209

URL: http://svn.apache.org/viewvc?view=rev&rev=507209
Log:
Refactored client and server side service handlers to keep all the connection state details
in one internal state object

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java?view=diff&rev=507209&r1=507208&r2=507209
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
Tue Feb 13 12:51:03 2007
@@ -68,8 +68,7 @@
  */
 public class BufferingHttpClientHandler implements NHttpClientHandler {
 
-    private static final String IN_BUF = "http.nio.in-buffer";
-    private static final String OUT_BUF = "http.nio.out-buffer";
+    private static final String CONN_STATE = "http.nio.conn-state";
     
     private HttpParams params;
     private HttpProcessor httpProcessor;
@@ -128,11 +127,9 @@
         
         initialize(conn, attachment);
         
-        InputBuffer inbuffer = new InputBuffer(2048); 
-        OutputBuffer outbuffer = new OutputBuffer(2048);
-        
-        context.setAttribute(IN_BUF, inbuffer);
-        context.setAttribute(OUT_BUF, outbuffer);
+        ConnState connState = new ConnState(); 
+
+        context.setAttribute(CONN_STATE, connState);
 
         if (this.eventListener != null) {
             InetAddress address = null;
@@ -172,11 +169,12 @@
     public void requestReady(final NHttpClientConnection conn) {
         HttpContext context = conn.getContext();
 
-        OutputBuffer outbuffer = (OutputBuffer) context.getAttribute(OUT_BUF);
-
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        OutputBuffer buffer = connState.getOutbuffer();
+        
         try {
             
-            submitRequest(conn, outbuffer);                
+            submitRequest(conn, buffer);                
             
         } catch (IOException ex) {
             shutdownConnection(conn);
@@ -194,18 +192,17 @@
     public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder)
{
         HttpContext context = conn.getContext();
 
-        InputBuffer inbuffer = (InputBuffer) context.getAttribute(IN_BUF);
-        OutputBuffer outbuffer = (OutputBuffer) context.getAttribute(OUT_BUF);
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        InputBuffer buffer = connState.getInbuffer();
 
         try {
-            
-            inbuffer.consumeContent(decoder);
+
+            buffer.consumeContent(decoder);
             if (decoder.isCompleted()) {
 
-                processResponse(conn, inbuffer);
-                // Clean up buffers just in case
-                inbuffer.clear();
-                outbuffer.clear();
+                processResponse(conn, buffer);
+
+                connState.clear();
                 // Ready for another request
                 conn.requestOutput();                
             }
@@ -225,11 +222,13 @@
 
     public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder)
{
         HttpContext context = conn.getContext();
-        OutputBuffer outbuffer = (OutputBuffer) context.getAttribute(OUT_BUF);
+
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        OutputBuffer buffer = connState.getOutbuffer();
 
         try {
             
-            outbuffer.produceContent(encoder);
+            buffer.produceContent(encoder);
             
         } catch (IOException ex) {
             shutdownConnection(conn);
@@ -245,8 +244,8 @@
         HttpRequest request = (HttpRequest) context.getAttribute(
                 HttpExecutionContext.HTTP_REQUEST);
 
-        InputBuffer inbuffer = (InputBuffer) context.getAttribute(IN_BUF);
-        OutputBuffer outbuffer = (OutputBuffer) context.getAttribute(OUT_BUF);
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        InputBuffer buffer = connState.getInbuffer();
         
         if (response.getStatusLine().getStatusCode() < HttpStatus.SC_OK) {
             // Just ignore 1xx responses;
@@ -256,10 +255,9 @@
         if (!canResponseHaveBody(request, response)) {
             try {
                 
-                processResponse(conn, inbuffer);
+                processResponse(conn, buffer);
                 // Clean up buffers just in case
-                inbuffer.clear();
-                outbuffer.clear();
+                connState.clear();
                 // Ready for another request
                 conn.requestOutput();                
                 
@@ -361,4 +359,31 @@
         }
         
     }
+    
+    private static class ConnState {
+        
+        private final InputBuffer inbuffer; 
+        private final OutputBuffer outbuffer;
+        
+        public ConnState() {
+            super();
+            this.inbuffer = new InputBuffer(2048);
+            this.outbuffer = new OutputBuffer(2048);
+        }
+
+        public InputBuffer getInbuffer() {
+            return this.inbuffer;
+        }
+
+        public OutputBuffer getOutbuffer() {
+            return this.outbuffer;
+        }
+        
+        public void clear() {
+            this.inbuffer.clear();
+            this.outbuffer.clear();
+        }
+        
+    }
+    
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java?view=diff&rev=507209&r1=507208&r2=507209
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
Tue Feb 13 12:51:03 2007
@@ -77,8 +77,7 @@
  */
 public class BufferingHttpServiceHandler implements NHttpServiceHandler {
 
-    private static final String IN_BUF = "http.nio.in-buffer";
-    private static final String OUT_BUF = "http.nio.out-buffer";
+    private static final String CONN_STATE = "http.nio.conn-state";
     
     private HttpParams params;
     private HttpProcessor httpProcessor;
@@ -131,11 +130,9 @@
     public void connected(final NHttpServerConnection conn) {
         HttpContext context = conn.getContext();
 
-        InputBuffer inbuffer = new InputBuffer(2048); 
-        OutputBuffer outbuffer = new OutputBuffer(2048);
-        
-        context.setAttribute(IN_BUF, inbuffer);
-        context.setAttribute(OUT_BUF, outbuffer);
+        ConnState connState = new ConnState(); 
+
+        context.setAttribute(CONN_STATE, connState);
 
         if (this.eventListener != null) {
             InetAddress address = null;
@@ -156,12 +153,9 @@
             ver = HttpVersion.HTTP_1_1;
         }
 
-        InputBuffer inbuffer = (InputBuffer) context.getAttribute(IN_BUF);
-        OutputBuffer outbuffer = (OutputBuffer) context.getAttribute(OUT_BUF);
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
 
-        // Clean the buffers just in case
-        inbuffer.clear();
-        outbuffer.clear();
+        connState.clear();
         
         try {
 
@@ -236,15 +230,16 @@
         HttpContext context = conn.getContext();
         HttpRequest request = conn.getHttpRequest();
 
-        InputBuffer inbuffer = (InputBuffer) context.getAttribute(IN_BUF);
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        InputBuffer buffer = connState.getInbuffer();
 
         try {
-            inbuffer.consumeContent(decoder);
+            buffer.consumeContent(decoder);
             if (decoder.isCompleted()) {
                 // Request entity has been fully received
 
                 // Create a wrapper entity instead of the original one
-                BufferedContent.wrapEntity((HttpEntityEnclosingRequest) request, inbuffer);
+                BufferedContent.wrapEntity((HttpEntityEnclosingRequest) request, buffer);
                 processRequest(conn, request);
             }
             
@@ -265,11 +260,13 @@
 
         HttpContext context = conn.getContext();
         HttpResponse response = conn.getHttpResponse();
-        OutputBuffer outbuffer = (OutputBuffer) context.getAttribute(OUT_BUF);
+
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        OutputBuffer buffer = connState.getOutbuffer();
 
         try {
             
-            outbuffer.produceContent(encoder);
+            buffer.produceContent(encoder);
             if (encoder.isCompleted()) {
                 if (!this.connStrategy.keepAlive(response, context)) {
                     conn.close();
@@ -379,7 +376,9 @@
             final HttpResponse response) throws IOException, HttpException {
 
         HttpContext context = conn.getContext();
-        OutputBuffer outbuffer = (OutputBuffer) context.getAttribute(OUT_BUF);
+
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        OutputBuffer buffer = connState.getOutbuffer();
 
         this.httpProcessor.process(response, context);
         conn.submitResponse(response);
@@ -387,7 +386,7 @@
         if (response.getEntity() != null) {
             HttpEntity entity = response.getEntity();
             if (entity != null) {
-                OutputStream outstream = new ContentOutputStream(outbuffer);
+                OutputStream outstream = new ContentOutputStream(buffer);
                 entity.writeTo(outstream);
                 outstream.flush();
                 outstream.close();
@@ -395,4 +394,30 @@
         }
     }
 
+    private static class ConnState {
+        
+        private final InputBuffer inbuffer; 
+        private final OutputBuffer outbuffer;
+        
+        public ConnState() {
+            super();
+            this.inbuffer = new InputBuffer(2048);
+            this.outbuffer = new OutputBuffer(2048);
+        }
+
+        public InputBuffer getInbuffer() {
+            return this.inbuffer;
+        }
+
+        public OutputBuffer getOutbuffer() {
+            return this.outbuffer;
+        }
+        
+        public void clear() {
+            this.inbuffer.clear();
+            this.outbuffer.clear();
+        }
+        
+    }
+        
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java?view=diff&rev=507209&r1=507208&r2=507209
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java
Tue Feb 13 12:51:03 2007
@@ -51,6 +51,7 @@
 import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.ContentIOControl;
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.NHttpServerConnection;
 import org.apache.http.nio.NHttpServiceHandler;
@@ -86,8 +87,7 @@
  */
 public class ThrottlingHttpServiceHandler implements NHttpServiceHandler {
 
-    private static final String IN_BUF = "http.nio.in-buffer";
-    private static final String OUT_BUF = "http.nio.out-buffer";
+    private static final String CONN_STATE = "http.nio.conn-state";
     
     private HttpParams params;
     private HttpProcessor httpProcessor;
@@ -151,12 +151,10 @@
             bufsize = 20480;
         }
         
-        SharedInputBuffer inbuffer = new SharedInputBuffer(bufsize, conn); 
-        SharedOutputBuffer outbuffer = new SharedOutputBuffer(bufsize, conn); 
-     
-        context.setAttribute(IN_BUF, inbuffer);
-        context.setAttribute(OUT_BUF, outbuffer);
-        
+        ConnState connState = new ConnState(bufsize, conn); 
+
+        context.setAttribute(CONN_STATE, connState);
+
         if (this.eventListener != null) {
             InetAddress address = null;
             if (conn instanceof HttpInetConnection) {
@@ -169,11 +167,9 @@
     public void closed(final NHttpServerConnection conn) {
         HttpContext context = conn.getContext();
         
-        SharedInputBuffer inbuffer = (SharedInputBuffer) context.getAttribute(IN_BUF);
-        SharedOutputBuffer outbuffer = (SharedOutputBuffer) context.getAttribute(OUT_BUF);
-        
-        inbuffer.shutdown();
-        outbuffer.shutdown();
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+
+        connState.shutdown();
         
         if (this.eventListener != null) {
             InetAddress address = null;
@@ -240,12 +236,10 @@
             ver = HttpVersion.HTTP_1_1;
         }
 
-        SharedInputBuffer inbuffer = (SharedInputBuffer) context.getAttribute(IN_BUF);
-        SharedOutputBuffer outbuffer = (SharedOutputBuffer) context.getAttribute(OUT_BUF);
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        SharedInputBuffer buffer = connState.getInbuffer();
 
-        // Clean the buffers just in case
-        inbuffer.reset();
-        outbuffer.reset();
+        connState.clear();
 
         if (request instanceof HttpEntityEnclosingRequest) {
             if (((HttpEntityEnclosingRequest) request).expectContinue()) {
@@ -266,7 +260,7 @@
                 }
             }
             // Create a wrapper entity instead of the original one
-            BufferedContent.wrapEntity((HttpEntityEnclosingRequest) request, inbuffer);
+            BufferedContent.wrapEntity((HttpEntityEnclosingRequest) request, buffer);
         }
         
         this.executor.execute(new Runnable() {
@@ -296,11 +290,12 @@
     public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder)
{
         HttpContext context = conn.getContext();
 
-        SharedInputBuffer inbuffer = (SharedInputBuffer) context.getAttribute(IN_BUF);
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        SharedInputBuffer buffer = connState.getInbuffer();
         
         try {
             
-            inbuffer.consumeContent(decoder);
+            buffer.consumeContent(decoder);
             
         } catch (IOException ex) {
             shutdownConnection(conn);
@@ -314,11 +309,12 @@
         HttpContext context = conn.getContext();
         HttpResponse response = conn.getHttpResponse();
 
-        SharedOutputBuffer outbuffer = (SharedOutputBuffer) context.getAttribute(OUT_BUF);
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        SharedOutputBuffer buffer = connState.getOutbuffer();
         
         try {
 
-            outbuffer.produceContent(encoder);
+            buffer.produceContent(encoder);
             if (encoder.isCompleted()) {
                 if (!this.connStrategy.keepAlive(response, context)) {
                     conn.close();
@@ -336,19 +332,14 @@
     private void shutdownConnection(final NHttpConnection conn) {
         HttpContext context = conn.getContext();
 
-        SharedInputBuffer inbuffer = (SharedInputBuffer) context.getAttribute(IN_BUF);
-        SharedOutputBuffer outbuffer = (SharedOutputBuffer) context.getAttribute(OUT_BUF);
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
         
         try {
             conn.shutdown();
         } catch (IOException ignore) {
         }
-        
-        if (inbuffer != null) {
-            inbuffer.shutdown();
-        }
-        if (outbuffer != null) {
-            outbuffer.shutdown();
+        if (connState != null) {
+            connState.shutdown();
         }
     }
     
@@ -429,18 +420,51 @@
             final HttpResponse response) throws IOException, HttpException {
 
         HttpContext context = conn.getContext();
-        SharedOutputBuffer outbuffer = (SharedOutputBuffer) context.getAttribute(OUT_BUF);
+
+        ConnState connState = (ConnState) context.getAttribute(CONN_STATE);
+        SharedOutputBuffer buffer = connState.getOutbuffer();
 
         this.httpProcessor.process(response, context);
         conn.submitResponse(response);
 
         if (response.getEntity() != null) {
             HttpEntity entity = response.getEntity();
-            OutputStream outstream = new ContentOutputStream(outbuffer);
+            OutputStream outstream = new ContentOutputStream(buffer);
             entity.writeTo(outstream);
             outstream.flush();
             outstream.close();
         }
+    }
+
+    private static class ConnState {
+        
+        private final SharedInputBuffer inbuffer; 
+        private final SharedOutputBuffer outbuffer;
+
+        public ConnState(int bufferSize, final ContentIOControl ioControl) {
+            super();
+            this.inbuffer = new SharedInputBuffer(bufferSize, ioControl);
+            this.outbuffer = new SharedOutputBuffer(bufferSize, ioControl);
+        }
+
+        public SharedInputBuffer getInbuffer() {
+            return this.inbuffer;
+        }
+
+        public SharedOutputBuffer getOutbuffer() {
+            return this.outbuffer;
+        }
+        
+        public void shutdown() {
+            this.inbuffer.shutdown();
+            this.outbuffer.shutdown();
+        }
+
+        public void clear() {
+            this.inbuffer.reset();
+            this.outbuffer.reset();
+        }
+        
     }
     
 }



Mime
View raw message