You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2009/10/05 21:01:05 UTC

svn commit: r821963 - in /cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/https: HttpsURLConnectionFactory.java HttpsURLConnectionInfo.java

Author: dkulp
Date: Mon Oct  5 19:01:04 2009
New Revision: 821963

URL: http://svn.apache.org/viewvc?rev=821963&view=rev
Log:
[CXF-1459] use reflection on the actual connection's class to get the
methods so it should work with the BEA versions as well.

Modified:
    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

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=821963&r1=821962&r2=821963&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 Mon Oct  5 19:01:04 2009
@@ -90,10 +90,6 @@
      * Cache the last SSLContext to avoid recreation
      */
     SSLSocketFactory socketFactory;
-
-    private Class deprecatedSunHttpsURLConnectionClass;
-
-    private Class deprecatedSunHostnameVerifierClass;
     
     /**
      * This constructor initialized the factory with the configured TLS
@@ -232,11 +228,11 @@
             }
             conn.setSSLSocketFactory(socketFactory);
         } else {
-            // handle the deprecated sun case
+            // handle the deprecated sun case and other possible hidden API's 
+            // that are similar to the Sun cases
             try {
-                Class<?> connectionClass = getDeprecatedSunHttpsURLConnectionClass();
-                Class<?> verifierClass = getDeprecatedSunHostnameVerifierClass();
-                Method setHostnameVerifier = connectionClass.getMethod("setHostnameVerifier", verifierClass);
+                Method method = connection.getClass().getMethod("getHostnameVerifier");
+                
                 InvocationHandler handler = new InvocationHandler() {
                     public Object invoke(Object proxy, 
                                          Method method, 
@@ -245,33 +241,27 @@
                     }
                 };
                 Object proxy = java.lang.reflect.Proxy.newProxyInstance(this.getClass().getClassLoader(),
-                                                                          new Class[] {verifierClass},
-                                                                          handler);
-                setHostnameVerifier.invoke(connectionClass.cast(connection), verifierClass.cast(proxy));
-                Method setSSLSocketFactory = connectionClass.getMethod("setSSLSocketFactory", 
-                                                                       SSLSocketFactory.class);
-                setSSLSocketFactory.invoke(connectionClass.cast(connection), socketFactory);
+                                                                        new Class[] {method.getReturnType()},
+                                                                        handler);
+
+                method = connection.getClass().getMethod("setHostnameVerifier", method.getReturnType());
+                method.invoke(connection, proxy);
+            } catch (Exception ex) {
+                //Ignore this one, we're just setting it to a completely stupid verifier anyway
+                //that is pretty pointless.
+            }
+            try {
+                Method setSSLSocketFactory = connection.getClass().getMethod("setSSLSocketFactory", 
+                                                                             SSLSocketFactory.class);
+                setSSLSocketFactory.invoke(connection, socketFactory);
             } catch (Exception ex) {
+                //if we cannot set the SSLSocketFactor, we're in serious trouble.
                 throw new IllegalArgumentException("Error decorating connection class " 
                         + connection.getClass().getName(), ex);
             }
         }
     }
 
-    private Class getDeprecatedSunHttpsURLConnectionClass() throws ClassNotFoundException {
-        if (deprecatedSunHttpsURLConnectionClass == null) {
-            deprecatedSunHttpsURLConnectionClass = Class.forName("com.sun.net.ssl.HttpsURLConnection");
-        }
-        return deprecatedSunHttpsURLConnectionClass;
-    }
-
-    private Class getDeprecatedSunHostnameVerifierClass() throws ClassNotFoundException {
-        if (deprecatedSunHostnameVerifierClass == null) {
-            deprecatedSunHostnameVerifierClass = Class.forName("com.sun.net.ssl.HostnameVerifier");
-        }
-        return deprecatedSunHostnameVerifierClass;
-    }
-
     /*
      *  For development and testing only
      */

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=821963&r1=821962&r2=821963&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 Mon Oct  5 19:01:04 2009
@@ -25,7 +25,6 @@
 import java.security.Principal;
 import java.security.cert.Certificate;
 
-import javax.imageio.IIOException;
 import javax.net.ssl.HttpsURLConnection;
 
 import org.apache.cxf.transport.http.HttpURLConnectionInfo;
@@ -66,7 +65,6 @@
      */
     protected Principal peerPrincipal;
 
-    private Class deprecatedSunHttpsURLConnectionOldImplClass;
     
     /**
      * This constructor is used to create the info object
@@ -87,13 +85,12 @@
         } else {
             Exception ex = null;
             try {
-                Class<?> deprecatedSunClass = getDeprecatedSunHttpsURLConnectionOldImplClass();
                 Method method = null;
-                method = deprecatedSunClass.getMethod("getCipherSuite", (Class[]) null);
+                method = connection.getClass().getMethod("getCipherSuite", (Class[]) null);
                 enabledCipherSuite = (String) method.invoke(connection, (Object[]) null);
-                method = deprecatedSunClass.getMethod("getLocalCertificates", (Class[]) null);
+                method = connection.getClass().getMethod("getLocalCertificates", (Class[]) null);
                 localCertificates = (Certificate[]) method.invoke(connection, (Object[]) null);
-                method = deprecatedSunClass.getMethod("getServerCertificates", (Class[]) null);
+                method = connection.getClass().getMethod("getServerCertificates", (Class[]) null);
                 serverCertificates = (Certificate[]) method.invoke(connection, (Object[]) null);
                 
                 //TODO Obtain localPrincipal and peerPrincipal using the com.sun.net.ssl api
@@ -104,21 +101,17 @@
                     if (ex instanceof IOException) {
                         throw (IOException) ex;
                     }
-                    throw new IIOException("Error constructing HttpsURLConnectionInfo for connection class "
-                            + connection.getClass().getName(), ex);
+                    IOException ioe = new IOException("Error constructing HttpsURLConnectionInfo "
+                                                      + "for connection class "
+                                                      + connection.getClass().getName());
+                    ioe.initCause(ex);
+                    throw ioe;
+                    
                 }
             }
         }
     }
 
-    private Class getDeprecatedSunHttpsURLConnectionOldImplClass() throws ClassNotFoundException {
-        if (deprecatedSunHttpsURLConnectionOldImplClass == null) {
-            deprecatedSunHttpsURLConnectionOldImplClass = 
-                    Class.forName("com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl");
-        }
-        return deprecatedSunHttpsURLConnectionOldImplClass;
-    }
-        
     /**
      * This method returns the cipher suite employed in this
      * HttpsURLConnection.