hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r573557 - in /jakarta/httpcomponents/httpcore/trunk: module-nio/src/examples/org/apache/http/examples/nio/ module-nio/src/main/java/org/apache/http/nio/protocol/ module-nio/src/test/java/org/apache/http/nio/protocol/ module-niossl/src/examp...
Date Fri, 07 Sep 2007 12:07:43 GMT
Author: olegk
Date: Fri Sep  7 05:07:42 2007
New Revision: 573557

URL: http://svn.apache.org/viewvc?rev=573557&view=rev
Log:
Added #finalizeContext method to the HttpRequestExecutionHandler interface; improved NHttp
client examples

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java
    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/HttpRequestExecutionHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLClient.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java?rev=573557&r1=573556&r2=573557&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java
Fri Sep  7 05:07:42 2007
@@ -149,7 +149,8 @@
     
     static class MyHttpRequestExecutionHandler implements HttpRequestExecutionHandler {
 
-        private final static String DONE = "done";
+        private final static String REQUEST_SENT       = "request-sent";
+        private final static String RESPONSE_RECEIVED  = "response-received";
         
         private final RequestCount requestCount;
         
@@ -162,14 +163,25 @@
             HttpHost targetHost = (HttpHost) attachment;
             context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
         }
+        
+        public void finalizeContext(final HttpContext context) {
+            Object flag = context.getAttribute(RESPONSE_RECEIVED);
+            if (flag == null) {
+                // Signal completion of the request execution
+                synchronized (this.requestCount) {
+                    this.requestCount.decrement();
+                    this.requestCount.notifyAll();
+                }
+            }
+        }
 
         public HttpRequest submitRequest(final HttpContext context) {
             HttpHost targetHost = (HttpHost) context.getAttribute(
                     ExecutionContext.HTTP_TARGET_HOST);
-            Object token = context.getAttribute(DONE);
-            if (token == null) {
+            Object flag = context.getAttribute(REQUEST_SENT);
+            if (flag == null) {
                 // Stick some object into the context
-                context.setAttribute(DONE, Boolean.TRUE);
+                context.setAttribute(REQUEST_SENT, Boolean.TRUE);
 
                 System.out.println("--------------");
                 System.out.println("Sending request to " + targetHost);
@@ -196,6 +208,8 @@
                 System.err.println("I/O error: " + ex.getMessage());
             }
 
+            context.setAttribute(RESPONSE_RECEIVED, Boolean.TRUE);
+            
             // Signal completion of the request execution
             synchronized (this.requestCount) {
                 this.requestCount.decrement();

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?rev=573557&r1=573556&r2=573557&view=diff
==============================================================================
--- 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
Fri Sep  7 05:07:42 2007
@@ -105,6 +105,10 @@
     }
 
     public void closed(final NHttpClientConnection conn) {
+        HttpContext context = conn.getContext();
+
+        this.execHandler.finalizeContext(context);
+        
         if (this.eventListener != null) {
             this.eventListener.connectionClosed(conn);
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/HttpRequestExecutionHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/HttpRequestExecutionHandler.java?rev=573557&r1=573556&r2=573557&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/HttpRequestExecutionHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/HttpRequestExecutionHandler.java
Fri Sep  7 05:07:42 2007
@@ -87,4 +87,13 @@
     void handleResponse(HttpResponse response, HttpContext context) 
         throws IOException;
     
+    /**
+     * Triggered when the connection is terminated. This event can be used
+     * to release objects stored in the context or perform some other kind 
+     * of cleanup.
+     * 
+     * @param context the actual HTTP context
+     */
+    void finalizeContext(HttpContext context);
+    
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java?rev=573557&r1=573556&r2=573557&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java
Fri Sep  7 05:07:42 2007
@@ -128,6 +128,10 @@
     }
 
     public void closed(final NHttpClientConnection conn) {
+        HttpContext context = conn.getContext();
+
+        this.execHandler.finalizeContext(context);
+        
         if (this.eventListener != null) {
             this.eventListener.connectionClosed(conn);
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java?rev=573557&r1=573556&r2=573557&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
Fri Sep  7 05:07:42 2007
@@ -337,6 +337,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpRequest get = null;
@@ -477,6 +480,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpEntityEnclosingRequest post = null;
@@ -620,6 +626,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpEntityEnclosingRequest post = null;
@@ -770,6 +779,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpEntityEnclosingRequest post = null;
@@ -916,6 +928,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpEntityEnclosingRequest post = null;
@@ -1065,6 +1080,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpEntityEnclosingRequest post = null;
@@ -1203,6 +1221,9 @@
                 context.setAttribute("LIST", (List) attachment);
                 context.setAttribute("REQ-COUNT", new Integer(0));
                 context.setAttribute("RES-COUNT", new Integer(0));
+            }
+
+            public void finalizeContext(final HttpContext context) {
             }
 
             public HttpRequest submitRequest(final HttpContext context) {

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLClient.java?rev=573557&r1=573556&r2=573557&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLClient.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLClient.java
Fri Sep  7 05:07:42 2007
@@ -131,14 +131,14 @@
                 new HttpHost("www.netscape.com", 443),
                 null);
         reqs[1] = ioReactor.connect(
-                new InetSocketAddress("www.netscape.com", 443), 
+                new InetSocketAddress("www.verisign.com", 443), 
                 null,
-                new HttpHost("www.netscape.com", 443),
+                new HttpHost("www.verisign.com", 443),
                 null);
         reqs[2] = ioReactor.connect(
-                new InetSocketAddress("www.netscape.com", 443), 
+                new InetSocketAddress("www.yahoo.com", 443), 
                 null,
-                new HttpHost("www.netscape.com", 443),
+                new HttpHost("www.yahoo.com", 443),
                 null);
      
         // Block until all connections signal
@@ -158,7 +158,8 @@
     
     static class MyHttpRequestExecutionHandler implements HttpRequestExecutionHandler {
 
-        private final static String DONE = "done";
+        private final static String REQUEST_SENT       = "request-sent";
+        private final static String RESPONSE_RECEIVED  = "response-received";
         
         private final RequestCount requestCount;
         
@@ -172,13 +173,24 @@
             context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
         }
 
+        public void finalizeContext(final HttpContext context) {
+            Object flag = context.getAttribute(RESPONSE_RECEIVED);
+            if (flag == null) {
+                // Signal completion of the request execution
+                synchronized (this.requestCount) {
+                    this.requestCount.decrement();
+                    this.requestCount.notifyAll();
+                }
+            }
+        }
+
         public HttpRequest submitRequest(final HttpContext context) {
             HttpHost targetHost = (HttpHost) context.getAttribute(
                     ExecutionContext.HTTP_TARGET_HOST);
-            Object token = context.getAttribute(DONE);
+            Object token = context.getAttribute(REQUEST_SENT);
             if (token == null) {
                 // Stick some object into the context
-                context.setAttribute(DONE, Boolean.TRUE);
+                context.setAttribute(REQUEST_SENT, Boolean.TRUE);
 
                 System.out.println("--------------");
                 System.out.println("Sending request to " + targetHost);
@@ -205,6 +217,8 @@
                 System.err.println("I/O error: " + ex.getMessage());
             }
 
+            context.setAttribute(RESPONSE_RECEIVED, Boolean.TRUE);
+            
             // Signal completion of the request execution
             synchronized (this.requestCount) {
                 this.requestCount.decrement();

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java?rev=573557&r1=573556&r2=573557&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java
Fri Sep  7 05:07:42 2007
@@ -243,6 +243,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpRequest get = null;
@@ -384,6 +387,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpEntityEnclosingRequest post = null;
@@ -528,6 +534,9 @@
                 context.setAttribute("RES-COUNT", new Integer(0));
             }
 
+            public void finalizeContext(final HttpContext context) {
+            }
+
             public HttpRequest submitRequest(final HttpContext context) {
                 int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
                 BasicHttpEntityEnclosingRequest post = null;
@@ -677,6 +686,9 @@
                 context.setAttribute("LIST", (List) attachment);
                 context.setAttribute("REQ-COUNT", new Integer(0));
                 context.setAttribute("RES-COUNT", new Integer(0));
+            }
+
+            public void finalizeContext(final HttpContext context) {
             }
 
             public HttpRequest submitRequest(final HttpContext context) {



Mime
View raw message