You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by ve...@apache.org on 2017/01/17 19:27:47 UTC

svn commit: r1779239 - in /axis/axis2/java/core/trunk/modules/transport: http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/ http/src/org/apache/axis2/transport/http/ http/src/org/apache/axis2/transport/http/impl/httpclient4/

Author: veithen
Date: Tue Jan 17 19:27:47 2017
New Revision: 1779239

URL: http://svn.apache.org/viewvc?rev=1779239&view=rev
Log:
Make the HTTP transport code more generic so that we don't need a separate Java method per HTTP method.

Modified:
    axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/HTTPSenderImpl.java
    axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/RequestImpl.java
    axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java
    axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/HTTPSenderImpl.java
    axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/RequestImpl.java

Modified: axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/HTTPSenderImpl.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/HTTPSenderImpl.java?rev=1779239&r1=1779238&r2=1779239&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/HTTPSenderImpl.java (original)
+++ axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/HTTPSenderImpl.java Tue Jan 17 19:27:47 2017
@@ -45,10 +45,6 @@ import org.apache.commons.httpclient.Hea
 import org.apache.commons.httpclient.HttpConnectionManager;
 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 import org.apache.commons.httpclient.NameValuePair;
-import org.apache.commons.httpclient.methods.DeleteMethod;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -69,68 +65,10 @@ public class HTTPSenderImpl extends HTTP
         this.isAllowedRetry = isAllowedRetry;
     }
 
-    /**
-     * Used to send a request via HTTP Get method
-     * 
-     * @param msgContext
-     *            - The MessageContext of the message
-     * @param url
-     *            - The target URL
-     * @throws AxisFault
-     *             - Thrown in case an exception occurs
-     */
-    @Override
-    protected Request prepareGet(final MessageContext msgContext, final URL url)
-            throws AxisFault {
-        return new RequestImpl(this, msgContext, url, null, new GetMethod());
-    }
-
-    /**
-     * Used to send a request via HTTP Delete Method
-     * 
-     * @param msgContext
-     *            - The MessageContext of the message
-     * @param url
-     *            - The target URL
-     * @throws AxisFault
-     *             - Thrown in case an exception occurs
-     */
-    @Override
-    protected Request prepareDelete(final MessageContext msgContext, final URL url)
-            throws AxisFault {
-        return new RequestImpl(this, msgContext, url, null, new DeleteMethod());
-    }
-
-    /**
-     * Used to send a request via HTTP Post Method
-     * 
-     * @param msgContext
-     *            - The MessageContext of the message
-     * @param url
-     *            - The target URL
-     * @throws AxisFault
-     *             - Thrown in case an exception occurs
-     */
-    @Override
-    protected Request preparePost(final MessageContext msgContext, final URL url, AxisRequestEntity requestEntity)
-            throws AxisFault {
-        return new RequestImpl(this, msgContext, url, requestEntity, new PostMethod());
-    }
-
-    /**
-     * Used to send a request via HTTP Put Method
-     * 
-     * @param msgContext
-     *            - The MessageContext of the message
-     * @param url
-     *            - The target URL
-     * @throws AxisFault
-     *             - Thrown in case an exception occurs
-     */
     @Override
-    protected Request preparePut(final MessageContext msgContext, final URL url, AxisRequestEntity requestEntity)
-            throws AxisFault {
-        return new RequestImpl(this, msgContext, url, requestEntity, new PutMethod());
+    protected Request createRequest(MessageContext msgContext, String methodName, URL url,
+            AxisRequestEntity requestEntity) throws AxisFault {
+        return new RequestImpl(this, msgContext, methodName, url, requestEntity);
     }
 
     /**

Modified: axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/RequestImpl.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/RequestImpl.java?rev=1779239&r1=1779238&r2=1779239&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/RequestImpl.java (original)
+++ axis/axis2/java/core/trunk/modules/transport/http-hc3/src/main/java/org/apache/axis2/transport/http/impl/httpclient3/RequestImpl.java Tue Jan 17 19:27:47 2017
@@ -62,20 +62,37 @@ final class RequestImpl implements Reque
     protected final HttpClient httpClient;
     private final HostConfiguration config;
 
-    RequestImpl(HTTPSenderImpl sender, MessageContext msgContext, URL url, AxisRequestEntity requestEntity, HttpMethodBase method) throws AxisFault {
+    RequestImpl(HTTPSenderImpl sender, MessageContext msgContext, final String methodName, URL url,
+            AxisRequestEntity requestEntity) throws AxisFault {
         this.sender = sender;
         this.msgContext = msgContext;
         this.url = url;
-        this.method = method;
         httpClient = sender.getHttpClient(msgContext);
-        sender.populateCommonProperties(msgContext, url, method, httpClient);
-        if (requestEntity != null) {
-            ((EntityEnclosingMethod)method).setRequestEntity(new AxisRequestEntityImpl(requestEntity));
-    
+        if (requestEntity == null) {
+            method = new HttpMethodBase() {
+                @Override
+                public String getName() {
+                    return methodName;
+                }
+            };
+            // This mimicks GetMethod
+            if (methodName.equals(HTTPConstants.HTTP_METHOD_GET)) {
+                method.setFollowRedirects(true);
+            }
+        } else {
+            EntityEnclosingMethod entityEnclosingMethod = new EntityEnclosingMethod() {
+                @Override
+                public String getName() {
+                    return methodName;
+                }
+            };
+            entityEnclosingMethod.setRequestEntity(new AxisRequestEntityImpl(requestEntity));
             if (!sender.getHttpVersion().equals(HTTPConstants.HEADER_PROTOCOL_10) && sender.isChunked()) {
-                ((EntityEnclosingMethod)method).setContentChunked(true);
+                entityEnclosingMethod.setContentChunked(true);
             }
+            method = entityEnclosingMethod;
         }
+        sender.populateCommonProperties(msgContext, url, method, httpClient);
         // TODO: this is fishy; it means that we may end up modifying a HostConfiguration from a cached HTTP client
         HostConfiguration config = httpClient.getHostConfiguration();
         if (config == null) {

Modified: axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java?rev=1779239&r1=1779238&r2=1779239&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java (original)
+++ axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java Tue Jan 17 19:27:47 2017
@@ -45,46 +45,25 @@ import javax.xml.namespace.QName;
 public abstract class HTTPSender extends AbstractHTTPSender {
 
     private static final Log log = LogFactory.getLog(HTTPSender.class);
-    /**
-     * Used to send a request via HTTP Get method
-     *
-     * @param msgContext        - The MessageContext of the message
-     * @param url               - The target URL
-     * @throws AxisFault - Thrown in case an exception occurs
-     */
-    protected abstract Request prepareGet(MessageContext msgContext, URL url)
-            throws AxisFault;
     
     /**
-     * Used to send a request via HTTP Delete Method
-     *
-     * @param msgContext        - The MessageContext of the message
-     * @param url               - The target URL
-     * @throws AxisFault - Thrown in case an exception occurs
-     */
-    protected abstract Request prepareDelete(MessageContext msgContext, URL url)
-            throws AxisFault; 
-    /**
-     * Used to send a request via HTTP Post Method
-     *
-     * @param msgContext       - The MessageContext of the message
-     * @param url              - The target URL
-     * @throws AxisFault - Thrown in case an exception occurs
-     */
-    protected abstract Request preparePost(MessageContext msgContext, URL url,
-                             AxisRequestEntity requestEntity) throws AxisFault;
-
-
-    /**
-     * Used to send a request via HTTP Put Method
+     * Start a new HTTP request.
      *
-     * @param msgContext       - The MessageContext of the message
-     * @param url              - The target URL
-     * @throws AxisFault - Thrown in case an exception occurs
+     * @param msgContext
+     *            The MessageContext of the request message
+     * @param methodName
+     *            The HTTP method name
+     * @param url
+     *            The target URL
+     * @param requestEntity
+     *            The content of the request or {@code null} if the HTTP request shouldn't have any
+     *            content (e.g. for {@code GET} requests)
+     * @throws AxisFault
+     *            Thrown in case an exception occurs
      */
-    protected abstract Request preparePut(MessageContext msgContext, URL url,
-                            AxisRequestEntity requestEntity) throws AxisFault;
-
+    protected abstract Request createRequest(MessageContext msgContext, String methodName, URL url,
+            AxisRequestEntity requestEntity) throws AxisFault;
+    
     public void send(MessageContext msgContext, URL url, String soapActionString)
             throws IOException {
 
@@ -93,26 +72,27 @@ public abstract class HTTPSender extends
 
         String httpMethod =
                 (String) msgContext.getProperty(Constants.Configuration.HTTP_METHOD);
+        if (httpMethod == null) {
+            httpMethod = Constants.Configuration.HTTP_METHOD_POST;
+        }
 
         MessageFormatter messageFormatter = MessageProcessorSelector
                 .getMessageFormatter(msgContext);
         url = messageFormatter.getTargetAddress(msgContext, format, url);
         
-        Request request = null;
-        if ((httpMethod != null)) {
-
-            if (Constants.Configuration.HTTP_METHOD_GET.equalsIgnoreCase(httpMethod)) {
-                request = prepareGet(msgContext, url);
-            } else if (Constants.Configuration.HTTP_METHOD_DELETE.equalsIgnoreCase(httpMethod)) {
-                request = prepareDelete(msgContext, url);
-            } else if (Constants.Configuration.HTTP_METHOD_PUT.equalsIgnoreCase(httpMethod)) {
-                request = preparePut(msgContext, url, buildRequestEntity(messageFormatter, msgContext, soapActionString));
-            }
+        AxisRequestEntity requestEntity;
+        if (Constants.Configuration.HTTP_METHOD_GET.equalsIgnoreCase(httpMethod)
+                || Constants.Configuration.HTTP_METHOD_DELETE.equalsIgnoreCase(httpMethod)) {
+            requestEntity = null;
+        } else if (Constants.Configuration.HTTP_METHOD_POST.equalsIgnoreCase(httpMethod)
+                || Constants.Configuration.HTTP_METHOD_PUT.equalsIgnoreCase(httpMethod)) {
+            requestEntity = new AxisRequestEntity(messageFormatter, msgContext, format,
+                    soapActionString, chunked, isAllowedRetry);
+        } else {
+            throw new AxisFault("Unsupported HTTP method " + httpMethod);
         }
 
-        if (request == null) {
-            request = preparePost(msgContext, url, buildRequestEntity(messageFormatter, msgContext, soapActionString));
-        }
+        Request request = createRequest(msgContext, httpMethod, url, requestEntity);
 
         if (msgContext.getOptions() != null && msgContext.getOptions().isManageSession()) {
             // setting the cookie in the out path
@@ -153,12 +133,6 @@ public abstract class HTTPSender extends
         request.execute();
     }   
 
-    private AxisRequestEntity buildRequestEntity(MessageFormatter messageFormatter, MessageContext msgContext,
-            String soapActionString) {
-        return new AxisRequestEntity(messageFormatter, msgContext, format,
-                soapActionString, chunked, isAllowedRetry);
-    }
-
     private void addCustomHeaders(MessageContext msgContext, Request request) {
     
         boolean isCustomUserAgentSet = false;

Modified: axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/HTTPSenderImpl.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/HTTPSenderImpl.java?rev=1779239&r1=1779238&r2=1779239&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/HTTPSenderImpl.java (original)
+++ axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/HTTPSenderImpl.java Tue Jan 17 19:27:47 2017
@@ -37,10 +37,6 @@ import org.apache.http.HeaderElement;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpPut;
 import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.client.params.AuthPolicy;
 import org.apache.http.client.params.ClientPNames;
@@ -83,58 +79,10 @@ public class HTTPSenderImpl extends HTTP
         this.isAllowedRetry = isAllowedRetry;
     }
 
-    /**
-     * Used to send a request via HTTP Get method
-     *
-     * @param msgContext        - The MessageContext of the message
-     * @param url               - The target URL
-     * @param soapActionString - The soapAction string of the request
-     * @throws org.apache.axis2.AxisFault - Thrown in case an exception occurs
-     */
-    @Override
-    protected Request prepareGet(final MessageContext msgContext, final URL url)
-            throws AxisFault {
-        return new RequestImpl(this, msgContext, url, null, new HttpGet());
-    }
-
-    /**
-     * Used to send a request via HTTP Delete Method
-     *
-     * @param msgContext        - The MessageContext of the message
-     * @param url               - The target URL
-     * @param soapActionString - The soapAction string of the request
-     * @throws org.apache.axis2.AxisFault - Thrown in case an exception occurs
-     */
-    @Override
-    protected Request prepareDelete(final MessageContext msgContext, final URL url)
-            throws AxisFault {
-        return new RequestImpl(this, msgContext, url, null, new HttpDelete());
-    }
-
-    /**
-     * Used to send a request via HTTP Post Method
-     *
-     * @param msgContext       - The MessageContext of the message
-     * @param url              - The target URL
-     * @throws org.apache.axis2.AxisFault - Thrown in case an exception occurs
-     */
-    @Override
-    protected Request preparePost(final MessageContext msgContext, final URL url, AxisRequestEntity requestEntity)
-            throws AxisFault {
-        return new RequestImpl(this, msgContext, url, requestEntity, new HttpPost());
-    }
-
-    /**
-     * Used to send a request via HTTP Put Method
-     *
-     * @param msgContext       - The MessageContext of the message
-     * @param url              - The target URL
-     * @throws org.apache.axis2.AxisFault - Thrown in case an exception occurs
-     */
     @Override
-    protected Request preparePut(final MessageContext msgContext, final URL url, AxisRequestEntity requestEntity)
-            throws AxisFault {
-        return new RequestImpl(this, msgContext, url, requestEntity, new HttpPut());
+    protected Request createRequest(MessageContext msgContext, String methodName, URL url,
+            AxisRequestEntity requestEntity) throws AxisFault {
+        return new RequestImpl(this, msgContext, methodName, url, requestEntity);
     }
 
     /**

Modified: axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/RequestImpl.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/RequestImpl.java?rev=1779239&r1=1779238&r2=1779239&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/RequestImpl.java (original)
+++ axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/impl/httpclient4/RequestImpl.java Tue Jan 17 19:27:47 2017
@@ -38,7 +38,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
-import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
@@ -48,6 +47,7 @@ import org.apache.http.auth.Credentials;
 import org.apache.http.auth.NTCredentials;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.auth.params.AuthPNames;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
 import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.client.params.AuthPolicy;
 import org.apache.http.client.params.ClientPNames;
@@ -69,21 +69,33 @@ final class RequestImpl implements Reque
     protected final AbstractHttpClient httpClient;
     private final HttpHost httpHost;
 
-    RequestImpl(HTTPSenderImpl sender, MessageContext msgContext, URL url, AxisRequestEntity requestEntity, HttpRequestBase method) throws AxisFault {
+    RequestImpl(HTTPSenderImpl sender, MessageContext msgContext, final String methodName, URL url,
+            AxisRequestEntity requestEntity) throws AxisFault {
         this.sender = sender;
         this.msgContext = msgContext;
         this.url = url;
-        this.method = method;
         httpClient = sender.getHttpClient(msgContext);
-        sender.populateCommonProperties(msgContext, url, method, httpClient);
-        if (requestEntity != null) {
-            AxisRequestEntityImpl requestEntityAdapter = new AxisRequestEntityImpl(requestEntity);
-            ((HttpEntityEnclosingRequest)method).setEntity(requestEntityAdapter);
-    
+        if (requestEntity == null) {
+            method = new HttpRequestBase() {
+                @Override
+                public String getMethod() {
+                    return methodName;
+                }
+            };
+        } else {
+            HttpEntityEnclosingRequestBase entityEnclosingRequest = new HttpEntityEnclosingRequestBase() {
+                @Override
+                public String getMethod() {
+                    return methodName;
+                }
+            };
+            entityEnclosingRequest.setEntity(new AxisRequestEntityImpl(requestEntity));
             if (!sender.getHttpVersion().equals(HTTPConstants.HEADER_PROTOCOL_10) && sender.isChunked()) {
                 requestEntity.setChunked(sender.isChunked());
             }
+            method = entityEnclosingRequest;
         }
+        sender.populateCommonProperties(msgContext, url, method, httpClient);
         int port = url.getPort();
         String protocol = url.getProtocol();
         if (port == -1) {