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/16 23:08:36 UTC

svn commit: r1779120 - 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: Mon Jan 16 23:08:36 2017
New Revision: 1779120

URL: http://svn.apache.org/viewvc?rev=1779120&view=rev
Log:
Simplify the authentication code.

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/Request.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=1779120&r1=1779119&r2=1779120&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 Mon Jan 16 23:08:36 2017
@@ -272,10 +272,6 @@ public class HTTPSenderImpl extends HTTP
         }
     }
 
-    protected boolean isAuthenticationEnabled(MessageContext msgCtx) {
-        return (msgCtx.getProperty(HTTPConstants.AUTHENTICATE) != null);
-    }
-
     /**
      * Method used to copy all the common properties
      * 
@@ -298,10 +294,6 @@ public class HTTPSenderImpl extends HTTP
             HttpMethodBase httpMethod, HttpClient httpClient)
             throws AxisFault {
 
-        if (isAuthenticationEnabled(msgContext)) {
-            httpMethod.setDoAuthentication(true);
-        }
-
         httpMethod.setPath(url.getPath());
 
         httpMethod.setQueryString(url.getQuery());

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=1779120&r1=1779119&r2=1779120&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 Mon Jan 16 23:08:36 2017
@@ -220,7 +220,6 @@ final class RequestImpl implements Reque
      */
     protected void populateHostConfiguration() throws AxisFault {
 
-        boolean isAuthenticationEnabled = sender.isAuthenticationEnabled(msgContext);
         int port = url.getPort();
 
         String protocol = url.getProtocol();
@@ -247,10 +246,6 @@ final class RequestImpl implements Reque
             config.setHost(url.getHost(), port, protocolHandler);
         }
 
-        if (isAuthenticationEnabled) {
-            // Basic, Digest, NTLM and custom authentications.
-            setAuthenticationInfo();
-        }
         // proxy configuration
 
         if (HTTPProxyConfigurator.isProxyEnabled(msgContext, url)) {
@@ -266,82 +261,73 @@ final class RequestImpl implements Reque
      * or Basic Authentication. Apart from that user can change the priory or
      * add a custom authentication scheme.
      */
-    private void setAuthenticationInfo() throws AxisFault {
-        HTTPAuthenticator authenticator;
-        Object obj = msgContext.getProperty(HTTPConstants.AUTHENTICATE);
-        if (obj != null) {
-            if (obj instanceof HTTPAuthenticator) {
-                authenticator = (HTTPAuthenticator) obj;
-
-                String username = authenticator.getUsername();
-                String password = authenticator.getPassword();
-                String host = authenticator.getHost();
-                String domain = authenticator.getDomain();
-
-                int port = authenticator.getPort();
-                String realm = authenticator.getRealm();
-
-                /* If retrying is available set it first */
-                sender.setAllowedRetry(authenticator.isAllowedRetry());
-
-                Credentials creds;
-
-                HttpState tmpHttpState = null;
-                HttpState httpState = (HttpState) msgContext
-                        .getProperty(HTTPConstants.CACHED_HTTP_STATE);
-                if (httpState != null) {
-                    tmpHttpState = httpState;
-                } else {
-                    tmpHttpState = httpClient.getState();
-                }
-
-                httpClient.getParams().setAuthenticationPreemptive(
-                        authenticator.getPreemptiveAuthentication());
-
-                if (host != null) {
-                    if (domain != null) {
-                        /* Credentials for NTLM Authentication */
-                        creds = new NTCredentials(username, password, host, domain);
-                    } else {
-                        /* Credentials for Digest and Basic Authentication */
-                        creds = new UsernamePasswordCredentials(username, password);
-                    }
-                    tmpHttpState.setCredentials(new AuthScope(host, port, realm), creds);
-                } else {
-                    if (domain != null) {
-                        /*
-                         * Credentials for NTLM Authentication when host is
-                         * ANY_HOST
-                         */
-                        creds = new NTCredentials(username, password, AuthScope.ANY_HOST, domain);
-                        tmpHttpState.setCredentials(new AuthScope(AuthScope.ANY_HOST, port, realm),
-                                creds);
-                    } else {
-                        /* Credentials only for Digest and Basic Authentication */
-                        creds = new UsernamePasswordCredentials(username, password);
-                        tmpHttpState.setCredentials(new AuthScope(AuthScope.ANY), creds);
-                    }
-                }
-                /* Customizing the priority Order */
-                List schemes = authenticator.getAuthSchemes();
-                if (schemes != null && schemes.size() > 0) {
-                    List authPrefs = new ArrayList(3);
-                    for (int i = 0; i < schemes.size(); i++) {
-                        if (schemes.get(i) instanceof AuthPolicy) {
-                            authPrefs.add(schemes.get(i));
-                            continue;
-                        }
-                        String scheme = (String) schemes.get(i);
-                        authPrefs.add(authenticator.getAuthPolicyPref(scheme));
+    @Override
+    public void enableAuthentication(HTTPAuthenticator authenticator) {
+        method.setDoAuthentication(true);
+
+        String username = authenticator.getUsername();
+        String password = authenticator.getPassword();
+        String host = authenticator.getHost();
+        String domain = authenticator.getDomain();
+
+        int port = authenticator.getPort();
+        String realm = authenticator.getRealm();
+
+        /* If retrying is available set it first */
+        sender.setAllowedRetry(authenticator.isAllowedRetry());
+
+        Credentials creds;
+
+        HttpState tmpHttpState = null;
+        HttpState httpState = (HttpState) msgContext
+                .getProperty(HTTPConstants.CACHED_HTTP_STATE);
+        if (httpState != null) {
+            tmpHttpState = httpState;
+        } else {
+            tmpHttpState = httpClient.getState();
+        }
 
-                    }
-                    httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
-                }
+        httpClient.getParams().setAuthenticationPreemptive(
+                authenticator.getPreemptiveAuthentication());
 
+        if (host != null) {
+            if (domain != null) {
+                /* Credentials for NTLM Authentication */
+                creds = new NTCredentials(username, password, host, domain);
+            } else {
+                /* Credentials for Digest and Basic Authentication */
+                creds = new UsernamePasswordCredentials(username, password);
+            }
+            tmpHttpState.setCredentials(new AuthScope(host, port, realm), creds);
+        } else {
+            if (domain != null) {
+                /*
+                 * Credentials for NTLM Authentication when host is
+                 * ANY_HOST
+                 */
+                creds = new NTCredentials(username, password, AuthScope.ANY_HOST, domain);
+                tmpHttpState.setCredentials(new AuthScope(AuthScope.ANY_HOST, port, realm),
+                        creds);
             } else {
-                throw new AxisFault("HttpTransportProperties.Authenticator class cast exception");
+                /* Credentials only for Digest and Basic Authentication */
+                creds = new UsernamePasswordCredentials(username, password);
+                tmpHttpState.setCredentials(new AuthScope(AuthScope.ANY), creds);
             }
         }
+        /* Customizing the priority Order */
+        List schemes = authenticator.getAuthSchemes();
+        if (schemes != null && schemes.size() > 0) {
+            List authPrefs = new ArrayList(3);
+            for (int i = 0; i < schemes.size(); i++) {
+                if (schemes.get(i) instanceof AuthPolicy) {
+                    authPrefs.add(schemes.get(i));
+                    continue;
+                }
+                String scheme = (String) schemes.get(i);
+                authPrefs.add(authenticator.getAuthPolicyPref(scheme));
 
+            }
+            httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
+        }
     }
 }

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=1779120&r1=1779119&r2=1779120&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 Mon Jan 16 23:08:36 2017
@@ -143,6 +143,15 @@ public abstract class HTTPSender extends
         // set the custom headers, if available
         addCustomHeaders(msgContext, request);
         
+        Object obj = msgContext.getProperty(HTTPConstants.AUTHENTICATE);
+        if (obj != null) {
+            if (obj instanceof HTTPAuthenticator) {
+                request.enableAuthentication((HTTPAuthenticator) obj);
+            } else {
+                throw new AxisFault("HttpTransportProperties.Authenticator class cast exception");
+            }
+        }
+
         request.execute();
     }   
 

Modified: axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/Request.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/Request.java?rev=1779120&r1=1779119&r2=1779120&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/Request.java (original)
+++ axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/Request.java Mon Jan 16 23:08:36 2017
@@ -29,5 +29,6 @@ public interface Request {
     void setHeader(String name, String value);
     void addHeader(String name, String value);
     NamedValue[] getRequestHeaders();
+    void enableAuthentication(HTTPAuthenticator authenticator);
     void execute() throws AxisFault;
 }

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=1779120&r1=1779119&r2=1779120&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 Mon Jan 16 23:08:36 2017
@@ -273,10 +273,6 @@ public class HTTPSenderImpl extends HTTP
         }
     }
 
-    protected boolean isAuthenticationEnabled(MessageContext msgCtx) {
-        return (msgCtx.getProperty(HTTPConstants.AUTHENTICATE) != null);
-    }
-
     /**
      * Method used to copy all the common properties
      *
@@ -294,10 +290,6 @@ public class HTTPSenderImpl extends HTTP
                                                         AbstractHttpClient httpClient)
             throws AxisFault {
 
-        if (isAuthenticationEnabled(msgContext)) {
-            httpMethod.getParams().setBooleanParameter(ClientPNames.HANDLE_AUTHENTICATION, true);
-        }
-
         try {
             httpMethod.setURI(url.toURI());
         } catch (URISyntaxException e) {

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=1779120&r1=1779119&r2=1779120&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 Mon Jan 16 23:08:36 2017
@@ -242,15 +242,6 @@ final class RequestImpl implements Reque
      * @throws org.apache.axis2.AxisFault if problems occur
      */
     private void populateHostConfiguration() throws AxisFault {
-
-        boolean isAuthenticationEnabled = sender.isAuthenticationEnabled(msgContext);
-
-        // TODO : one might need to set his own socket factory. We have to allow that case as well.
-
-        if (isAuthenticationEnabled) {
-            // Basic, Digest, NTLM and custom authentications.
-            setAuthenticationInfo();
-        }
         // proxy configuration
 
         if (HTTPProxyConfigurator.isProxyEnabled(msgContext, url)) {
@@ -266,77 +257,68 @@ final class RequestImpl implements Reque
      * or Basic Authentication. Apart from that user can change the priory or
      * add a custom authentication scheme.
      */
-    private void setAuthenticationInfo() throws AxisFault {
-        HTTPAuthenticator authenticator;
-        Object obj = msgContext.getProperty(HTTPConstants.AUTHENTICATE);
-        if (obj != null) {
-            if (obj instanceof HTTPAuthenticator) {
-                authenticator = (HTTPAuthenticator) obj;
-
-                String username = authenticator.getUsername();
-                String password = authenticator.getPassword();
-                String host = authenticator.getHost();
-                String domain = authenticator.getDomain();
-
-                int port = authenticator.getPort();
-                String realm = authenticator.getRealm();
-
-                /* If retrying is available set it first */
-                sender.setAllowedRetry(authenticator.isAllowedRetry());
-
-                Credentials creds;
-
-                // TODO : Set preemptive authentication, but its not recommended in HC 4
-
-                if (host != null) {
-                    if (domain != null) {
-                        /* Credentials for NTLM Authentication */
-                        httpClient.getAuthSchemes().register("ntlm",new NTLMSchemeFactory());
-                        creds = new NTCredentials(username, password, host, domain);
-                    } else {
-                        /* Credentials for Digest and Basic Authentication */
-                        creds = new UsernamePasswordCredentials(username, password);
-                    }
-                    httpClient.getCredentialsProvider().
-                            setCredentials(new AuthScope(host, port, realm), creds);
-                } else {
-                    if (domain != null) {
-                        /*
-                         * Credentials for NTLM Authentication when host is
-                         * ANY_HOST
-                         */
-                        httpClient.getAuthSchemes().register("ntlm",new NTLMSchemeFactory());
-                        creds = new NTCredentials(username, password, AuthScope.ANY_HOST, domain);
-                        httpClient.getCredentialsProvider().
-                                setCredentials(new AuthScope(AuthScope.ANY_HOST, port, realm),
-                                               creds);
-                    } else {
-                        /* Credentials only for Digest and Basic Authentication */
-                        creds = new UsernamePasswordCredentials(username, password);
-                        httpClient.getCredentialsProvider().
-                                setCredentials(new AuthScope(AuthScope.ANY), creds);
-                    }
-                }
-                /* Customizing the priority Order */
-                List schemes = authenticator.getAuthSchemes();
-                if (schemes != null && schemes.size() > 0) {
-                    List authPrefs = new ArrayList(3);
-                    for (int i = 0; i < schemes.size(); i++) {
-                        if (schemes.get(i) instanceof AuthPolicy) {
-                            authPrefs.add(schemes.get(i));
-                            continue;
-                        }
-                        String scheme = (String) schemes.get(i);
-                        authPrefs.add(authenticator.getAuthPolicyPref(scheme));
-
-                    }
-                    httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authPrefs);
-                }
-
+    @Override
+    public void enableAuthentication(HTTPAuthenticator authenticator) {
+        method.getParams().setBooleanParameter(ClientPNames.HANDLE_AUTHENTICATION, true);
+
+        String username = authenticator.getUsername();
+        String password = authenticator.getPassword();
+        String host = authenticator.getHost();
+        String domain = authenticator.getDomain();
+
+        int port = authenticator.getPort();
+        String realm = authenticator.getRealm();
+
+        /* If retrying is available set it first */
+        sender.setAllowedRetry(authenticator.isAllowedRetry());
+
+        Credentials creds;
+
+        // TODO : Set preemptive authentication, but its not recommended in HC 4
+
+        if (host != null) {
+            if (domain != null) {
+                /* Credentials for NTLM Authentication */
+                httpClient.getAuthSchemes().register("ntlm",new NTLMSchemeFactory());
+                creds = new NTCredentials(username, password, host, domain);
+            } else {
+                /* Credentials for Digest and Basic Authentication */
+                creds = new UsernamePasswordCredentials(username, password);
+            }
+            httpClient.getCredentialsProvider().
+                    setCredentials(new AuthScope(host, port, realm), creds);
+        } else {
+            if (domain != null) {
+                /*
+                 * Credentials for NTLM Authentication when host is
+                 * ANY_HOST
+                 */
+                httpClient.getAuthSchemes().register("ntlm",new NTLMSchemeFactory());
+                creds = new NTCredentials(username, password, AuthScope.ANY_HOST, domain);
+                httpClient.getCredentialsProvider().
+                        setCredentials(new AuthScope(AuthScope.ANY_HOST, port, realm),
+                                       creds);
             } else {
-                throw new AxisFault("HttpTransportProperties.Authenticator class cast exception");
+                /* Credentials only for Digest and Basic Authentication */
+                creds = new UsernamePasswordCredentials(username, password);
+                httpClient.getCredentialsProvider().
+                        setCredentials(new AuthScope(AuthScope.ANY), creds);
             }
         }
+        /* Customizing the priority Order */
+        List schemes = authenticator.getAuthSchemes();
+        if (schemes != null && schemes.size() > 0) {
+            List authPrefs = new ArrayList(3);
+            for (int i = 0; i < schemes.size(); i++) {
+                if (schemes.get(i) instanceof AuthPolicy) {
+                    authPrefs.add(schemes.get(i));
+                    continue;
+                }
+                String scheme = (String) schemes.get(i);
+                authPrefs.add(authenticator.getAuthPolicyPref(scheme));
 
+            }
+            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authPrefs);
+        }
     }
 }