Monday, 15 September 2014

java - Maintaining session state across two http different requests in an application -



java - Maintaining session state across two http different requests in an application -

i have scenario want store session info across multiple sessions in application # 2. have 2 applications deployed on tomcat server. our utilize case follows:

a. web application # 1 makes http post request application # 2 using http rest client. post request contains json http request body encapsulating info send application # 2. code block follows:

resttemplate template = new resttemplate(); final searchcustomer client = new searchcustomer(); resttemplate.execute( send_customer_profile, httpmethod.post, new searchrequestcallback(searchcustomer), null);

the request callback function is

static class searchrequestcallback implements requestcallback { /** * write json response request body. */ @override public void dowithrequest(clienthttprequest request) throws ioexception { httpheaders httpheaders = request.getheaders(); list<mediatype> acceptablemediatypes = new linkedlist<>(); acceptablemediatypes.add(mediatype.application_json); httpheaders.setaccept(acceptablemediatypes); httpheaders.setcontenttype(mediatype.application_json); request.getbody().write( new gson().tojson(this.searchcustomer).getbytes(standardcharsets.utf_8.displayname())); } }

the sec application has spring controller next set up

@controller public class searchcustomercontroller { /** * builds client profile knowledge graph. * * <p>this invoked synchronous request. */ @requestmapping(value="/searchprofilepayload.go", method=requestmethod.post) @responsestatus(httpstatus.ok) public void constructsearchcustomerprofileknowledgegraph( @requestbody final searchcustomer customer, httpservletrequest request) { usercontext usercontext = (usercontext) request.getsession().getattribute("usercontext"); if (usercontext == null) { // perform heavy operation fetch user session. usercontext = usercontexthelper.getusercontext(request); request.getsession("usercontext", usercontext) } usercontext.setcustomerprofile(customer); } }

when create phone call uri within application # 2 via browser, want done in such way session attributes retained when making call. there way that?

i know url rewriting stores jsessionidin cookie, don't think how can set value when making rest call, , using same jessionid maintain session attributes.

if has improve way this, gratefully appreciate it. these have no answers. have looked @ these links, none seem reply question.

maintaining session state outside request http , sessions comparison of ways maintain state

thanks,

jraahhali spot on.

set cookie header value of jsessionid=${sessionid} or utilize straight in url per url rewriting link.

first step retrieve jsessionid initial response (this depend on how decide set session id - url or cookies, lets assume cookie now)

@override public void dowithrequest(clienthttprequest request) throws ioexception { httpheaders httpheaders = request.getheaders(); list<mediatype> acceptablemediatypes = new linkedlist<>(); acceptablemediatypes.add(mediatype.application_json); httpheaders.setaccept(acceptablemediatypes); httpheaders.setcontenttype(mediatype.application_json); request.getbody().write( new gson().tojson(this.searchcustomer).getbytes(standardcharsets.utf_8.displayname())); clienthttpresponse response = request.execute(); string sessionid = response.getheaders().get(httpheaders.set_cookie).split(":")[1].trim(); // didnt test this, prolly npe :p this.sessionid = sessionid; }

then in subsequent requests (ie app #1 or browser or whatever)

if (this.sessionid != null && !this.sessionid.equals("")) httpheaders.set(httpheaders.cookie, "jsessionid=" + this.sessionid); // ... request.execute();

note if want utilize browser other client utilize url rewriting method ease of utilize ...

java rest session servlets cookies

No comments:

Post a Comment