CXF Interceptor Overview:

CXF has high federated extensibility which benefits from the design of interceptor and InterceptorChain, Interceptor is the fundamental processing unit inside CXF, the series of interceptors inside CXF are responsible for the whole handling process started by invoking a web service request to sent a response. and includes processing soap XML, transforming data, parsing headers and even more.

Problem Statement:

To retrieve the header messages(e.g. client IP address, the version of web browser, the client language or any special text messages), the best solution is to customize an interceptor and add it to chain.

Solution:

We created a class UserCredientialInterceptor which extends AbstractSoapInterceptor, and extended abstract method handleMessage(SoapMessage message), in this method, we use (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST) to get IP address of client browser.

package com.interceptor;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.w3c.dom.NodeList;

import javax.servlet.http.HttpServletRequest;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import java.util.Date;

public class UserCredientialInterceptor extends AbstractSoapInterceptor {
   private SAAJInInterceptor saajIn = new SAAJInInterceptor();

   public UserCredientialInterceptor() {
      super(Phase.PRE_PROTOCOL);
      getAfter().add(SAAJInInterceptor.class.getName());
   }

   public void handleMessage(SoapMessage message) throws Fault {
      SOAPMessage doc = message.getContent(SOAPMessage.class);
      if (doc == null) {
         saajIn.handleMessage(message);
         doc = message.getContent(SOAPMessage.class);
      }
      SOAPHeader headerr = null;
      try {
         headerr = doc.getSOAPHeader();
      } catch (SOAPException e) {
         e.printStackTrace();
      }
      if (headerr != null) {
         NodeList nodes = headerr.getElementsByTagNameNS("http://asjava.com/types", "Username");
         if (nodes != null && nodes.item(0) != null) {
            String user =Â nodes.item(0).getTextContent();
         }
      }

      //if you want to read more http header messages, just use get method to obtain from  HttpServletRequest.
      HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
      if(null!=request){
         //Read http header to get client IP adress
         String addr = request.getRemoteAddr();
         //Read http header to get HeaderNames
         Enumeration enums = request.getHeaderNames();
         //Read http header to get cookie/
         Cookie[] cookies = request.getCookies();
      }
   }
}

If you are newbie for CXF, you may get starting from CXF web service example .