hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1348409 - in /httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec: MainRequestExecutor.java ProtocolFacade.java
Date Sat, 09 Jun 2012 13:35:56 GMT
Author: olegk
Date: Sat Jun  9 13:35:56 2012
New Revision: 1348409

URL: http://svn.apache.org/viewvc?rev=1348409&view=rev
Log:
Factored protocol handling logic into a separate facade

Added:
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolFacade.java
  (with props)
Modified:
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java?rev=1348409&r1=1348408&r2=1348409&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java
(original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainRequestExecutor.java
Sat Jun  9 13:35:56 2012
@@ -29,8 +29,6 @@ package org.apache.http.impl.client.exec
 
 import java.io.IOException;
 import java.io.InterruptedIOException;
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.logging.Log;
@@ -42,7 +40,6 @@ import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
-import org.apache.http.ProtocolException;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.annotation.ThreadSafe;
 import org.apache.http.auth.AuthProtocolState;
@@ -54,7 +51,6 @@ import org.apache.http.client.UserTokenH
 import org.apache.http.client.methods.AbortableHttpRequest;
 import org.apache.http.client.params.HttpClientParams;
 import org.apache.http.client.protocol.ClientContext;
-import org.apache.http.client.utils.URIUtils;
 import org.apache.http.conn.BasicManagedEntity;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.ClientConnectionRequest;
@@ -99,17 +95,8 @@ import org.apache.http.util.EntityUtils;
  *  <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li>
  *  <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
  *  <li>{@link org.apache.http.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}</li>
- *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
- *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
- *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
- *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li>
- *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li>
  *  <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
- *  <li>{@link org.apache.http.client.params.ClientPNames#COOKIE_POLICY}</li>
  *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li>
- *  <li>{@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST}</li>
- *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}</li>
- *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}</li>
  *  <li>{@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}</li>
  * </ul>
  *
@@ -176,34 +163,6 @@ public class MainRequestExecutor impleme
         this.params             = params;
     }
 
-    private void rewriteRequestURI(
-            final HttpRequestWrapper request,
-            final HttpRoute route) throws ProtocolException {
-        try {
-            URI uri = request.getURI();
-            if (route.getProxyHost() != null && !route.isTunnelled()) {
-                // Make sure the request URI is absolute
-                if (!uri.isAbsolute()) {
-                    HttpHost target = route.getTargetHost();
-                    uri = URIUtils.rewriteURI(uri, target, true);
-                } else {
-                    uri = URIUtils.rewriteURI(uri);
-                }
-            } else {
-                // Make sure the request URI is relative
-                if (uri.isAbsolute()) {
-                    uri = URIUtils.rewriteURI(uri, null);
-                } else {
-                    uri = URIUtils.rewriteURI(uri);
-                }
-            }
-            request.setURI(uri);
-        } catch (URISyntaxException ex) {
-            throw new ProtocolException("Invalid URI: " +
-                    request.getRequestLine().getUri(), ex);
-        }
-    }
-
     public HttpResponse execute(
             final HttpRoute route,
             final HttpRequestWrapper request,
@@ -218,29 +177,9 @@ public class MainRequestExecutor impleme
             proxyAuthState = new AuthState();
         }
 
-        HttpHost target = route.getTargetHost();
-        HttpHost proxy = route.getProxyHost();
-
         HttpRequest original = request.getOriginal();
-        
-        // Save original request headers
         Header[] origheaders = request.getAllHeaders();
 
-        // Re-write request URI if needed
-        rewriteRequestURI(request, route);
-
-        HttpHost virtualHost = request.getVirtualHost();
-        // HTTPCLIENT-1092 - add the port if necessary
-        if (virtualHost != null && virtualHost.getPort() == -1) {
-            int port = target.getPort();
-            if (port != -1){
-                virtualHost = new HttpHost(virtualHost.getHostName(), port, virtualHost.getSchemeName());
-            }
-        }
-
-        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, virtualHost != null ? virtualHost
: target);
-        context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
-
         Object userToken = context.getAttribute(ClientContext.USER_TOKEN);
 
         ManagedClientConnection managedConn = null;

Added: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolFacade.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolFacade.java?rev=1348409&view=auto
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolFacade.java
(added)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolFacade.java
Sat Jun  9 13:35:56 2012
@@ -0,0 +1,137 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.client.exec;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.ProtocolException;
+import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.client.utils.URIUtils;
+import org.apache.http.conn.routing.HttpRoute;
+import org.apache.http.protocol.ExecutionContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpProcessor;
+
+/**
+ * @since 4.3
+ */
+@ThreadSafe
+public class ProtocolFacade implements HttpClientRequestExecutor {
+
+    private final Log log = LogFactory.getLog(getClass());
+
+    private final HttpClientRequestExecutor requestExecutor;
+    private final HttpProcessor httpProcessor;
+
+    public ProtocolFacade(
+            final HttpClientRequestExecutor requestExecutor,
+            final HttpProcessor httpProcessor) {
+        if (requestExecutor == null) {
+            throw new IllegalArgumentException("HTTP client request executor may not be null");
+        }
+        if (httpProcessor == null) {
+            throw new IllegalArgumentException("HTTP protocol processor may not be null");
+        }
+        this.requestExecutor = requestExecutor;
+        this.httpProcessor = httpProcessor;
+    }
+
+    private void rewriteRequestURI(
+            final HttpRequestWrapper request,
+            final HttpRoute route) throws ProtocolException {
+        try {
+            URI uri = request.getURI();
+            if (route.getProxyHost() != null && !route.isTunnelled()) {
+                // Make sure the request URI is absolute
+                if (!uri.isAbsolute()) {
+                    HttpHost target = route.getTargetHost();
+                    uri = URIUtils.rewriteURI(uri, target, true);
+                } else {
+                    uri = URIUtils.rewriteURI(uri);
+                }
+            } else {
+                // Make sure the request URI is relative
+                if (uri.isAbsolute()) {
+                    uri = URIUtils.rewriteURI(uri, null);
+                } else {
+                    uri = URIUtils.rewriteURI(uri);
+                }
+            }
+            request.setURI(uri);
+        } catch (URISyntaxException ex) {
+            throw new ProtocolException("Invalid URI: " +
+                    request.getRequestLine().getUri(), ex);
+        }
+    }
+
+    public HttpResponse execute(
+            final HttpRoute route,
+            final HttpRequestWrapper request,
+            final HttpContext context) throws HttpException, IOException {
+        HttpHost target = route.getTargetHost();
+        HttpHost proxy = route.getProxyHost();
+
+        // Re-write request URI if needed
+        rewriteRequestURI(request, route);
+
+        HttpHost virtualHost = request.getVirtualHost();
+        // HTTPCLIENT-1092 - add the port if necessary
+        if (virtualHost != null && virtualHost.getPort() == -1) {
+            int port = target.getPort();
+            if (port != -1){
+                virtualHost = new HttpHost(virtualHost.getHostName(), port, virtualHost.getSchemeName());
+            }
+            if (this.log.isDebugEnabled()) {
+                this.log.debug("Using virtual host" + virtualHost);
+            }
+        }
+        
+        // Run request protocol interceptors
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, virtualHost != null ? virtualHost
: target);
+        context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
+        context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
+
+        this.httpProcessor.process(request, context);
+        
+        HttpResponse response = this.requestExecutor.execute(route, request, context);
+        
+        // Run response protocol interceptors
+        context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
+        this.httpProcessor.process(response, context);
+        
+        return response;
+    }
+
+}

Propchange: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolFacade.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolFacade.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolFacade.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message