CXF Interceptor example: how to get HTTP Headers in CXF

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.


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() {

   public void handleMessage(SoapMessage message) throws Fault {
      SOAPMessage doc = message.getContent(SOAPMessage.class);
      if (doc == null) {
         doc = message.getContent(SOAPMessage.class);
      SOAPHeader headerr = null;
      try {
         headerr = doc.getSOAPHeader();
      } catch (SOAPException e) {
      if (headerr != null) {
         NodeList nodes = headerr.getElementsByTagNameNS("", "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);
         //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.

Оцените статью
Добавить комментарий

Your email address will not be published. Required fields are marked *

  1. Emily N.

    Hi, I’m very interested in Linux but Im a Super Newbie and I’m having trouble deciding on the right distribution for me (Havent you heard this a million times?) anyway here is my problem, I need a distribution that can switch between reading and writing in English and Japanese (Japanese Language Support) with out restarting the operating system.