Now you can work around this by using the Apache HTTP Client adapter for Jersey; but this is not always available. So if you want to use the Jersey client with cookies in a server environment you need to do a little bit of reflection to ensure you use your own private cookie jar.
final CookieHandler ch = new CookieManager(); Client client = new Client(new URLConnectionClientHandler( new HttpURLConnectionFactory() { @Override public HttpURLConnection getHttpURLConnection(URL uRL) throws IOException { HttpURLConnection connect = (HttpURLConnection) uRL.openConnection(); try { Object toModify = connect; if (!(toModify instanceof sun.net.www.protocol.http.HttpURLConnection)) { Field delegateField = connect.getClass().getDeclaredField("delegate"); delegateField.setAccessible(true); toModify = MethodHandles.lookup().unreflectGetter(delegateField) .bindTo(toModify).invoke(); } Field cookieField = sun.net.www.protocol.http.HttpURLConnection.class.getDeclaredField("cookieHandler"); cookieField.setAccessible(true); MethodHandle mh = MethodHandles.lookup().unreflectSetter(cookieField); mh.bindTo(toModify).invoke(ch); } catch (Throwable e) { e.printStackTrace(); } return connect; } }));
This will only work if your environment is using the internal implementation of
sun.net.www.protocol.http.HttpURLConnection
that comes with the JDK. This appears to be the case for modern versions of WLS.For JAX-RS 2.0 you can do a similar change using Jersey 2.x specific ClientConfig class and HttpUrlConnectorProvider.
final CookieHandler ch = new CookieManager(); Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new HttpUrlConnectorProvider().connectionFactory(new HttpUrlConnectorProvider.ConnectionFactory() { @Override public HttpURLConnection getConnection(URL uRL) throws IOException { HttpURLConnection connect = (HttpURLConnection) uRL.openConnection(); try { Object toModify = connect; if (!(toModify instanceof sun.net.www.protocol.http.HttpURLConnection)) { Field delegateField = connect.getClass().getDeclaredField("delegate"); delegateField.setAccessible(true); toModify = MethodHandles.lookup().unreflectGetter(delegateField) .bindTo(toModify).invoke(); } Field cookieField = sun.net.www.protocol.http.HttpURLConnection.class.getDeclaredField("cookieHandler"); cookieField.setAccessible(true); MethodHandle mh = MethodHandles.lookup().unreflectSetter(cookieField); mh.bindTo(toModify).invoke(ch); } catch (Throwable e) { e.printStackTrace(); } return connect; } })));
Update 11th Feb 2015: It seems in some cases, in particular using https, I have seen the HttpURLConnection wrapped in another class, to work around this just use reflection to access the value of the delegate field. I have updated the code examples to reflect this issue.
No comments:
Post a Comment