Get the session ID in rest service plugin

Graeme Mitchell November 20, 2013

I have developed a rest service plugin on my jira instance which acts as a middle man service. This service does some magic before eventually calling the standard create Issue service in the standard Jira Rest API.

I'm trying to get the user context / credentials used in the call to my middle man service and pass them in the call to standard create issue rest service. I'm trying to do this by using a cookie in the header, something like this:-

ClientResponse response = webResource.header("Cookie", "JSESSIONID=" + sessionId)
   							         .type("application/json")
				    				 .accept("application/json")
				    				 .post(ClientResponse.class, json);

The problem is that I don't know how to find the session id within the rest service (sessionId variable in the above code). I've done a similar thing in a table panel plugin which calls a rest service and this works fine, something like this:-

Map sessionMap = ActionContext.getSession();
String sessionId = ((String) sessionMap.get("ASESSIONID")).replaceAll("[0-9a-z]*-", "");

How can I get the session Id within a rest service?

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
Ian_Gordon November 21, 2013

You can inject an HttpHeaders parameter into your REST method:

@GET
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getSomething(@Context HttpHeaders headers, @QueryParam("username") String username)
{
    ...
    Map<String, Cookie> existingCookies = headers.getCookies();
}

Alternatively, you could use @CookieParam if you just wanted the JSESSIONID, viz:

@GET
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getSomething(@CookieParam("JSESSIONID") String jsessionId, @QueryParam("username") String username)
{
    ....
}

Graeme Mitchell November 21, 2013

Works perfectly! Thanks Ian!

KPSA June 23, 2017

Graeme, would you mind posting some code on how you actually did that? I tried the same, but simply adding the JESSIONID-Cookie didn't help.

TAGS
AUG Leaders

Atlassian Community Events