Imran khan
Imran khan

Reputation: 847

How to get the JSESSIONID from a HTTP request using ContainerRequestContext?

I have the following HTTP headers in a request and I want to extract the JSESSIONID from it:

Accept=application/json
Accept-Encoding=gzip
deflate,Accept-Language=en-us
Connection=keep-alive
Content-Length=0
Content-Type=application/json
Cookie=JSESSIONID=ss0ox8w99o9142b73rssvc0r
Host=localhost:8080
User-Agent=Test_QA/34 CFNetwork/711.5.7 Darwin/14.5.0 (x86_64)

I'm using a ContainerRequestContext as following:

@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
    System.out.println("***HEADER VALUE**: " + requestContext.getHeaderString("Cookie"));
}

Getting result as:

JSESSIONID=zv71od6l2fd41hv6yf0980khy

And:

@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
    Map<String, javax.ws.rs.core.Cookie> clientCookie = requestContext.getCookies();
    System.out.println("Client Cookie Map: " + clientCookie);
}

Getting result as:

Clinet Cookie Map: {JSESSIONID=JSESSIONID=1of1x5u1s1l4hdxfg2azlep42}

What is the best way to extract the JSESSIONID from the request?

Upvotes: 3

Views: 35531

Answers (1)

cassiomolin
cassiomolin

Reputation: 131037

A step back

First of all, REST and session identifiers don't sound well in the same sentence.

The S in REST means stateless and not stateful. In REST applications, the session state must be managed by the client and not by the server. Consequently, there must not be any session identifiers.

For more information, have a look here and here.

Getting a value from a cookie in JAX-RS

I think you are looking for the following solution:

Cookie cookie = requestContext.getCookies().get("JSESSIONID");
String value = cookie.getValue();

For more information about Cookie, have a look at the documentation.

Upvotes: 8

Related Questions