Get HTTP Header From Service Implementator Class With CXF

SOAP runtimes like CXF allows to interact with the inbound/outbound messages with interceptors.
Interceptors are the fundamental processing unit inside CXF. When a service is invoked, an InterceptorChain is created and invoked. Each interceptor gets a chance to do what they want with the message. This can include reading it, transforming it, processing headers, validating the message, etc.

Interceptors are used with both CXF clients and CXF servers. When a CXF client invokes a CXF server, there is an outgoing interceptor chain for the client and an incoming chain for the server. When the server sends the response back to the client, there is an outgoing chain for the server and an incoming one for the client.

In same cases can be useful access that values directly in the WebService implementation class.
The code below shows how to access the HTTP header of the SOAP request and extract the “Content-Type” value.

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

public class MyServiceInterface {

    public MyMethodResponse getStuff(
        @WebParam(name = "GetStuffRequest", targetNamespace = "urn:madbit:my-service:types", partName = "request")
        MyMethodRequest request) {
		Message message = PhaseInterceptorChain.getCurrentMessage();
		// get the HTTP request
		HttpServletRequest httpRequest = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
		// get the HTTP request
		String httpHeader = httpRequest.toString();
		// get the Content-Type value from header
		String contentType = httpRequest.getHeader("Content-Type");

If you want to learn more retro games news, read our magazine and subscribe to our newsletter.