Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CXF JAX-RS client proxy not maintaining cookies

I am using CXF to build client code for a JAX-RS REST service. This REST service unfortunately relies on cookies to authenticate each request and maintain other key session state. Accessing the user's account info involves two requests: one to login, and one to get account info. The session cookies retrieved in the first request must be sent with the second request.

Here is my code.

// Login (POST /sessions)
Response response = proxy.login(userCredentials);
assertEquals(200, response.getStatus());

// Get user's account info (GET /user)
response = proxy.getUser();
User user = response.readEntity(User.class);

The second request fails authentication because it does not include the required session cookies that were returned by the prior login operation.

I believe there is a way to configure the WebClient to maintain cookies across requests. I have searched high and low, but I cannot seem to find out how. I'm hoping someone can point me to an example. Thanks in advance!

like image 839
txdev Avatar asked Apr 19 '13 14:04

txdev


1 Answers

I finally found a solution. I had to do the following before using the proxy.

WebClient.getConfig(proxy).getRequestContext().put(
        org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
like image 90
txdev Avatar answered Sep 30 '22 12:09

txdev