[jboss-svn-commits] JBL Code SVN: r24910 - in labs/jbossesb/trunk/product: rosetta/src/org/jboss/soa/esb/http/configurators and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jan 26 05:10:47 EST 2009


Author: beve
Date: 2009-01-26 05:10:47 -0500 (Mon, 26 Jan 2009)
New Revision: 24910

Added:
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/http/
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/http/configurators/
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/http/configurators/HttpProtocolUnitTest.java
Modified:
   labs/jbossesb/trunk/product/docs/ProgrammersGuide.odt
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/http/configurators/HttpProtocol.java
Log:
Work for https://jira.jboss.org/jira/browse/JBESB-2303 "Add support for http.proxyHost configuration"


Modified: labs/jbossesb/trunk/product/docs/ProgrammersGuide.odt
===================================================================
(Binary files differ)

Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/http/configurators/HttpProtocol.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/http/configurators/HttpProtocol.java	2009-01-26 03:21:05 UTC (rev 24909)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/http/configurators/HttpProtocol.java	2009-01-26 10:10:47 UTC (rev 24910)
@@ -20,12 +20,14 @@
 package org.jboss.soa.esb.http.configurators;
 
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.ProxyHost;
 import org.apache.commons.httpclient.contrib.ssl.StrictSSLProtocolSocketFactory;
 import org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory;
 import org.apache.commons.httpclient.protocol.Protocol;
 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
 import org.apache.commons.ssl.KeyMaterial;
 import org.apache.commons.ssl.SSLClient;
+import org.apache.log4j.Logger;
 import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.http.Configurator;
 import org.jboss.soa.esb.http.protocol.ProtocolSocketFactoryBuilder;
@@ -68,6 +70,8 @@
  * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
  */
 public class HttpProtocol extends Configurator {
+    
+    private Logger log = Logger.getLogger(HttpProtocol.class);
 
     public void configure(HttpClient httpClient, Properties properties) throws ConfigurationException {
         URI targetURI = getTargetURI(properties, true);
@@ -86,21 +90,68 @@
         if(scheme.equals("https")) {
             factory = properties.getProperty("protocol-socket-factory", StrictSSLProtocolSocketFactory.class.getName());
             keyMaterial = getKeyMaterial(properties);
+            setHttpsProxyHost(httpClient, properties);
 
             if(port == -1) {
                 port = 443;
             }
         } else {
             factory = properties.getProperty("protocol-socket-factory", DefaultProtocolSocketFactory.class.getName());
+            setHttpProxyHost(httpClient, properties);
         }
         assertPropertySetAndNotBlank(factory, "protocol-socket-factory");
         socketFactory = createFactoryClass(factory, keyMaterial, properties);
+        
 
         // And finally... configure the host with the protocol....
         protocol = new Protocol(scheme, socketFactory, port);
         Protocol.registerProtocol(scheme, protocol);
     }
-
+    
+    /**
+     * Will create a ProxyHost using the properties 'http.proxyHost' and optionally
+     * the property 'http.proxyPort'.
+     * 
+     * @param httpClient    The HttpClient that should have its host configurations proxy host set.
+     * @param properties    The properties that should be set.
+     */
+    void setHttpProxyHost(final HttpClient httpClient, final Properties properties) {
+        ProxyHost proxyHost = createProxyHost("http.proxyHost", "http.proxyPort", 80, properties);
+        if (proxyHost != null) {
+            httpClient.getHostConfiguration().setProxyHost(proxyHost);
+        }
+    }
+    
+    /**
+     * Will create a ProxyHost using the properties 'https.proxyHost' and optionally
+     * the property 'https.proxyPort'.
+     * 
+     * @param httpClient    The HttpClient that should have its host configurations proxy host set.
+     * @param properties    The properties that should be set.
+     */
+    void setHttpsProxyHost(final HttpClient httpClient, final Properties properties) {
+        ProxyHost proxyHost = createProxyHost("https.proxyHost", "https.proxyPort", 443, properties);
+        if (proxyHost != null) {
+            httpClient.getHostConfiguration().setProxyHost(proxyHost);
+        }
+    }
+    
+    private ProxyHost createProxyHost(final String hostPropertyName, final String portPropertyName, final int defaultPort, final Properties properties) {
+        final String proxyHost = (String) properties.get(hostPropertyName);
+        ProxyHost proxy = null;
+        if (proxyHost != null) {
+            final String proxyPortStr = (String) properties.get(portPropertyName);
+            if (proxyPortStr == null) { 
+                proxy = new ProxyHost(proxyHost, defaultPort);
+            }
+            else {
+                proxy = new ProxyHost(proxyHost, Integer.parseInt(proxyPortStr));
+            }
+            log.debug("ProxyHost " + proxy.toString());
+        }
+        return proxy;
+    }
+    
     private KeyMaterial getKeyMaterial(Properties properties) throws ConfigurationException {
         String keyStore = properties.getProperty("keystore", "/keystore");
         String keyStorePassword = properties.getProperty("keystore-passw", "changeit");

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/http/configurators/HttpProtocolUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/http/configurators/HttpProtocolUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/http/configurators/HttpProtocolUnitTest.java	2009-01-26 10:10:47 UTC (rev 24910)
@@ -0,0 +1,119 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.http.configurators;
+
+import static org.junit.Assert.*;
+
+import java.util.Properties;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link HttpProtocol}.
+ * <p/>
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ */
+public class HttpProtocolUnitTest
+{
+    private HttpClient httpClient = new HttpClient();
+    private HttpProtocol httpProtocol = new HttpProtocol();
+    
+    @Test
+    public void setHttpsProxyHostWithPort()
+    {
+        final String proxyHost = "192.87.22.1";
+        final int proxyPort = 8899;
+        final Properties properties = createHttpsProperties(proxyHost, proxyPort);
+                
+        httpProtocol.setHttpsProxyHost(httpClient, properties);
+        
+        assertEquals(proxyHost, httpClient.getHostConfiguration().getProxyHost());
+        assertEquals(proxyPort, httpClient.getHostConfiguration().getProxyPort());
+    }
+    
+    @Test
+    public void setHttpsProxyHostDefaultPort()
+    {
+        final String proxyHost = "192.87.22.1";
+        final Properties properties = createHttpsProperties(proxyHost, -1);
+                
+        httpProtocol.setHttpsProxyHost(httpClient, properties);
+        
+        assertEquals(proxyHost, httpClient.getHostConfiguration().getProxyHost());
+        assertEquals(443, httpClient.getHostConfiguration().getProxyPort());
+    }
+    
+    @Test
+    public void setHttpProxyHostWithPort()
+    {
+        final String proxyHost = "192.87.22.1";
+        final int proxyPort = 8899;
+        final Properties properties = createHttpProperties(proxyHost, proxyPort);
+                
+        httpProtocol.setHttpProxyHost(httpClient, properties);
+        
+        assertEquals(proxyHost, httpClient.getHostConfiguration().getProxyHost());
+        assertEquals(proxyPort, httpClient.getHostConfiguration().getProxyPort());
+    }
+    
+    @Test
+    public void setHttpProxyHostDefaultPort()
+    {
+        final String proxyHost = "192.87.22.8";
+        final Properties properties = createHttpProperties(proxyHost, -1);
+        
+        httpProtocol.setHttpProxyHost(httpClient, properties);
+        
+        assertEquals(proxyHost, httpClient.getHostConfiguration().getProxyHost());
+        assertEquals(80, httpClient.getHostConfiguration().getProxyPort());
+    }
+    
+    private Properties createHttpProperties(final String host, final int port)
+    {
+        final Properties properties = new Properties();
+        properties.setProperty("http.proxyHost", host);
+        if (port != -1)
+        {
+            properties.setProperty("http.proxyPort", String.valueOf(port));
+        }
+        return properties;
+    }
+    
+    private Properties createHttpsProperties(final String host, final int port)
+    {
+        final Properties properties = new Properties();
+        properties.setProperty("https.proxyHost", host);
+        if (port != -1)
+        {
+            properties.setProperty("https.proxyPort", String.valueOf(port));
+        }
+        return properties;
+    }
+    
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(HttpProtocolUnitTest.class);
+    }
+}




More information about the jboss-svn-commits mailing list