[prev in list] [next in list] [prev in thread] [next in thread] 

List:       wink-commits
Subject:    svn commit: r1043176 -
From:       bluk () apache ! org
Date:       2010-12-07 19:42:07
Message-ID: 20101207194207.8BD2623889F7 () eris ! apache ! org
[Download RAW message or body]

Author: bluk
Date: Tue Dec  7 19:42:07 2010
New Revision: 1043176

URL: http://svn.apache.org/viewvc?rev=1043176&view=rev
Log:
Fix support for SSL Hostname Verifier Bypass

See [WINK-242]

Thanks to David Johnson for the patch.

Modified:
    incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java


Modified: incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java
                
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/main/java/org/a \
pache/wink/client/internal/handlers/HttpURLConnectionHandler.java?rev=1043176&r1=1043175&r2=1043176&view=diff
 ==============================================================================
--- incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java \
                (original)
+++ incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java \
Tue Dec  7 19:42:07 2010 @@ -59,6 +59,29 @@ public class HttpURLConnectionHandler ex
         }
     }
 
+    private boolean getBypassHostnameVerification(ClientRequest request, \
HttpURLConnection connection) { +        return \
((ClientConfig)request.getAttribute(WinkConfiguration.class)) +            \
.getBypassHostnameVerification() && (connection instanceof HttpsURLConnection); +    \
} +    
+    private HostnameVerifier setupHostnameVerificationBypass(HttpsURLConnection \
connection) { +        HttpsURLConnection https = ((HttpsURLConnection)connection);
+        HostnameVerifier hv = https.getHostnameVerifier();
+        https.setHostnameVerifier(new HostnameVerifier() {
+            public boolean verify(String urlHostName, SSLSession session) {
+                logger.trace("Bypassing hostname verification: URL host is " + \
urlHostName //$NON-NLS-1$ +                    + ", SSLSession host is " \
//$NON-NLS-1$ +                    + session.getPeerHost());
+                return true;
+            }
+        });
+        return hv;
+    }
+    
+    private void teardownHostnameVerificationBypass(HttpsURLConnection connection, \
HostnameVerifier hv) { +        connection.setHostnameVerifier(hv);
+    }
+    
     private HttpURLConnection processRequest(ClientRequest request, HandlerContext \
context)  throws IOException {
         HttpURLConnection connection = openConnection(request);
@@ -66,26 +89,15 @@ public class HttpURLConnectionHandler ex
         OutputStream os = ncos;
         processRequestHeaders(request, connection);
         HostnameVerifier hv = null;
-        boolean bypassHostnameVerification =
-            ((ClientConfig)request.getAttribute(WinkConfiguration.class))
-                .getBypassHostnameVerification() && (connection instanceof \
                HttpsURLConnection);
-        if (bypassHostnameVerification) {
-            HttpsURLConnection https = ((HttpsURLConnection)connection);
-            hv = https.getHostnameVerifier();
-            https.setHostnameVerifier(new HostnameVerifier() {
-                public boolean verify(String urlHostName, SSLSession session) {
-                    logger.trace("Bypassing hostname verification: URL host is " + \
                urlHostName //$NON-NLS-1$
-                        + ", SSLSession host is " //$NON-NLS-1$
-                        + session.getPeerHost());
-                    return true;
-                }
-            });
+        if (getBypassHostnameVerification(request, connection)) {
+            hv = setupHostnameVerificationBypass((HttpsURLConnection)connection);
         }
         try {
             connection.connect();
         } finally {
-            if (bypassHostnameVerification)
-                ((HttpsURLConnection)connection).setHostnameVerifier(hv);
+            if (getBypassHostnameVerification(request, connection)) {
+                teardownHostnameVerificationBypass((HttpsURLConnection) connection, \
hv); +            }
         }
         if (request.getEntity() != null) {
             ncos.setOutputStream(connection.getOutputStream());
@@ -158,11 +170,21 @@ public class HttpURLConnectionHandler ex
 
     private ClientResponse createResponse(ClientRequest request, HttpURLConnection \
connection)  throws IOException {
+        HostnameVerifier hv = null;
+        if (getBypassHostnameVerification(request, connection)) {
+            hv = setupHostnameVerificationBypass((HttpsURLConnection)connection);
+        }
         ClientResponse response = new ClientResponseImpl();
-        response.setStatusCode(connection.getResponseCode());
-        response.setMessage(connection.getResponseMessage());
-        response.getAttributes().putAll(request.getAttributes());
-        processResponseHeaders(response, connection);
+        try {
+            response.setStatusCode(connection.getResponseCode());
+            response.setMessage(connection.getResponseMessage());
+            response.getAttributes().putAll(request.getAttributes());
+            processResponseHeaders(response, connection);
+        } finally {
+            if (getBypassHostnameVerification(request, connection)) {
+                teardownHostnameVerificationBypass((HttpsURLConnection) connection, \
hv); +            }
+        }
         return response;
     }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic