How to access HTTP headers in Spring-ws endpoint?

荒凉一梦 提交于 2019-11-30 23:56:03

问题


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?


回答1:


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



回答2:


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.




回答3:


You can access to HTTP headers in Spring SOAP Endpoint by injecting HttpServletRequest.

For example, you need to get Autorization header (you use Basic authentication).

SOAP request:

POST http://localhost:8025/ws HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Authorization: Basic YWRtaW46YWRtaW4=
Content-Length: 287
Host: localhost:8025
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tok="http://abcdef.com/integration/adapter/services/Token">
   <soapenv:Header/>
   <soapenv:Body>
      <tok:GetTokenRequest>
      </tok:GetTokenRequest>
   </soapenv:Body>
</soapenv:Envelope>

@Endpoint java class

@Endpoint
@Slf4j
public class TokenEndpoint {

    public static final String NAMESPACE_URI = "http://abcdef.com/integration/adapter/services/Token";
    private static final String AUTH_HEADER = "Authorization";

    private final HttpServletRequest servletRequest;
    private final TokenService tokenService;

    public TokenEndpoint(HttpServletRequest servletRequest, TokenService tokenService) {
        this.servletRequest = servletRequest;
        this.tokenService = tokenService;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetTokenRequest")
    @ResponsePayload
    public GetTokenResponse getToken(@RequestPayload GetTokenRequest request) {
        String auth = servletRequest.getHeader(AUTH_HEADER);
        log.debug("Authorization header is {}", auth);
        return tokenService.getToken(request);
    }
}


来源:https://stackoverflow.com/questions/3975694/how-to-access-http-headers-in-spring-ws-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!