views:

33

answers:

2

How can I access HTTP headers in Spring-ws endpoint?

My code looks like this:

public class MyEndpoint extends AbstractMarshallingPayloadEndpoint {
  protected Object invokeInternal(Object arg) throws Exception {
      MyReq request = (MyReq) arg;
      // need to access some HTTP headers here
      return createMyResp();
  }
}

invokeInternal() gets only the unmarshalled JAXB object as the parameter. How can I access HTTP headers that came with the request inside invokeInternal()?

One way that would probably work is to create a Servlet filter that stores header values to ThreadLocal variable that is then accessed inside invokeInternal(), but is there a nicer, more spring-like way to do this?

A: 

I had the same kind of problem (see this other question). I needed to add a Content-Type header to my WS. I went the road of the Servlet Filter. Most of the time, you should not need to change HTTP headers in a webservice. But ... there is sometime a diference between theory and practice.

Guillaume
A: 

You can add these methods. The TransportContextHolder will hold some data related to transport (HTTP in this case) in a thread local variable. You can access HttpServletRequest from the TransportContext.

protected HttpServletRequest getHttpServletRequest() {
    TransportContext ctx = TransportContextHolder.getTransportContext();
    return ( null != ctx ) ? ((HttpServletConnection ) ctx.getConnection()).getHttpServletRequest() : null;
}

protected String getHttpHeaderValue( final String headerName ) {
    HttpServletRequest httpServletRequest = getHttpServletRequest();
    return ( null != httpServletRequest ) ? httpServletRequest.getHeader( headerName ) : null;
}
Juha Syrjälä