hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r165528 - in /jakarta/httpclient/trunk/http-common/src: examples/org/apache/http/examples/ java/org/apache/http/ java/org/apache/http/executor/ java/org/apache/http/impl/
Date Sun, 01 May 2005 17:42:11 GMT
Author: olegk
Date: Sun May  1 10:42:10 2005
New Revision: 165528

URL: http://svn.apache.org/viewcvs?rev=165528&view=rev
Log:
Initial implementation of the HttpRequestExecutor

Added:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
  (with props)
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/BasicHttpContext.java
  (with props)
Modified:
    jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpGet.java
    jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpPost.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpClientConnection.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpRequestInterceptor.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpResponseInterceptor.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestRetryHandler.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/DefaultHttpClientConnection.java

Modified: jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpGet.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpGet.java?rev=165528&r1=165527&r2=165528&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpGet.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpGet.java
Sun May  1 10:42:10 2005
@@ -52,7 +52,7 @@
         
         HttpParams connparams = new DefaultHttpParams(null);
         HttpHost host = new HttpHost("www.yahoo.com");
-        HttpClientConnection conn = new DefaultHttpClientConnection();
+        HttpClientConnection conn = new DefaultHttpClientConnection(host);
         try {
             
             String[] targets = {
@@ -67,7 +67,7 @@
                 request.setHeader(new Header("Connection", "Keep-Alive"));
                 if (!conn.isOpen()) {
                     System.out.println("Open new connection to: " + host);
-                    conn.open(host, connparams, null);
+                    conn.open(connparams);
                 } else {
                     System.out.println("Connection kept alive. Reusing...");
                 }

Modified: jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpPost.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpPost.java?rev=165528&r1=165527&r2=165528&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpPost.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/examples/org/apache/http/examples/ElementalHttpPost.java
Sun May  1 10:42:10 2005
@@ -58,7 +58,7 @@
         
         HttpParams connparams = new DefaultHttpParams(null);
         HttpHost host = new HttpHost("localhost", 8080);
-        HttpClientConnection conn = new DefaultHttpClientConnection();
+        HttpClientConnection conn = new DefaultHttpClientConnection(host);
         try {
             
             HttpEntity[] requestBodies = {
@@ -94,7 +94,7 @@
                 
                 if (!conn.isOpen()) {
                     System.out.println("Open new connection to: " + host);
-                    conn.open(host, connparams, null);
+                    conn.open(connparams);
                 } else {
                     System.out.println("Connection kept alive. Reusing...");
                 }

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpClientConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpClientConnection.java?rev=165528&r1=165527&r2=165528&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpClientConnection.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpClientConnection.java
Sun May  1 10:42:10 2005
@@ -30,7 +30,6 @@
 package org.apache.http;
 
 import java.io.IOException;
-import java.net.InetAddress;
 
 import org.apache.http.params.HttpParams;
 
@@ -45,7 +44,9 @@
  */
 public interface HttpClientConnection extends HttpConnection {
 
-    void open(HttpHost host, HttpParams params, InetAddress localAddress) throws IOException;
+    HttpHost getHost();
+    
+    void open(HttpParams params) throws IOException;
     
     HttpResponse sendRequest(HttpRequest request) throws HttpException, IOException;
 

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpRequestInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpRequestInterceptor.java?rev=165528&r1=165527&r2=165528&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpRequestInterceptor.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpRequestInterceptor.java
Sun May  1 10:42:10 2005
@@ -42,7 +42,7 @@
  */
 public interface HttpRequestInterceptor {
 
-    void intercept(HttpMutableRequest request, HttpContext context) 
+    void process(HttpMutableRequest request, HttpContext context) 
         throws HttpException, IOException;
     
 }

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpResponseInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpResponseInterceptor.java?rev=165528&r1=165527&r2=165528&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpResponseInterceptor.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpResponseInterceptor.java
Sun May  1 10:42:10 2005
@@ -42,7 +42,7 @@
  */
 public interface HttpResponseInterceptor {
 
-    void intercept(HttpMutableResponse response, HttpContext context) 
+    void process(HttpMutableResponse response, HttpContext context) 
         throws HttpException, IOException;
     
 }

Added: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java?rev=165528&view=auto
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
(added)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
Sun May  1 10:42:10 2005
@@ -0,0 +1,231 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 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.executor;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.http.HttpClientConnection;
+import org.apache.http.HttpContext;
+import org.apache.http.HttpException;
+import org.apache.http.HttpMutableRequest;
+import org.apache.http.HttpMutableResponse;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.impl.BasicHttpContext;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+
+/**
+ * <p>
+ * </p>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+public class HttpRequestExecutor {
+
+    private final HttpParams params;
+    private final HttpContext localContext;
+    
+    private HttpRequestRetryHandler retryhandler = null;
+    private Set interceptors = null; 
+    
+    public HttpRequestExecutor(final HttpParams params, final HttpContext parentContext)
{
+        super();
+        if (params == null) {
+            throw new IllegalArgumentException("HTTP parameters may not be null");
+        }
+        this.params = params;
+        this.localContext = new BasicHttpContext(parentContext); 
+    }
+    
+    private void setInterceptor(final Object obj) {
+        if (obj == null) {
+            return;
+        }
+        if (this.interceptors == null) {
+            this.interceptors = new HashSet();
+        }
+        this.interceptors.add(obj);
+    }
+    
+    public void removeInterceptor(final Object obj) {
+        if (obj == null) {
+            return;
+        }
+        if (this.interceptors == null) {
+            return;
+        }
+        this.interceptors.remove(obj);
+        if (this.interceptors.isEmpty()) {
+            this.interceptors = null;
+        }
+    }
+    
+    public void setRequestInterceptor(final HttpRequestInterceptor interceptor) {
+        setInterceptor(interceptor);
+    }
+    
+    public void setResponseInterceptor(final HttpResponseInterceptor interceptor) {
+        setInterceptor(interceptor);
+    }
+
+    public void removeRequestInterceptor(final HttpRequestInterceptor interceptor) {
+        removeInterceptor(interceptor);
+    }
+    
+    public void removeResponseInterceptor(final HttpResponseInterceptor interceptor) {
+        removeInterceptor(interceptor);
+    }
+    
+    public void removeInterceptors(final Class clazz) {
+        if (clazz == null) {
+            return;
+        }
+        if (this.interceptors == null) {
+            return;
+        }
+        for (Iterator i = this.interceptors.iterator(); i.hasNext(); ) {
+            if (clazz.isInstance(i.next())) {
+                i.remove();
+            }
+        }
+    }
+    
+    public HttpRequestRetryHandler getRetryHandler() {
+        return this.retryhandler;
+    }
+    
+    public void setRetryHandler(final HttpRequestRetryHandler retryhandler) {
+        this.retryhandler = retryhandler;
+    }
+    
+    private void preprocessRequest(final HttpMutableRequest request) 
+            throws IOException, HttpException {
+        if (this.interceptors == null) {
+            return;
+        }
+        for (Iterator i = this.interceptors.iterator(); i.hasNext(); ) {
+            Object obj = i.next();
+            if (obj instanceof HttpRequestInterceptor) {
+                HttpRequestInterceptor interceptor = (HttpRequestInterceptor)obj;
+                interceptor.process(request, this.localContext);
+            }
+        }
+    }
+
+    
+    
+    private void postprocessResponse(final HttpMutableResponse response) 
+            throws IOException, HttpException {
+        if (this.interceptors == null) {
+            return;
+        }
+        for (Iterator i = this.interceptors.iterator(); i.hasNext(); ) {
+            Object obj = i.next();
+            if (obj instanceof HttpResponseInterceptor) {
+                HttpResponseInterceptor interceptor = (HttpResponseInterceptor)obj;
+                interceptor.process(response, this.localContext);
+            }
+        }
+    }
+    
+    public HttpResponse execute(final HttpRequest request, final HttpClientConnection conn)

+            throws IOException, HttpException {
+        
+        if (request == null) {
+            throw new IllegalArgumentException("HTTP request may not be null");
+        }
+        if (conn == null) {
+            throw new IllegalArgumentException("Client connection may not be null");
+        }
+        
+        this.localContext.setAttribute(HttpContext.HTTP_REQUEST, request);
+        this.localContext.setAttribute(HttpContext.HTTP_CONNECTION, conn);
+        this.localContext.setAttribute(HttpContext.HTTP_TARGET_HOST, conn.getHost());
+        
+        if (request instanceof HttpMutableRequest) {
+            preprocessRequest((HttpMutableRequest)request);
+        }
+        
+        HttpResponse response = null;
+        
+        // loop until the method is successfully processed, the retryHandler 
+        // returns false or a non-recoverable exception is thrown
+        HttpConnectionParams connparams = new HttpConnectionParams(this.params); 
+        for (int execCount = 0; ; execCount++) {
+            try {
+                if (connparams.isStaleCheckingEnabled()) {
+                    if (conn.isStale()) {
+                        conn.close();
+                    }
+                }
+                if (!conn.isOpen()) {
+                    conn.open(this.params);
+                    // TODO: Implement secure tunnelling
+                }
+                response = conn.sendRequest(request);
+                // Request may be terminated prematurely, if the expect-continue 
+                // protocol is used
+                if (response == null) {
+                    // No error response so far. 
+                    response = conn.receiveResponse(request);
+                }
+                break;
+            } catch (IOException ex) {
+                conn.close();
+                if (this.retryhandler == null) {
+                    throw ex;
+                }
+                if (!this.retryhandler.retryRequest(ex, execCount, null)) {
+                    throw ex;
+                }
+            } catch (HttpException ex) {
+                conn.close();
+                throw ex;
+            } catch (RuntimeException ex) {
+                conn.close();
+                throw ex;
+            }
+        }
+        
+        if (response instanceof HttpMutableResponse) {
+            postprocessResponse((HttpMutableResponse)response);
+        }
+        return response;
+    }
+}

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestRetryHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestRetryHandler.java?rev=165528&r1=165527&r2=165528&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestRetryHandler.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestRetryHandler.java
Sun May  1 10:42:10 2005
@@ -31,7 +31,7 @@
 
 import java.io.IOException;
 
-import org.apache.http.HttpRequest;
+import org.apache.http.HttpContext;
 
 /**
  * A handler for determining if an HttpRequest should be retried after a 
@@ -59,6 +59,6 @@
      * @return <code>true</code> if the method should be retried, <code>false</code>
      * otherwise
      */
-    boolean retryRequest(HttpRequest request, IOException exception, int executionCount);
+    boolean retryRequest(IOException exception, int executionCount, HttpContext context);
 
 }

Added: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/BasicHttpContext.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/BasicHttpContext.java?rev=165528&view=auto
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/BasicHttpContext.java
(added)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/BasicHttpContext.java
Sun May  1 10:42:10 2005
@@ -0,0 +1,80 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 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.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.http.HttpContext;
+
+/**
+ * <p>
+ * </p>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+public class BasicHttpContext implements HttpContext {
+    
+    private final HttpContext parentContext;
+    private Map map = null;
+    
+    public BasicHttpContext(final HttpContext parentContext) {
+        super();
+        this.parentContext = parentContext;
+    }
+    
+    public Object getAttribute(final String id) {
+        if (id == null) {
+            throw new IllegalArgumentException("Id may not be null");
+        }
+        Object obj = null;
+        if (this.map != null) {
+            obj = this.map.get(id);
+        }
+        if (obj == null && this.parentContext != null) {
+            obj = this.parentContext.getAttribute(id);
+        }
+        return obj;
+    }
+
+    public void setAttribute(final String id, final Object obj) {
+        if (id == null) {
+            throw new IllegalArgumentException("Id may not be null");
+        }
+        if (this.map == null) {
+            this.map = new HashMap();
+        }
+        this.map.put(id, obj);
+    }
+    
+}

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/BasicHttpContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/BasicHttpContext.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/BasicHttpContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/DefaultHttpClientConnection.java?rev=165528&r1=165527&r2=165528&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
Sun May  1 10:42:10 2005
@@ -69,19 +69,23 @@
     private static final String EXPECT_CONTINUE = "100-Continue";
     private static final int CONTINUE_WAIT_MS = 3000;
     
-    private HttpHost targethost = null;
+    private final HttpHost targethost;
+    private final InetAddress localAddress;
     
-    public DefaultHttpClientConnection() {
+    public DefaultHttpClientConnection(final HttpHost targethost, final InetAddress localAddress)
{
         super();
-    }
-    
-    public void open(
-            final HttpHost targethost,
-            final HttpParams params,
-            final InetAddress localAddress) throws IOException {
         if (targethost == null) {
             throw new IllegalArgumentException("Target host may not be null");
         }
+        this.targethost = targethost;
+        this.localAddress = localAddress;
+    }
+    
+    public DefaultHttpClientConnection(final HttpHost targethost) {
+        this(targethost, null);
+    }
+    
+    public void open(final HttpParams params) throws IOException {
         if (params == null) {
             throw new IllegalArgumentException("HTTP parameters may not be null");
         }
@@ -89,17 +93,17 @@
         
         ProtocolSocketFactory socketfactory = targethost.getProtocol().getSocketFactory();
         Socket socket = socketfactory.createSocket(
-                targethost.getHostName(), targethost.getPort(), localAddress, 0, params);
+                this.targethost.getHostName(), this.targethost.getPort(), 
+                this.localAddress, 0, 
+                params);
         bind(socket, params);
-        this.targethost = targethost;
     }
     
-    public HttpHost getTargetHost() {
+    public HttpHost getHost() {
         return this.targethost;
     }
     
     public void close() throws IOException {
-        this.targethost = null;
         super.close();
     }
 



Mime
View raw message