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