CXF Interceptor Example

CXF Interceptor Example explains about intercepting request/response of a CXF message

How to add an interceptor to the CXF ?

How to modify CXF message using interceptor?

In CXF framework InterceptorChains are divided up into Phases. Each request/response are going through different phases, we can intercept request and response on these phases with minimum effort.

CXF also providing lot of in built interceptors too, you can see them on the below link as CXF contributed interceptors

you can see following in-build interceptors available with CXF

Default JAX-WS Incoming interceptor chain (Server): Default Outgoing chain stack (Server):
AttachmentInInterceptor HolderOutInterceptor
StaxInInterceptor SwAOutInterceptor
ReadHeadersInterceptor WrapperClassOutInterceptor
SoapActionInInterceptor SoapHeaderOutFilterInterceptor
MustUnderstandInterceptor SoapActionOutInterceptor
SOAPHandlerInterceptor MessageSenderInterceptor
LogicalHandlerInInterceptor SoapPreProtocolOutInterceptor
CheckFaultInterceptor AttachmentOutInterceptor
URIMappingInterceptor StaxOutInterceptor
DocLiteralnInterceptor SoapHandlerInterceptor
SoapHeaderInterceptor SoapOutInterceptor
WrapperClassInInterceptor LogicalHandlerOutInterceptor
SwAInInterceptor WrapperOutInterceptor
HolderInInterceptor BareOutInterceptor
ServiceInvokerInInterceptor  

Here I am showing an example of how to intercept a CXF SOAP message

I am going to reuse CXF Web Service Tutorial

By using this interceptor, we are changing the inputted student name from Rockey to Ramu

ServiceResponseInterceptor

package com.student;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class ServiceResponseInterceptor extends AbstractPhaseInterceptor {

 
public ServiceResponseInterceptor() {
     
super(Phase.RECEIVE);
 
}
 
 
public void handleMessage(Message message) {
   
System.out.println("message "+message);
    message.put
(Message.ENCODING, "UTF-8");
    InputStream is = message.getContent
(InputStream.class);
   
   
if(is!=null){
     
CachedOutputStream bos = new CachedOutputStream();
     
try{
       
IOUtils.copy(is,bos);
        String soapMessage =
new String(bos.getBytes());
        System.out.println
("-------------------------------------------");
        System.out.println
("incoming message is " + soapMessage);
        System.out.println
("-------------------------------------------");
        bos.flush
();
        message.setContent
(InputStream.class, is);
       
        is.close
();
        InputStream inputStream =
new ByteArrayInputStream(changeName(soapMessage).getBytes());
        message.setContent
(InputStream.class, inputStream);
        bos.close
();
       
} catch (IOException ioe) {
       
ioe.printStackTrace();
     
}
    }
  }

 
private String changeName(String soapMessage) {
   
soapMessage = soapMessage.replaceAll("Rockey", "Ramu");
    System.out.println
("After change message is " + soapMessage);
   
return soapMessage;
 
}
}
1 2 Next





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