You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by cs...@apache.org on 2010/11/19 23:30:53 UTC

svn commit: r1037063 - in /cxf/trunk/rt/transports/http/src: main/java/org/apache/cxf/transport/http/ main/java/org/apache/cxf/transport/https/ test/java/org/apache/cxf/transport/http/

Author: cschneider
Date: Fri Nov 19 22:30:53 2010
New Revision: 1037063

URL: http://svn.apache.org/viewvc?rev=1037063&view=rev
Log:
CXF-3137 Simplify http connection factory in http transport

Removed:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpURLConnectionFactory.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpURLConnectionFactoryImpl.java
Modified:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPTransportFactory.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionFactory.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionInfo.java
    cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPTransportFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPTransportFactory.java?rev=1037063&r1=1037062&r2=1037063&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPTransportFactory.java (original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPTransportFactory.java Fri Nov 19 22:30:53 2010
@@ -31,13 +31,11 @@ import javax.wsdl.extensions.soap.SOAPAd
 import javax.xml.namespace.QName;
 
 import org.apache.cxf.configuration.Configurer;
-import org.apache.cxf.configuration.jsse.TLSClientParameters;
 import org.apache.cxf.service.Service;
 import org.apache.cxf.service.model.BindingInfo;
 import org.apache.cxf.service.model.EndpointInfo;
 import org.apache.cxf.service.model.ServiceInfo;
 import org.apache.cxf.transport.AbstractTransportFactory;
-import org.apache.cxf.transport.https.HttpsURLConnectionFactory;
 import org.apache.cxf.wsdl.http.AddressType;
 import org.apache.cxf.wsdl11.WSDLEndpointFactory;
 
@@ -132,43 +130,6 @@ public abstract class AbstractHTTPTransp
             }
         }
     }
-
-    /**
-     * This static call creates a connection factory based on
-     * the existence of the SSL (TLS) client side configuration. 
-     */
-    static HttpURLConnectionFactory getConnectionFactory(HTTPConduit configuredConduit) {
-        return getConnectionFactory(configuredConduit, null);
-    }
-    
-    static HttpURLConnectionFactory getConnectionFactory(
-        HTTPConduit configuredConduit,
-        String address
-    ) {
-        HttpURLConnectionFactory fac = null;
-        boolean useHttps = false;
-
-        if (address == null) {
-            address = configuredConduit.getAddress();
-        }
-        if (address != null 
-            && address.startsWith(HttpsURLConnectionFactory.HTTPS_URL_PROTOCOL_ID + ":/")) {
-            useHttps = true;
-        }
-        if (address == null) {
-            useHttps = configuredConduit.getTlsClientParameters() != null;
-        }
-        if (useHttps) {
-            TLSClientParameters params = configuredConduit.getTlsClientParameters();
-            if (params == null) {
-                params = new TLSClientParameters(); //use defaults
-            }
-            fac = new HttpsURLConnectionFactory(params);
-        } else {
-            fac = new HttpURLConnectionFactoryImpl();
-        }
-        return fac;
-    }   
     
     private static class HttpEndpointInfo extends EndpointInfo {
         AddressType saddress;

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java?rev=1037063&r1=1037062&r2=1037063&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java (original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java Fri Nov 19 22:30:53 2010
@@ -28,6 +28,7 @@ import java.io.PushbackInputStream;
 import java.net.HttpRetryException;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
+import java.net.Proxy;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.ArrayList;
@@ -74,6 +75,8 @@ import org.apache.cxf.transport.http.pol
 import org.apache.cxf.transport.https.CertConstraints;
 import org.apache.cxf.transport.https.CertConstraintsInterceptor;
 import org.apache.cxf.transport.https.CertConstraintsJaxBUtils;
+import org.apache.cxf.transport.https.HttpsURLConnectionFactory;
+import org.apache.cxf.transport.https.HttpsURLConnectionInfo;
 import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
 import org.apache.cxf.version.Version;
 import org.apache.cxf.workqueue.WorkQueueManager;
@@ -181,7 +184,7 @@ public class HTTPConduit 
      * that an extended class may alter its value with an EasyMock URLConnection
      * Factory. 
      */
-    protected HttpURLConnectionFactory connectionFactory;
+    protected HttpsURLConnectionFactory connectionFactory;
     
     /**
      *  This field holds a reference to the CXF bus associated this conduit.
@@ -247,14 +250,6 @@ public class HTTPConduit 
     private HttpAuthSupplier authSupplier;
 
     /**
-     * This boolean signfies that that finalizeConfig is called, which is
-     * after the HTTPTransportFactory configures this object via spring.
-     * At this point, any change by a "setter" is dynamic, and any change
-     * should be handled as such.
-     */
-    private boolean configFinalized;
-
-    /**
      * Variables for holding session state if sessions are supposed to be maintained
      */
     private Map<String, Cookie> sessionCookies = new ConcurrentHashMap<String, Cookie>();
@@ -295,7 +290,15 @@ public class HTTPConduit 
             fromEndpointReferenceType = true;
         }
         proxyFactory = new ProxyFactory();
-        initializeConfig();
+        connectionFactory = new HttpsURLConnectionFactory();
+        
+        // wsdl extensors are superseded by policies which in        
+        // turn are superseded by injection                          
+        PolicyEngine pe = bus.getExtension(PolicyEngine.class);      
+        if (null != pe && pe.isEnabled() && endpointInfo.getService() != null) {                          
+            clientSidePolicy =                                       
+                PolicyUtils.getClient(pe, endpointInfo, this);              
+        }
         CXFAuthenticator.addAuthenticator();
     }
 
@@ -315,24 +318,6 @@ public class HTTPConduit 
         return endpointInfo.getName() + SC_HTTP_CONDUIT_SUFFIX;
     }
 
-    /**
-     * This method is called from the constructor which initializes
-     * the configuration. The TransportFactory will call configureBean
-     * on this object after construction.
-     */    
-    private void initializeConfig() {
-    
-        // wsdl extensors are superseded by policies which in        
-        // turn are superseded by injection                          
-
-        PolicyEngine pe = bus.getExtension(PolicyEngine.class);      
-        if (null != pe && pe.isEnabled() && endpointInfo.getService() != null) {                          
-            clientSidePolicy =                                       
-                PolicyUtils.getClient(pe, endpointInfo, this);              
-        }                                                            
-
-    }
-    
     private static void configureConduitFromEndpointInfo(HTTPConduit conduit,
             EndpointInfo endpointInfo) {
         if (conduit.getClient() == null) {
@@ -419,14 +404,6 @@ public class HTTPConduit 
         
         configureConduitFromEndpointInfo(this, endpointInfo);
         logConfig();
-
-        // Get the correct URLConnection factory based on the 
-        // configuration.
-        connectionFactory = retrieveConnectionFactory(getAddress());
-
-        // We have finalized the configuration. Any configurable entity
-        // set now, must make changes dynamically.
-        configFinalized = true;
         
         if (getClient().getDecoupledEndpoint() != null) {
             this.endpointInfo.setProperty("org.apache.cxf.ws.addressing.replyto",
@@ -445,29 +422,12 @@ public class HTTPConduit 
         return sessionCookies;
     }
     
-    /**
-     * This method sets the connectionFactory field for this object. It is called
-     * after an SSL Client Policy is set or an HttpsHostnameVerifier
-     * because we need to reinitialize the connection factory.
-     * <p>
-     * This method is "protected" so that this class may be extended and override
-     * this method to put an EasyMock URL Connection factory for some contrived 
-     * UnitTest that will of course break, should the calls to the URL Connection
-     * Factory get altered.
-     */
-    protected synchronized HttpURLConnectionFactory retrieveConnectionFactory(String url) {
-        return AbstractHTTPTransportFactory.getConnectionFactory(this, url);
+    private HttpURLConnection createConnection(Message message, URL url) throws IOException {
+        HTTPClientPolicy csPolicy = getClient(message);
+        Proxy proxy = proxyFactory.createProxy(csPolicy , url);
+        return connectionFactory.createConnection(tlsClientParameters, proxy, url);
     }
-    
-    
-    protected synchronized HttpURLConnectionFactory getConnectionFactory(URL url) {
-        if (connectionFactory == null 
-            || !url.getProtocol().equals(connectionFactory.getProtocol())) {
-            connectionFactory = retrieveConnectionFactory(url.toString());
-        }
 
-        return connectionFactory;
-    }
     /**
      * Prepare to send an outbound HTTP message over this http conduit to a 
      * particular endpoint.
@@ -501,10 +461,7 @@ public class HTTPConduit 
         boolean needToCacheRequest = false;
         
         HTTPClientPolicy csPolicy = getClient(message);
-
-        HttpURLConnectionFactory f = getConnectionFactory(currentURL);
-        HttpURLConnection connection = f.createConnection(proxyFactory.createProxy(csPolicy, currentURL), 
-                currentURL);
+        HttpURLConnection connection = createConnection(message, currentURL);
         connection.setDoOutput(true);       
         
         long timeout = csPolicy.getConnectionTimeout();
@@ -691,8 +648,7 @@ public class HTTPConduit 
                 // The call is (said to be) ingored internally if
                 // already connected.
                 connection.connect();
-                URLConnectionInfo info = getConnectionFactory(connection.getURL())
-                    .getConnectionInfo(connection);
+                HttpsURLConnectionInfo info = new HttpsURLConnectionInfo(connection);
                 if (trustDecider != null) {
                     trustDecider.establishTrust(
                         getConduitName(), 
@@ -1316,11 +1272,6 @@ public class HTTPConduit 
                     + "' has been (re)configured for plain http.");
             }
         }
-        // If this is called after the HTTPTransportFactory called 
-        // finalizeConfig, we need to update the connection factory.
-        if (configFinalized) {
-            connectionFactory = retrieveConnectionFactory(getAddress());
-        }
     }
 
     /**
@@ -1603,8 +1554,7 @@ public class HTTPConduit 
         connection.disconnect();
         
         HTTPClientPolicy cp = getClient(message);
-        connection = getConnectionFactory(newURL).createConnection(proxyFactory.createProxy(cp, newURL), 
-                newURL);
+        connection = createConnection(message, newURL);
         connection.setDoOutput(true);        
         // TODO: using Message context to deceided HTTP send properties
         connection.setConnectTimeout((int)cp.getConnectionTimeout());

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionFactory.java?rev=1037063&r1=1037062&r2=1037063&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionFactory.java (original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionFactory.java Fri Nov 19 22:30:53 2010
@@ -38,9 +38,6 @@ import javax.net.ssl.SSLSocketFactory;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.ReflectionInvokationHandler;
 import org.apache.cxf.configuration.jsse.TLSClientParameters;
-import org.apache.cxf.transport.http.HTTPConduit;
-import org.apache.cxf.transport.http.HttpURLConnectionFactory;
-import org.apache.cxf.transport.http.HttpURLConnectionInfo;
 
 /**
  * This HttpsURLConnectionFactory implements the HttpURLConnectionFactory
@@ -48,8 +45,7 @@ import org.apache.cxf.transport.http.Htt
  * URLs.
  * 
  */
-public final class HttpsURLConnectionFactory 
-    implements HttpURLConnectionFactory {
+public class HttpsURLConnectionFactory {
     
     /**
      * This constant holds the URL Protocol Identifier for HTTPS
@@ -63,19 +59,6 @@ public final class HttpsURLConnectionFac
     private static boolean weblogicWarned;
     
     /**
-     * This field holds the conduit to which this connection factory
-     * is a slave.
-     */
-    HTTPConduit conduit;
-    
-    /**
-     * This field contains the TLS configuration for the URLs created by
-     * this factory.
-     */
-    TLSClientParameters tlsClientParameters;
-    
-    
-    /**
      * Cache the last SSLContext to avoid recreation
      */
     SSLSocketFactory socketFactory;
@@ -83,13 +66,8 @@ public final class HttpsURLConnectionFac
     /**
      * This constructor initialized the factory with the configured TLS
      * Client Parameters for the HTTPConduit for which this factory is used.
-     * 
-     * @param params The TLS Client Parameters. This parameter is guaranteed 
-     *               to be non-null.
      */
-    public HttpsURLConnectionFactory(TLSClientParameters params) {
-        tlsClientParameters        = params;
-        assert tlsClientParameters != null;
+    public HttpsURLConnectionFactory() {
     }
     
     /**
@@ -105,24 +83,22 @@ public final class HttpsURLConnectionFac
      *         is thrown. 
      *                     
      */
-    public HttpURLConnection createConnection(Proxy proxy, URL url)
-        throws IOException {
-
-        if (!url.getProtocol().equals(HTTPS_URL_PROTOCOL_ID)) {
-            throw new IOException("Illegal Protocol " 
-                    + url.getProtocol() 
-                    + " for HTTPS URLConnection Factory.");
-        }
+    public HttpURLConnection createConnection(TLSClientParameters tlsClientParameters, 
+            Proxy proxy, URL url) throws IOException {
         
         HttpURLConnection connection =
             (HttpURLConnection) (proxy != null 
                                    ? url.openConnection(proxy)
                                    : url.openConnection());
-                                   
-        if (tlsClientParameters != null) {
+        if (HTTPS_URL_PROTOCOL_ID.equals(url.getProtocol())) {
+            
+            if (tlsClientParameters == null) {
+                tlsClientParameters = new TLSClientParameters();
+            }
+
             Exception ex = null;
             try {
-                decorateWithTLS(connection);
+                decorateWithTLS(tlsClientParameters, connection);
             } catch (Exception e) {
                 ex = e;
             } finally {
@@ -146,8 +122,8 @@ public final class HttpsURLConnectionFac
      * from the TLS Client Parameters. Connection parameter is of supertype HttpURLConnection, 
      * which allows internal cast to potentially divergent subtype (https) implementations.
      */
-    protected synchronized void decorateWithTLS(HttpURLConnection connection)
-        throws GeneralSecurityException {
+    protected synchronized void decorateWithTLS(TLSClientParameters tlsClientParameters, 
+            HttpURLConnection connection) throws GeneralSecurityException {
 
         // always reload socketFactory from HttpsURLConnection.defaultSSLSocketFactory and 
         // tlsClientParameters.sslSocketFactory to allow runtime configuration change
@@ -265,27 +241,6 @@ public final class HttpsURLConnectionFac
         LOG.addHandler(handler);
     }
 
-    /**
-     * This operation returns an HttpsURLConnectionInfo for the 
-     * given HttpsURLConnection. 
-     * 
-     * @param connection The HttpsURLConnection
-     * @return The HttpsURLConnectionInfo object for the given 
-     *         HttpsURLConnection.
-     * @throws IOException Normal IO Exceptions.
-     * @throws ClassCastException If "connection" is not an HttpsURLConnection 
-     *         (or a supported subtype of HttpURLConnection)
-     */
-    public HttpURLConnectionInfo getConnectionInfo(
-            HttpURLConnection connection
-    ) throws IOException {  
-        return new HttpsURLConnectionInfo(connection);
-    }
-    
-    public String getProtocol() {
-        return "https";
-    }
-
 }
 
 

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionInfo.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionInfo.java?rev=1037063&r1=1037062&r2=1037063&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionInfo.java (original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https/HttpsURLConnectionInfo.java Fri Nov 19 22:30:53 2010
@@ -72,7 +72,7 @@ public class HttpsURLConnectionInfo exte
      * of supertype HttpURLConnection, which allows internal cast to 
      * potentially divergent subtype (Https) implementations.
      */
-    HttpsURLConnectionInfo(HttpURLConnection connection)
+    public HttpsURLConnectionInfo(HttpURLConnection connection)
         throws IOException {
         super(connection);
         if (connection instanceof HttpsURLConnection) {

Modified: cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java?rev=1037063&r1=1037062&r2=1037063&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java (original)
+++ cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java Fri Nov 19 22:30:53 2010
@@ -34,6 +34,7 @@ import java.util.Map;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.bus.CXFBusImpl;
+import org.apache.cxf.configuration.jsse.TLSClientParameters;
 import org.apache.cxf.configuration.security.AuthorizationPolicy;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.io.AbstractThresholdOutputStream;
@@ -42,6 +43,7 @@ import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.service.model.EndpointInfo;
 import org.apache.cxf.transport.MessageObserver;
+import org.apache.cxf.transport.https.HttpsURLConnectionFactory;
 import org.apache.cxf.ws.addressing.EndpointReferenceType;
 import org.easymock.classextension.EasyMock;
 import org.easymock.classextension.IMocksControl;
@@ -62,7 +64,7 @@ public class HTTPConduitURLEasyMockTest 
     private static final String PAYLOAD = "message payload";
     private IMocksControl control;
     private EndpointInfo endpointInfo;
-    private HttpURLConnectionFactory connectionFactory;
+    private HttpsURLConnectionFactory connectionFactory;
     private HttpURLConnection connection;
     private Proxy proxy;
     private Message inMessage;
@@ -80,18 +82,12 @@ public class HTTPConduitURLEasyMockTest 
             Bus                      associatedBus, 
             EndpointInfo             endpoint, 
             EndpointReferenceType    epr,
-            HttpURLConnectionFactory testFactory
+            HttpsURLConnectionFactory testFactory
         ) throws IOException {
             super(associatedBus, endpoint, epr);
             connectionFactory = testFactory;
         }
-        @Override
-        protected HttpURLConnectionFactory retrieveConnectionFactory(String s) {
-            // do nothing. i.e do not change the connectionFactory field.
-            return connectionFactory;
-        }
-        
-        
+
     }
     /**
      * @throws java.lang.Exception
@@ -251,14 +247,14 @@ public class HTTPConduitURLEasyMockTest 
         endpointInfo = new EndpointInfo();
         endpointInfo.setAddress(NOWHERE + "bar/foo");
         connectionFactory = 
-            control.createMock(HttpURLConnectionFactory.class);
+            control.createMock(HttpsURLConnectionFactory.class);
         
         if (send) {
             //proxy = control.createMock(Proxy.class);
             proxy =  null;
             connection =
                 control.createMock(HttpURLConnection.class);
-            connectionFactory.createConnection(
+            connectionFactory.createConnection((TLSClientParameters)EasyMock.isNull(),
                                       EasyMock.eq(proxy), 
                                       EasyMock.eq(new URL(NOWHERE + "bar/foo")));
             EasyMock.expectLastCall().andReturn(connection);