I am trying to write a library that accesses a RESTful web service using the Jersey Client API. The service requires a login request that sets a cookie, then subsequent req
This is the jersey v2 equivalent for @Emmanuel Touzery's Answer:
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
...
...
ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.property(ApacheClientProperties.DISABLE_COOKIES, false);
client = ClientBuilder.newClient(config);
The problem is WebResource is immutable - the cookie() method returns WebResource.Builder. So, doing the following just creates a new instance of WebResource.Builder every time you call cookie (and does not modify the original WebResource at all). You ignore those Builder instances and still perform the request on the original WebResource:
for ( NewCookie c : CookieJar.Cookies ) {
logger.debug( "Setting cookie " + c.getName() );
wr.cookie( c );
}
You should do the following instead:
WebResource.Builder builder = wr.getRequestBuilder();
for (NewCookie c : CookieJar.Cookies) {
builder = builder.cookie(c);
}
Then you can make the request by:
ClientResponse response = builder.queryParams(qs).get(ClientResponse.class);
Also, to avoid duplicating this code in all your resource methods, you may want to consider writing a client filter, which will do it for you for all your requests. E.g. the following code would ensure cookies sent from the server get set for each response:
client.addFilter(new ClientFilter() {
private ArrayList<Object> cookies;
@Override
public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
if (cookies != null) {
request.getHeaders().put("Cookie", cookies);
}
ClientResponse response = getNext().handle(request);
if (response.getCookies() != null) {
if (cookies == null) {
cookies = new ArrayList<Object>();
}
// simple addAll just for illustration (should probably check for duplicates and expired cookies)
cookies.addAll(response.getCookies());
}
return response;
}
});
NOTE: This will only work if you don't share client instances with multiple threads!
Perhaps you need to fit cookies into a WebResource call that had been working previously without them. Then you might find yourself breaking up your line of code that you can work with the builder part way through. To include cookies, your code might go from:
clientResponse = webResource.queryParams(parameters).type(httpContentType).accept(httpAcceptType).post(ClientResponse.class, requestBody);
To:
builder = webResource.queryParams(parameters).type(httpContentType);
if (cookieJar != null)
{
for (Cookie c : cookieJar)
builder = builder.cookie(c);
}
clientResponse = builder.accept(httpAcceptType).post(ClientResponse.class, requestBody);
I have found that a simpler way to ensure cookies are sent back is to use the Apache HTTP client integration of jersey-client. It is found in the maven package jersey-apache-client:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client</artifactId>
<version>1.13</version>
<type>jar</type>
</dependency>
You can then do:
ApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
config.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true);
ApacheHttpClient client = ApacheHttpClient.create(config);
From then on, just keep using the same client across requests and the cookies will be collected and sent back to the server as expected.