How to access HTTP headers in Spring-ws endpoint?

Juha Syrjälä picture Juha Syrjälä · Oct 20, 2010 · Viewed 13.2k times · Source

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?

Answer

Juha Syrjälä picture Juha Syrjälä · Oct 26, 2010

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;
}