From 8cf02a9e8733ad6ee42941eda85d6d13f7eca830 Mon Sep 17 00:00:00 2001 From: Martin Leon Date: Mon, 18 Nov 2013 16:06:24 -0800 Subject: [PATCH 1/2] Fixed setRequestMethodViaJreBugWorkaround() to work with HTTPS as well as HTTP --- .../glassfish/jersey/client/HttpUrlConnector.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java index 19050dd835..0b70564327 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java @@ -358,7 +358,18 @@ private static void setRequestMethodViaJreBugWorkaround(final HttpURLConnection AccessController.doPrivileged(new PrivilegedExceptionAction() { @Override public Object run() throws NoSuchFieldException, IllegalAccessException { - final Field methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method"); + final Class parentClass = httpURLConnectionClass + .getSuperclass(); + final Field methodField; + // If the implementation class is an HTTPS URL Connection, we + // need to go up one level higher in the heirarchy to modify the + // 'method' field. + if (parentClass == HttpsURLConnection.class) { + methodField = parentClass.getSuperclass().getDeclaredField( + "method"); + } else { + methodField = parentClass.getDeclaredField("method"); + } methodField.setAccessible(true); methodField.set(httpURLConnection, method); return null; From a6b348a0d7a68e6790f0d227452753f995c206b2 Mon Sep 17 00:00:00 2001 From: Martin Leon Date: Thu, 21 Nov 2013 14:09:33 -0800 Subject: [PATCH 2/2] Figured out how to use the patch with HTTPS where Delegate is involved. --- .../jersey/client/HttpUrlConnector.java | 71 +++++++++++++------ 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java index 0b70564327..e88a3802f3 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java @@ -355,26 +355,57 @@ private static void setRequestMethodViaJreBugWorkaround(final HttpURLConnection } catch (final ProtocolException pe) { try { final Class httpURLConnectionClass = httpURLConnection.getClass(); - AccessController.doPrivileged(new PrivilegedExceptionAction() { - @Override - public Object run() throws NoSuchFieldException, IllegalAccessException { - final Class parentClass = httpURLConnectionClass - .getSuperclass(); - final Field methodField; - // If the implementation class is an HTTPS URL Connection, we - // need to go up one level higher in the heirarchy to modify the - // 'method' field. - if (parentClass == HttpsURLConnection.class) { - methodField = parentClass.getSuperclass().getDeclaredField( - "method"); - } else { - methodField = parentClass.getDeclaredField("method"); - } - methodField.setAccessible(true); - methodField.set(httpURLConnection, method); - return null; - } - }); + AccessController + .doPrivileged(new PrivilegedExceptionAction() { + @Override + public Object run() throws NoSuchFieldException, + IllegalAccessException { + try { + httpURLConnection.setRequestMethod(method); + // Check whether we are running on a buggy + // JRE + } catch (final ProtocolException pe) { + Class connectionClass = httpURLConnection + .getClass(); + Field delegateField = null; + try { + delegateField = connectionClass + .getDeclaredField("delegate"); + delegateField.setAccessible(true); + HttpURLConnection delegateConnection = (HttpURLConnection) delegateField + .get(httpURLConnection); + setRequestMethodViaJreBugWorkaround( + delegateConnection, method); + } catch (NoSuchFieldException e) { + // Ignore for now, keep going + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + try { + Field methodField; + while (connectionClass != null) { + try { + methodField = connectionClass + .getDeclaredField("method"); + } catch (NoSuchFieldException e) { + connectionClass = connectionClass + .getSuperclass(); + continue; + } + methodField.setAccessible(true); + methodField.set(httpURLConnection, + method); + break; + } + } catch (final Exception e) { + throw new RuntimeException(e); + } + } + return null; + } + }); } catch (final PrivilegedActionException e) { final Throwable cause = e.getCause(); if (cause instanceof RuntimeException) {