Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit dd10bdd

Browse files
author
Miroslav Fuksa
committed
Merge pull request #45 from martinrleon/2.4.x
Fixed setRequestMethodViaJreBugWorkaround() to work with HTTPS as well as HTTP
2 parents 78afc6e + 045ba16 commit dd10bdd

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,57 @@ private static void setRequestMethodViaJreBugWorkaround(final HttpURLConnection
391391
} catch (final ProtocolException pe) {
392392
try {
393393
final Class<?> httpURLConnectionClass = httpURLConnection.getClass();
394-
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
395-
@Override
396-
public Object run() throws NoSuchFieldException, IllegalAccessException {
397-
final Field methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method");
398-
methodField.setAccessible(true);
399-
methodField.set(httpURLConnection, method);
400-
return null;
401-
}
402-
});
394+
AccessController
395+
.doPrivileged(new PrivilegedExceptionAction<Object>() {
396+
@Override
397+
public Object run() throws NoSuchFieldException,
398+
IllegalAccessException {
399+
try {
400+
httpURLConnection.setRequestMethod(method);
401+
// Check whether we are running on a buggy
402+
// JRE
403+
} catch (final ProtocolException pe) {
404+
Class<?> connectionClass = httpURLConnection
405+
.getClass();
406+
Field delegateField = null;
407+
try {
408+
delegateField = connectionClass
409+
.getDeclaredField("delegate");
410+
delegateField.setAccessible(true);
411+
HttpURLConnection delegateConnection = (HttpURLConnection) delegateField
412+
.get(httpURLConnection);
413+
setRequestMethodViaJreBugWorkaround(
414+
delegateConnection, method);
415+
} catch (NoSuchFieldException e) {
416+
// Ignore for now, keep going
417+
} catch (IllegalArgumentException e) {
418+
throw new RuntimeException(e);
419+
} catch (IllegalAccessException e) {
420+
throw new RuntimeException(e);
421+
}
422+
try {
423+
Field methodField;
424+
while (connectionClass != null) {
425+
try {
426+
methodField = connectionClass
427+
.getDeclaredField("method");
428+
} catch (NoSuchFieldException e) {
429+
connectionClass = connectionClass
430+
.getSuperclass();
431+
continue;
432+
}
433+
methodField.setAccessible(true);
434+
methodField.set(httpURLConnection,
435+
method);
436+
break;
437+
}
438+
} catch (final Exception e) {
439+
throw new RuntimeException(e);
440+
}
441+
}
442+
return null;
443+
}
444+
});
403445
} catch (final PrivilegedActionException e) {
404446
final Throwable cause = e.getCause();
405447
if (cause instanceof RuntimeException) {

0 commit comments

Comments
 (0)