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
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;
}
}
Modify cxf.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cxf="http://cxf.apache.org/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <!--CXF interceptor Example --> <bean class="com.student.ServiceResponseInterceptor" id="ServiceRequestInterceptor" /> <cxf:bus> <cxf:inInterceptors> <ref bean="ServiceRequestInterceptor" /> </cxf:inInterceptors> </cxf:bus> <jaxws:endpoint id="changeStudent" implementor="com.student.ChangeStudentDetailsImpl" address="/ChangeStudent" /> </beans>
Now run the below client and see the output
Run
import org.apache.cxf.ext.logging.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.student.ChangeStudentDetails;
import com.student.Student;
// CXF JAX-WS Client / Consuming Web Services With CXF
public final class StudentClient {
public static void main(String args[]) throws Exception {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ChangeStudentDetails.class);
factory.setAddress("http://localhost:8080/CXFTutorial/ChangeStudent?wsdl");
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
ChangeStudentDetails client = (ChangeStudentDetails) factory.create();
Student student = new Student();
student.setName("Rockey");
Student changeName = client.changeName(student);
System.out.println("Server said: " + changeName.getName());
System.exit(0);
}
}
Output
Server said: Hello RamuWe are actually sending the name as Rockey but it is changed to Ramu dynamically using CXF interceptor.