Announcement Announcement Module
Collapse
No announcement yet.
Setting the HTTP cookie header to apply a JSESSIONID Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting the HTTP cookie header to apply a JSESSIONID

    Hi,

    In a nutshell, when using the Spring WebServiceTemplate (Just ripped off the plain old XML (pox) webservice client example) I need a way to set the HTTP header so that I can set a Cookie with a JSESSIONID parameter. There seems to be no way to do this without mangling the spring ws framework so I'm think I am missing a trick here. Anyone any ideas?

    Further Detail:

    We have a requirement where a user logs onto our site and we stick a logon object in the session. I then want to use a spring web service client (via an thirdparty applet) to make a request to a web service in the same application.
    The web service client and actual web service has not been developed yet but we were thinking of just using the pox spring webservice and client example in the samples that come with spring ws 1.5.7 (plain old XML).

    Problem here is that we will need to get the current Cookie JSESSIONID and pass that through to the POX webservice client but there seems to be no easy way to do this without rewriting portions of the ws template.

    Any ideas?

  • #2
    One way using TheadLocal and a ClientInterceptor

    Before the web service call I can bind the current cookie value to a Thread Local:

    Code:
    CookieHolder.setCookieValue("JSESSIONID=1234;");
    (obviously I would get this ID from the HttpServletRequest)

    and the call the webservice template

    Code:
    getWebServiceTemplate().sendSourceAndReceiveToResult . . .
    and add an interceptor which does the following:
    Code:
    public class AddCookieHeaderInterceptor implements ClientInterceptor {
    
    	public boolean handleFault(MessageContext messageContext)
    			throws WebServiceClientException {
    		return true;
    	}
    
    	public boolean handleRequest(MessageContext messageContext)
    			throws WebServiceClientException {
    		TransportContext context = TransportContextHolder.getTransportContext();
    		CommonsHttpConnection connection = (CommonsHttpConnection )context.getConnection();
    		String cookieValue = CookieHolder.getCookieValue();
    		if(cookieValue!=null){
    			connection.getPostMethod().addRequestHeader("Cookie", cookieValue);
    		}
    		return true;
    	}
    
    	public boolean handleResponse(MessageContext messageContext)
    			throws WebServiceClientException {
    		return true;
    	}
    
    }
    This works but I can't help feeling there is a better way!!

    Comment


    • #3
      The WebServiceTemplate allows specifying MessageSenders to use (objects responsible for creating WebServiceConnection). You can use theCommonsHttpMessageSender, which utilizes Jakarta Commons HttpClient.
      HttpClient is quite flexible and you should be able to do what you need with it.

      Comment

      Working...
      X