1

I'm working on system which will allow users to create workspaces.After workspace creation user may login by providing username, password and workspace. And i need jsessionid generated after login to create connection between workspace and jsessionid.

Login goes thru REST.

    @POST
    @Path("login")
    @Produces(ContentType.APPLICATION_JSON_CHARSET_UTF_8)
    public JsonObject login(LoginCredentials loginCredentials) throws UnAuthorizedException {
        //login logic
    }

I also have interceptor implementing ContainerResponseFilter, WriterInterceptor.

@Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext context)throws IOException {
        //...
    }

how can i get generated jsessionid (which i see in response header)?

Nem
  • 337
  • 3
  • 7
  • 20

3 Answers3

2

If you have a http request object you can get hold of the associated session. Once you've got the session you can get its id

request.getSession(true).getId()

Passing true to getSession causes a new session to be created if one does not already exist.

DaveH
  • 6,831
  • 5
  • 28
  • 48
  • Thanks for the tip. I've got the sessionid in login function from @Context HttpServletRequest request. request.getSession(false).getId(). – Nem May 06 '15 at 11:06
1

Assuming you are using Jersey for the REST services, You can use e.g.

@Context HttpServletRequest request

in your service method signatures in order to access the response headers. Take a look at https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2742 and Under what conditions is a JSESSIONID created? (this one is about JSessionID)

Community
  • 1
  • 1
francesco foresti
  • 1,914
  • 16
  • 21
  • Thanks for the tip. I've used your and @DaveHowes tips to come up with the solution. – Nem May 06 '15 at 11:07
0

In your interceptor you can get a request coming to your service and extract Headers or anything you put in the Request (so your JSESSIONID as well):

HttpServletRequest request = (HttpServletRequest) message
            .getExchange().getInMessage()
            .get(AbstractHTTPDestination.HTTP_REQUEST);
m4tt
  • 102
  • 4