You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2010/12/07 20:42:07 UTC
svn commit: r1043176 -
/incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java
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/apache/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;
}