You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by sa...@apache.org on 2007/06/15 09:33:40 UTC

svn commit: r547565 - in /webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http: AbstractHTTPSender.java HttpTransportProperties.java ProxyConfiguration.java

Author: saminda
Date: Fri Jun 15 00:33:39 2007
New Revision: 547565

URL: http://svn.apache.org/viewvc?view=rev&rev=547565
Log:
1. JIRA Fixed 2214 : This is a feature. 
2. JIRA 2316 has been fixed before 1.2 release. 
3. New proxy configuration via Axis2.xml is added 
ex: 
<parameter name="Proxy">
      <Configuration>
             <ProxyHost>example.org</ProxyHost>
             <ProxyPort>5678</ProxyPort>
             <ProxyUser>EXAMPLE\\saminda</ProxyUser> 
             <ProxyPassword>ppp</ProxyPassword>
      </Configuration>
</parameter>

4. There are four ways to configure the proxy right now.

    1. Using PROXY parameter (old configuration) under http transport sender: This way is not recommended and 
inefficient. 
    2. Using "Proxy" parameter in Axis2.xml
    3. Using Java Networking Properties 
            http.proxyHost (default: <none>)
            http.proxyPort (default: 80 if http.proxyHost specified)
            http.nonProxyHosts (default: <none>

       For the above usage please refer to : http://java.sun.com/features/2002/11/hilevel_network.html
    4. In runtime via HttpTransportProperties.ProxyProperties and HttpConstants.PROXY

5. Added ProxyConfiguration to configure proxy and cleanup the code

6. Cleanup the HttpTransportProperties 

6. TODO test the old configuration/Adding Paul suggested "ProxyURL" 
      

Added:
    webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/ProxyConfiguration.java
Modified:
    webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/AbstractHTTPSender.java
    webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HttpTransportProperties.java

Modified: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/AbstractHTTPSender.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/AbstractHTTPSender.java?view=diff&rev=547565&r1=547564&r2=547565
==============================================================================
--- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/AbstractHTTPSender.java (original)
+++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/AbstractHTTPSender.java Fri Jun 15 00:33:39 2007
@@ -21,15 +21,13 @@
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
-import org.apache.axis2.transport.MessageFormatter;
-import org.apache.axis2.transport.TransportUtils;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.context.OperationContext;
-import org.apache.axis2.description.Parameter;
 import org.apache.axis2.description.TransportOutDescription;
 import org.apache.axis2.i18n.Messages;
+import org.apache.axis2.transport.MessageFormatter;
+import org.apache.axis2.transport.TransportUtils;
 import org.apache.axis2.util.JavaUtils;
-import org.apache.axis2.util.Utils;
 import org.apache.axis2.wsdl.WSDLConstants;
 import org.apache.commons.httpclient.*;
 import org.apache.commons.httpclient.auth.AuthPolicy;
@@ -44,7 +42,6 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.zip.GZIPInputStream;
@@ -93,108 +90,6 @@
     }
 
     /**
-     * Helper method for Proxy and NTLM authentication
-     *
-     * @param client HttpClient in which to place proxy config
-     * @param proxySetting TransportOutDescription
-     * @param config HostConfiguraiton in which to place proxy config
-     * @param msgCtx the active MessageContext
-     * @throws AxisFault in case of problems
-     */
-    protected void configProxyAuthentication(HttpClient client,
-                                             TransportOutDescription proxySetting,
-                                             HostConfiguration config,
-                                             MessageContext msgCtx)
-            throws AxisFault {
-        Parameter proxyParam = proxySetting.getParameter(HTTPConstants.PROXY);
-        String usrName;
-        String domain;
-        String passwd;
-        Credentials proxyCred = null;
-        String proxyHostName = null;
-        int proxyPort = -1;
-
-        if (proxyParam != null) {
-            String value = (String) proxyParam.getValue();
-            String split[] = value.split(":");
-
-            // values being hard coded due best practise
-            usrName = split[0];
-            domain = split[1];
-            passwd = split[2];
-
-            OMElement proxyParamElement = proxyParam.getParameterElement();
-            Iterator ite = proxyParamElement.getAllAttributes();
-
-            while (ite.hasNext()) {
-                OMAttribute att = (OMAttribute) ite.next();
-
-                if (att.getLocalName().equalsIgnoreCase(PROXY_HOST_NAME)) {
-                    proxyHostName = att.getAttributeValue();
-                }
-
-                if (att.getLocalName().equalsIgnoreCase(PROXY_PORT)) {
-                    proxyPort = Integer.parseInt(att.getAttributeValue());
-                }
-            }
-
-            if (domain.length() == 0 || domain.equals(ANONYMOUS)) {
-                if (usrName.equals(ANONYMOUS) && passwd.equals(ANONYMOUS)) {
-                    proxyCred = new UsernamePasswordCredentials("", "");
-                } else {
-                    proxyCred = new UsernamePasswordCredentials(usrName,
-                            passwd);    // proxy
-                }
-            } else {
-                proxyCred = new NTCredentials(usrName, passwd, proxyHostName,
-                        domain);    // NTLM authentication with additionals prams
-            }
-        }
-
-        HttpTransportProperties.ProxyProperties proxyProperties =
-                (HttpTransportProperties.ProxyProperties) msgCtx
-                        .getProperty(HTTPConstants.PROXY);
-
-        if (proxyProperties != null) {
-            if (proxyProperties.getProxyPort() != -1) {
-                proxyPort = proxyProperties.getProxyPort();
-            }
-
-            proxyHostName = proxyProperties.getProxyHostName();
-            if (proxyHostName == null
-                    || proxyHostName.length() == 0) {
-                throw new AxisFault("Proxy Name is not valid");
-            }
-
-            if (proxyProperties.getUserName().equals(ANONYMOUS)
-                    || proxyProperties.getPassWord().equals(ANONYMOUS)) {
-                proxyCred = new UsernamePasswordCredentials("", "");
-            }
-            if (!proxyProperties.getUserName().equals(ANONYMOUS) &&
-                    !proxyProperties.getPassWord().equals(ANONYMOUS)) {
-                proxyCred = new UsernamePasswordCredentials(
-                        proxyProperties.getUserName().trim(),
-                        proxyProperties
-                                .getPassWord().trim()); // Basic Authentication
-            }
-            if (!proxyProperties.getDomain().equals(ANONYMOUS)) {
-                if (!proxyProperties.getUserName().equals(ANONYMOUS) &&
-                        !proxyProperties.getPassWord().equals(ANONYMOUS) &&
-                        !proxyProperties.getDomain().equals(ANONYMOUS)) {
-                    proxyCred = new NTCredentials(
-                            proxyProperties.getUserName().trim(),
-                            proxyProperties.getPassWord().trim(), proxyHostName,
-                            proxyProperties
-                                    .getDomain().trim()); // NTLM Authentication
-                }
-            }
-        }
-
-        client.getState().setProxyCredentials(AuthScope.ANY, proxyCred);
-        config.setProxy(proxyHostName, proxyPort);
-    }
-
-    /**
      * Collect the HTTP header information and set them in the message context
      *
      * @param method HttpMethodBase from which to get information
@@ -314,10 +209,7 @@
      */
     protected HostConfiguration getHostConfiguration(HttpClient client,
                                                      MessageContext msgCtx,
-                                                     URL targetURL)
-            throws AxisFault {
-        boolean isProxyListed = isProxyListed(msgCtx);    // list the proxy
-
+                                                     URL targetURL)throws AxisFault {
 
         boolean isAuthenticationEnabled = isAuthenticationEnabled(msgCtx);
         int port = targetURL.getPort();
@@ -340,7 +232,7 @@
                 (Protocol)msgCtx.getOptions().getProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER);
 
         // setting the real host configuration
-        // I assume the 90% case, or even 99% case will be no protocol handler case. 
+        // I assume the 90% case, or even 99% case will be no protocol handler case.
         if (protocolHandler == null) {
             config.setHost(targetURL.getHost(), port, targetURL.getProtocol());
         } else {
@@ -348,12 +240,15 @@
         }
 
         if (isAuthenticationEnabled) {
-            // Basic, Digest, NTLM and custom authentications. 
+            // Basic, Digest, NTLM and custom authentications.
             this.setAuthenticationInfo(client, msgCtx, config);
         }
         // proxy configuration
-        if (isProxyListed) {
-            this.configProxyAuthentication(client, proxyOutSetting, config, msgCtx);
+
+        if (ProxyConfiguration.isProxyEnabled(msgCtx,targetURL)) {
+            log.debug("ProxyConfiguration");
+            ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
+            proxyConfiguration.configure(msgCtx,client,config);
         }
 
         return config;
@@ -536,48 +431,6 @@
                 httpClient.getParams().setSoTimeout((int) timeout);
             }
         }
-    }
-
-    private boolean isProxyListed(MessageContext msgCtx) throws AxisFault {
-        boolean returnValue = false;
-        Parameter par = null;
-
-        proxyOutSetting = msgCtx.getConfigurationContext()
-                .getAxisConfiguration().getTransportOut(Constants.TRANSPORT_HTTP);
-
-        if (proxyOutSetting != null) {
-            par = proxyOutSetting.getParameter(HTTPConstants.PROXY);
-        }
-
-        OMElement hostElement = null;
-
-        if (par != null) {
-            hostElement = par.getParameterElement();
-        }
-
-        if (hostElement != null) {
-            Iterator ite = hostElement.getAllAttributes();
-
-            while (ite.hasNext()) {
-                OMAttribute attribute = (OMAttribute) ite.next();
-
-                if (attribute.getLocalName().equalsIgnoreCase(PROXY_HOST_NAME)) {
-                    returnValue = true;
-                }
-            }
-        }
-
-        HttpTransportProperties.ProxyProperties proxyProperties;
-
-        if ((proxyProperties =
-                (HttpTransportProperties.ProxyProperties) msgCtx.getProperty(
-                        HTTPConstants.PROXY)) != null) {
-            if (proxyProperties.getProxyHostName() != null) {
-                returnValue = true;
-            }
-        }
-
-        return returnValue;
     }
 
     public void setFormat(OMOutputFormat format) {

Modified: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HttpTransportProperties.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HttpTransportProperties.java?view=diff&rev=547565&r1=547564&r2=547565
==============================================================================
--- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HttpTransportProperties.java (original)
+++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HttpTransportProperties.java Fri Jun 15 00:33:39 2007
@@ -60,28 +60,20 @@
 
     public static class ProxyProperties {
         protected int proxyPort = -1;
-        protected String domain;
-        protected String passWord;
-        protected String proxyHostName;
-        protected String userName;
+        protected String domain = null;
+        protected String passWord = null;
+        protected String proxyHostName = null;
+        protected String userName = null;
 
         public ProxyProperties() {
         }
 
         public String getDomain() {
-            if (domain == null || domain.length() == 0) {
-                return "anonymous";
-            } else {
-                return domain;
-            }
+            return domain;
         }
 
         public String getPassWord() {
-            if (passWord == null || passWord.length() == 0) {
-                return "anonymous";
-            } else {
-                return passWord;
-            }
+            return passWord;
         }
 
         public String getProxyHostName() {
@@ -93,11 +85,7 @@
         }
 
         public String getUserName() {
-            if (userName == null || userName.length() == 0) {
-                return "anonymous";
-            } else {
-                return userName;
-            }
+            return userName;
         }
 
         public void setDomain(String domain) {

Added: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/ProxyConfiguration.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/ProxyConfiguration.java?view=auto&rev=547565
==============================================================================
--- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/ProxyConfiguration.java (added)
+++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/ProxyConfiguration.java Fri Jun 15 00:33:39 2007
@@ -0,0 +1,341 @@
+/*
+ * Copyright 2004,2005 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.
+ */
+package org.apache.axis2.transport.http;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.Parameter;
+import org.apache.commons.httpclient.*;
+import org.apache.commons.httpclient.auth.AuthScope;
+
+import javax.xml.namespace.QName;
+import java.net.URL;
+
+
+/**
+ * The purpose of this class is to configure the proxy auth regardles of the protocol.
+ * Proxy will be set only for HTTP connection
+ */
+
+public class ProxyConfiguration {
+
+    protected String proxyHost;
+    protected String nonProxyHosts;
+    protected int proxyPort = -1; //If port is not set, default is set to -1
+    protected String proxyUser;
+    protected String proxyPassword;
+
+    protected static final String HTTP_PROXY_HOST = "http.proxyHost";
+    protected static final String HTTP_PROXY_PORT = "http.proxyPort";
+    protected static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";
+
+    protected static final String ATTR_PROXY = "Proxy";
+    protected static final String PROXY_HOST_ELEMENT = "ProxyHost";
+    protected static final String PROXY_PORT_ELEMENT = "ProxyPort";
+    protected static final String PROXY_USER_ELEMENT = "ProxyUser";
+    protected static final String PROXY_PASSWORD_ELEMENT = "ProxyPassword";
+
+    public void configure(MessageContext messageContext,
+                          HttpClient httpClient,
+                          HostConfiguration config) throws AxisFault {
+
+        //        <parameter name="Proxy">
+        //              <Configuration>
+        //                     <ProxyHost>example.org</ProxyHost>
+        //                     <ProxyPort>5678</ProxyPort>
+        //                     <ProxyUser>EXAMPLE\saminda</ProxyUser>
+        //                     <ProxyPassword>ppp</ProxyPassword>
+        //              </Configuration>
+        //        </parameter>
+        Credentials proxyCred = null;
+
+        //Getting configuration values from Axis2.xml
+        Parameter param = messageContext.getConfigurationContext().getAxisConfiguration()
+                .getParameter(ATTR_PROXY);
+
+        if (param != null) {
+            OMElement configurationEle = param.getParameterElement().getFirstElement();
+            if (configurationEle == null) {
+                throw new AxisFault(
+                        ProxyConfiguration.class.getName() + " Configuration element is missing");
+            }
+
+            OMElement proxyHostEle =
+                    configurationEle.getFirstChildWithName(new QName(PROXY_HOST_ELEMENT));
+            OMElement proxyPortEle =
+                    configurationEle.getFirstChildWithName(new QName(PROXY_PORT_ELEMENT));
+            OMElement proxyUserEle =
+                    configurationEle.getFirstChildWithName(new QName(PROXY_USER_ELEMENT));
+            OMElement proxyPasswordEle =
+                    configurationEle.getFirstChildWithName(new QName(PROXY_PASSWORD_ELEMENT));
+
+            if (proxyHostEle == null) {
+                throw new AxisFault(
+                        ProxyConfiguration.class.getName() + " ProxyHost element is missing");
+            }
+            String text = proxyHostEle.getText();
+            if (text == null) {
+                throw new AxisFault(
+                        ProxyConfiguration.class.getName() + " ProxyHost's value is missing");
+            }
+
+            this.setProxyHost(text);
+
+            if (proxyPortEle != null) {
+                this.setProxyPort(Integer.parseInt(proxyPortEle.getText()));
+            }
+
+            if (proxyUserEle != null) {
+                this.setProxyUser(proxyUserEle.getText());
+            }
+
+            if (proxyPasswordEle != null) {
+                this.setProxyPassword(proxyPasswordEle.getText());
+            }
+
+            if (this.getProxyUser() == null && this.getProxyUser() == null) {
+                proxyCred = new UsernamePasswordCredentials("", "");
+            } else {
+                proxyCred =
+                        new UsernamePasswordCredentials(this.getProxyUser(),
+                                                        this.getProxyPassword());
+            }
+
+            // if the username is in the form "DOMAIN\\user"
+            // then use NTCredentials instead.
+            if (this.getProxyUser() != null) {
+                int domainIndex = this.getProxyUser().indexOf("\\");
+                if (domainIndex > 0) {
+                    String domain = this.getProxyUser().substring(0, domainIndex);
+                    if (this.getProxyUser().length() > domainIndex + 1) {
+                        String user = this.getProxyUser().substring(domainIndex + 1);
+                        proxyCred = new NTCredentials(user,
+                                                      this.getProxyPassword(),
+                                                      this.getProxyHost(),
+                                                      domain);
+                    }
+                }
+            }
+        }
+
+        // Overide the property setting in runtime.
+        HttpTransportProperties.ProxyProperties proxyProperties =
+                (HttpTransportProperties.ProxyProperties) messageContext
+                        .getProperty(HTTPConstants.PROXY);
+
+        if (proxyProperties != null) {
+            String host = proxyProperties.getProxyHostName();
+            if (host == null || host.length() == 0) {
+                throw new AxisFault(ProxyConfiguration.class.getName() +
+                                    " Proxy host is not available. Host is a MUST parameter");
+
+            } else {
+                this.setProxyHost(host);
+            }
+
+
+            this.setProxyPort(proxyProperties.getProxyPort());
+
+            //Setting credentials
+
+            String userName = proxyProperties.getUserName();
+            String password = proxyProperties.getPassWord();
+            String domain = proxyProperties.getDomain();
+
+            if (userName == null && password == null) {
+                proxyCred = new UsernamePasswordCredentials("", "");
+            } else {
+                proxyCred = new UsernamePasswordCredentials(userName, password);
+            }
+
+            if (userName != null && password != null && domain != null) {
+                proxyCred = new NTCredentials(userName, password, host, domain);
+            }
+
+        }
+
+        //Using Java Networking Properties
+
+        String host = System.getProperty(HTTP_PROXY_HOST);
+        if (host != null) {
+            this.setProxyHost(host);
+            proxyCred = new UsernamePasswordCredentials("","");
+        }
+
+        String port = System.getProperty(HTTP_PROXY_PORT);
+
+        if (port != null) {
+            this.setProxyPort(Integer.parseInt(port));
+        }
+
+        if (proxyCred == null) {
+            throw new AxisFault(ProxyConfiguration.class.getName() +
+                                    " Minimum proxy credentials are not set");
+        }
+        httpClient.getState().setProxyCredentials(AuthScope.ANY, proxyCred);
+        config.setProxy(this.getProxyHost(), this.getProxyPort());
+    }
+
+    /**
+     * Check first if the proxy is configured or active.
+     * If yes this will return true. This is not a deep check
+     *
+     * @param messageContext
+     * @return boolean
+     */
+
+    public static boolean isProxyEnabled(MessageContext messageContext, URL targetURL)
+            throws AxisFault {
+
+        boolean state = false;
+
+
+        Parameter param = messageContext.getConfigurationContext().getAxisConfiguration()
+                .getParameter(ATTR_PROXY);
+
+        //If configuration is over ridden
+        Object obj = messageContext.getProperty(HTTPConstants.PROXY);
+
+        //From Java Networking Properties
+        String sp = System.getProperty(HTTP_PROXY_HOST);
+
+        if (param != null || obj != null || sp != null) {
+            state = true;
+        }
+
+        boolean isNonProxyHost = validateNonProxyHosts(targetURL.getHost());
+
+        return state && !isNonProxyHost;
+
+    }
+
+    /**
+     * Validates for names that shouldn't be listered as proxies.
+     * The http.nonProxyHosts can be set to specify the hosts which should be
+     * connected to directly (not through the proxy server).
+     * The value of the http.nonProxyHosts property can be a list of hosts,
+     * each separated by a |; it can also take a regular expression for matches;
+     * for example: *.sfbay.sun.com would match any fully qualified hostname in the sfbay domain.
+     *
+     * For more information refer to : http://java.sun.com/features/2002/11/hilevel_network.html
+     *
+     * false : validation fail : User can use the proxy
+     * true : validation pass ; User can't use the proxy
+     *
+     * @return boolean
+     */
+    private static boolean validateNonProxyHosts(String targetHost) {
+
+        //From system property http.nonProxyHosts
+        String nonProxyHosts = System.getProperty(HTTP_NON_PROXY_HOSTS);
+
+        if (nonProxyHosts == null) {
+            return false;
+        }
+
+        String[] nonProxyHostsArray = nonProxyHosts.split("\\|");
+
+        if (nonProxyHostsArray.length == 1) {
+            return targetHost.matches(nonProxyHosts);
+        } else {
+            boolean pass = false;
+            for (int i = 0; i < nonProxyHostsArray.length; i++) {
+                String a = nonProxyHostsArray[i];
+                if (targetHost.matches(a)) {
+                    pass = true;
+                    break;
+                }
+            }
+            return pass;
+        }
+    }
+
+    /**
+     * Retrun proxy host
+     *
+     * @return String
+     */
+    public String getProxyHost() {
+        return proxyHost;
+    }
+
+    /**
+     * set proxy host
+     *
+     * @param proxyHost
+     */
+
+    public void setProxyHost(String proxyHost) {
+        this.proxyHost = proxyHost;
+    }
+
+    /**
+     * retrun proxy port
+     *
+     * @return String
+     */
+    public int getProxyPort() {
+        return proxyPort;
+    }
+
+    /**
+     * set proxy port
+     *
+     * @param proxyPort
+     */
+    public void setProxyPort(int proxyPort) {
+        this.proxyPort = proxyPort;
+    }
+
+    /**
+     * return proxy user. Proxy user can be user/domain or user
+     *
+     * @return String
+     */
+    public String getProxyUser() {
+        return proxyUser;
+    }
+
+    /**
+     * get proxy user
+     *
+     * @param proxyUser
+     */
+    public void setProxyUser(String proxyUser) {
+        this.proxyUser = proxyUser;
+    }
+
+    /**
+     * set password
+     *
+     * @return String
+     */
+    public String getProxyPassword() {
+        return proxyPassword;
+    }
+
+    /**
+     * get password
+     *
+     * @param proxyPassword
+     */
+    public void setProxyPassword(String proxyPassword) {
+        this.proxyPassword = proxyPassword;
+    }
+
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org