hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r535511 - in /jakarta/httpcomponents/httpcore/trunk: contrib/src/main/java/org/apache/http/contrib/benchmark/ contrib/src/main/java/org/apache/http/contrib/spring/ module-main/src/examples/org/apache/http/examples/ module-main/src/main/java...
Date Sat, 05 May 2007 12:01:58 GMT
Author: olegk
Date: Sat May  5 05:01:58 2007
New Revision: 535511

URL: http://svn.apache.org/viewvc?view=rev&rev=535511
Log:
Changelog:
* Decoupled the process of request preparation and response finalization from the process
of request execution (something Roland long insisted upon). This makes it possible to retry
the request execution in case of an I/O failure without re-running HTTP protocol interceptors
on the failed request 

Modified:
    jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/BenchmarkWorker.java
    jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/HttpBenchmark.java
    jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/SpringHttpDemo.java
    jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/http-beans.xml
    jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpGet.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpPost.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java

Modified: jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/BenchmarkWorker.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/BenchmarkWorker.java?view=diff&rev=535511&r1=535510&r2=535511
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/BenchmarkWorker.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/BenchmarkWorker.java
Sat May  5 05:01:58 2007
@@ -60,6 +60,7 @@
     private final int verbosity;
     private final HttpParams params;
     private final HttpContext context;
+    private final BasicHttpProcessor httpProcessor;
     private final HttpRequestExecutor httpexecutor;
     private final ConnectionReuseStrategy connstrategy;
     private final HttpRequest request;
@@ -68,8 +69,13 @@
     private final boolean keepalive;
     private final Stats stats = new Stats();
 
-    public BenchmarkWorker(final HttpParams params, int verbosity, final HttpRequest request,
-        final HttpHost targetHost, int count, boolean keepalive) {
+    public BenchmarkWorker(
+            final HttpParams params, 
+            int verbosity, 
+            final HttpRequest request,
+            final HttpHost targetHost, 
+            int count, 
+            boolean keepalive) {
 
         super();
         this.params = params;
@@ -79,17 +85,16 @@
         this.count = count;
         this.keepalive = keepalive;
 
-        BasicHttpProcessor httpproc = new BasicHttpProcessor();
-        this.httpexecutor = new HttpRequestExecutor(httpproc);
-        this.httpexecutor.setParams(params);
+        this.httpProcessor = new BasicHttpProcessor();
+        this.httpexecutor = new HttpRequestExecutor(params);
 
         // Required request interceptors
-        httpproc.addInterceptor(new RequestContent());
-        httpproc.addInterceptor(new RequestTargetHost());
+        this.httpProcessor.addInterceptor(new RequestContent());
+        this.httpProcessor.addInterceptor(new RequestTargetHost());
         // Recommended request interceptors
-        httpproc.addInterceptor(new RequestConnControl());
-        httpproc.addInterceptor(new RequestUserAgent());
-        httpproc.addInterceptor(new RequestExpectContinue());
+        this.httpProcessor.addInterceptor(new RequestConnControl());
+        this.httpProcessor.addInterceptor(new RequestUserAgent());
+        this.httpProcessor.addInterceptor(new RequestExpectContinue());
 
         this.connstrategy = new DefaultConnectionReuseStrategy();
         this.verbosity = verbosity;
@@ -106,7 +111,11 @@
             port = 80;
         }
 
-        this.context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, targetHost);
+        // Populate the execution context
+        this.context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+        this.context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, this.targetHost);
+        this.context.setAttribute(HttpExecutionContext.HTTP_REQUEST, this.request);
+
         stats.start();
 
         for (int i=0; i < count; i++) {
@@ -125,7 +134,13 @@
                 }
 
                 try {
-                    response = this.httpexecutor.execute(request, conn, this.context);
+                    // Prepare request
+                    this.httpexecutor.preProcess(this.request, this.httpProcessor, this.context);
+                    // Execute request and get a response
+                    response = this.httpexecutor.execute(this.request, conn, this.context);
+                    // Finalize response
+                    this.httpexecutor.postProcess(response, this.httpProcessor, this.context);
+                    
                 } catch (HttpException e) {
                     stats.incWriteErrors();
                     if (this.verbosity >= 2) {

Modified: jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/HttpBenchmark.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/HttpBenchmark.java?view=diff&rev=535511&r1=535510&r2=535511
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/HttpBenchmark.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/benchmark/HttpBenchmark.java
Sat May  5 05:01:58 2007
@@ -112,7 +112,8 @@
             }
 
             for (int i = 0; i < threads; i++) {
-                BasicHttpEntityEnclosingRequest httppost = new BasicHttpEntityEnclosingRequest("POST",
url.getPath());
+                BasicHttpEntityEnclosingRequest httppost = 
+                    new BasicHttpEntityEnclosingRequest("POST", url.getPath());
                 httppost.setEntity(entity);
                 request[i] = httppost;
             }
@@ -134,13 +135,15 @@
             }
         }
 
-        for (int i = 0; i < headers.length; i++) {
-            String s = headers[i];
-            int pos = s.indexOf(':');
-            if (pos != -1) {
-                Header header = new DefaultHeader(s.substring(0, pos).trim(), s.substring(pos
+ 1));
-                for (int j = 0; j < threads; j++) {
-                    request[j].addHeader(header);
+        if (headers != null) {
+            for (int i = 0; i < headers.length; i++) {
+                String s = headers[i];
+                int pos = s.indexOf(':');
+                if (pos != -1) {
+                    Header header = new DefaultHeader(s.substring(0, pos).trim(), s.substring(pos
+ 1));
+                    for (int j = 0; j < threads; j++) {
+                        request[j].addHeader(header);
+                    }
                 }
             }
         }
@@ -164,7 +167,13 @@
 
         BenchmarkWorker[] workers = new BenchmarkWorker[threads];
         for (int i = 0; i < threads; i++) {
-            workers[i] = new BenchmarkWorker(params, verbosity, request[i], host, requests,
keepAlive);
+            workers[i] = new BenchmarkWorker(
+                    params, 
+                    verbosity, 
+                    request[i], 
+                    host, 
+                    requests, 
+                    keepAlive);
             workerPool.execute(workers[i]);
         }
 

Modified: jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/SpringHttpDemo.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/SpringHttpDemo.java?view=diff&rev=535511&r1=535510&r2=535511
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/SpringHttpDemo.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/SpringHttpDemo.java
Sat May  5 05:01:58 2007
@@ -66,7 +66,6 @@
         HttpParams params = (HttpParams) beanfactory.getBean("params");
         
         HttpRequestExecutor httpexec = (HttpRequestExecutor)beanfactory.getBean("http-executor");
-        httpexec.setParams(params);
         
         HttpHost host = new HttpHost("www.yahoo.com", 80);
 

Modified: jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/http-beans.xml
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/http-beans.xml?view=diff&rev=535511&r1=535510&r2=535511
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/http-beans.xml
(original)
+++ jakarta/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/spring/http-beans.xml
Sat May  5 05:01:58 2007
@@ -61,7 +61,7 @@
   <bean id="http-executor" 
     class="org.apache.http.protocol.HttpRequestExecutor" 
     singleton="true" >
-    <constructor-arg index="0"><ref bean="http-processor"/></constructor-arg>
+    <constructor-arg index="0"><ref bean="params"/></constructor-arg>
   </bean>
   
   <bean id="http-request-factory"

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpGet.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpGet.java?view=diff&rev=535511&r1=535510&r2=535511
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpGet.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpGet.java
Sat May  5 05:01:58 2007
@@ -82,16 +82,17 @@
         httpproc.addInterceptor(new RequestUserAgent());
         httpproc.addInterceptor(new RequestExpectContinue());
 
-        HttpRequestExecutor httpexecutor = new HttpRequestExecutor(httpproc);
-        httpexecutor.setParams(params);
+        HttpRequestExecutor httpexecutor = new HttpRequestExecutor(params);
         
         HttpContext context = new HttpExecutionContext(null);
         HttpHost host = new HttpHost("localhost", 8080);
-        context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, host);
 
         DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
         ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
-        
+
+        context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+        context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, host);
+
         try {
             
             String[] targets = {
@@ -106,7 +107,13 @@
                 }
                 BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
                 System.out.println(">> Request URI: " + request.getRequestLine().getUri());
+                
+                context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
+                
+                httpexecutor.preProcess(request, httpproc, context);
                 HttpResponse response = httpexecutor.execute(request, conn, context);
+                httpexecutor.postProcess(response, httpproc, context);
+                
                 System.out.println("<< Response: " + response.getStatusLine());
                 System.out.println(EntityUtils.toString(response.getEntity()));
                 System.out.println("==============");

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpPost.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpPost.java?view=diff&rev=535511&r1=535510&r2=535511
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpPost.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpPost.java
Sat May  5 05:01:58 2007
@@ -87,17 +87,18 @@
         httpproc.addInterceptor(new RequestUserAgent());
         httpproc.addInterceptor(new RequestExpectContinue());
         
-        HttpRequestExecutor httpexecutor = new HttpRequestExecutor(httpproc);
-        httpexecutor.setParams(params);
+        HttpRequestExecutor httpexecutor = new HttpRequestExecutor(params);
 
         HttpContext context = new HttpExecutionContext(null);
         
         HttpHost host = new HttpHost("localhost", 8080);
-        context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, host);
         
         DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
         ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
 
+        context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+        context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, host);
+
         try {
             
             HttpEntity[] requestBodies = {
@@ -120,7 +121,13 @@
                         "/servlets-examples/servlet/RequestInfoExample");
                 request.setEntity(requestBodies[i]);
                 System.out.println(">> Request URI: " + request.getRequestLine().getUri());
+
+                context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
+                
+                httpexecutor.preProcess(request, httpproc, context);
                 HttpResponse response = httpexecutor.execute(request, conn, context);
+                httpexecutor.postProcess(response, httpproc, context);
+                
                 System.out.println("<< Response: " + response.getStatusLine());
                 System.out.println(EntityUtils.toString(response.getEntity()));
                 System.out.println("==============");

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java?view=diff&rev=535511&r1=535510&r2=535511
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java
Sat May  5 05:01:58 2007
@@ -57,20 +57,14 @@
  */
 public class HttpRequestExecutor {
 
-    private HttpParams params;
-    private final HttpProcessor processor;
+    private final HttpParams params;
 
     /**
      * Create a new request executor.
-     *
-     * @param proc      the processor to use on requests and responses
      */
-    public HttpRequestExecutor(HttpProcessor proc) {
-        if (proc == null)
-            throw new IllegalArgumentException
-                ("HTTP processor must not be null.");
-
-        this.processor = proc;
+    public HttpRequestExecutor(final HttpParams params) {
+        super();
+        this.params = params;
     }
 
     /**
@@ -83,15 +77,6 @@
     }
 
     /**
-     * Set new parameters for executing requests.
-     *
-     * @param params    the new parameters to use from now on
-     */
-    public final void setParams(final HttpParams params) {
-        this.params = params;
-    }
-
-    /**
      * Decide whether a response comes with an entity.
      * The implementation in this class is based on RFC 2616.
      * Unknown methods and response codes are supposed to
@@ -142,16 +127,11 @@
             throw new IllegalArgumentException("HTTP context may not be null");
         }
 
-        context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
-        context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
-        
         try {
-            doPrepareRequest(request, context);
             HttpResponse response = doSendRequest(request, conn, context);
             if (response == null) {
                 response = doReceiveResponse(request, conn, context);
             }
-            doFinishResponse(response, context);
             return response;
         } catch (IOException ex) {
             conn.close();
@@ -169,18 +149,23 @@
      * Prepare a request for sending.
      *
      * @param request   the request to prepare
+     * @param processor the processor to use
      * @param context   the context for sending the request
      *
      * @throws HttpException      in case of a protocol or processing problem
      * @throws IOException        in case of an I/O problem
      */
-    protected void doPrepareRequest(
-            final HttpRequest          request,
-            final HttpContext          context)
+    public void preProcess(
+            final HttpRequest request,
+            final HttpProcessor processor,
+            final HttpContext context)
                 throws HttpException, IOException {
         if (request == null) {
             throw new IllegalArgumentException("HTTP request may not be null");
         }
+        if (processor == null) {
+            throw new IllegalArgumentException("HTTP processor may not be null");
+        }
         if (context == null) {
             throw new IllegalArgumentException("HTTP context may not be null");
         }
@@ -323,17 +308,22 @@
      * connection over which the response is coming in.
      *
      * @param response  the response object to finish
+     * @param processor the processor to use
      * @param context   the context for post-processing the response
      *
      * @throws HttpException      in case of a protocol or processing problem
      * @throws IOException        in case of an I/O problem
      */
-    protected void doFinishResponse(
+    public void postProcess(
             final HttpResponse response,
+            final HttpProcessor processor,
             final HttpContext context)
                 throws HttpException, IOException {
         if (response == null) {
             throw new IllegalArgumentException("HTTP response may not be null");
+        }
+        if (processor == null) {
+            throw new IllegalArgumentException("HTTP processor may not be null");
         }
         if (context == null) {
             throw new IllegalArgumentException("HTTP context may not be null");

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java?view=diff&rev=535511&r1=535510&r2=535511
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
Sat May  5 05:01:58 2007
@@ -81,8 +81,7 @@
         this.httpproc.addInterceptor(new RequestUserAgent());
         this.httpproc.addInterceptor(new RequestExpectContinue());
 
-        this.httpexecutor = new HttpRequestExecutor(this.httpproc);
-        this.httpexecutor.setParams(this.params);
+        this.httpexecutor = new HttpRequestExecutor(this.params);
         this.connStrategy = new DefaultConnectionReuseStrategy();
         this.context = new HttpExecutionContext(null);
     }
@@ -95,8 +94,13 @@
             final HttpRequest request,
             final HttpHost targetHost,
             final HttpClientConnection conn) throws HttpException, IOException {
+        this.context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
         this.context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, targetHost);
-        return this.httpexecutor.execute(request, conn, this.context);
+        this.context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+        this.httpexecutor.preProcess(request, this.httpproc, this.context);
+        HttpResponse response = this.httpexecutor.execute(request, conn, this.context);
+        this.httpexecutor.postProcess(response, this.httpproc, this.context);
+        return response;
     }
     
     public boolean keepAlive(final HttpResponse response) {



Mime
View raw message