...
Code Block |
---|
|
public interface InterceptorProvider {
List<Interceptor> getInInterceptors();
List<Interceptor> getOutInterceptors();
List<Interceptor> getOutFaultInterceptors();
List<Interceptor> getInFaultInterceptors();
}
|
...
Code Block |
---|
|
import java.io.IOException;
import org.apache.cxf.attachment.AttachmentDeserializer;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
public class AttachmentInInterceptor extends AbstractPhaseInterceptor<Message> {
public AttachmentInInterceptor() {
super(Phase.RECEIVE);
}
public void handleMessage(Message message) {
String contentType = (String) message.get(Message.CONTENT_TYPE);
if (contentType != null && contentType.toLowerCase().indexOf("multipart/related") != -1) {
AttachmentDeserializer ad = new AttachmentDeserializer(message);
try {
ad.initializeAttachments();
} catch (IOException e) {
throw new Fault(e);
}
}
}
public void handleFault(Message messageParam) {
}
}
|
...
Code Block |
---|
|
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.cxf.binding.soap.Soap11;
import org.apache.cxf.binding.soap.Soap12;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.model.SoapOperationInfo;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.cxf.service.model.OperationInfo;
public class SoapActionInInterceptor extends AbstractSoapInterceptor {
public SoapActionInInterceptor() {
super(Phase.READ);
addAfter(ReadHeadersInterceptor.class.getName());
addAfter(EndpointSelectionInterceptor.class.getName());
}
public void handleMessage(SoapMessage message) throws Fault {
if (message.getVersion() instanceof Soap11) {
Map<String, List<String>> headers = CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS));
if (headers != null) {
List<String> sa = headers.get("SOAPAction");
if (sa != null && sa.size() > 0) {
String action = sa.get(0);
if (action.startsWith("\"")) {
action = action.substring(1, action.length() - 1);
}
getAndSetOperation(message, action);
}
}
} else if (message.getVersion() instanceof Soap12) {
...........
}
}
private void getAndSetOperation(SoapMessage message, String action) {
if ("".equals(action)) {
return;
}
Exchange ex = message.getExchange();
Endpoint ep = ex.get(Endpoint.class);
BindingOperationInfo bindingOp = null;
Collection<BindingOperationInfo> bops = ep.getBinding().getBindingInfo().getOperations();
for (BindingOperationInfo boi : bops) {
SoapOperationInfo soi = (SoapOperationInfo) boi.getExtensor(SoapOperationInfo.class);
if (soi != null && soi.getAction().equals(action)) {
if (bindingOp != null) {
//more than one op with the same action, will need to parse normally
return;
}
bindingOp = boi;
}
}
if (bindingOp != null) {
ex.put(BindingOperationInfo.class, bindingOp);
ex.put(OperationInfo.class, bindingOp.getOperationInfo());
}
}
}
|
You may also want to specify what phase your interceptor is in. To do this, you can simply set the phase:
...
Adding interceptors through configuration
//TODO add the configuration file information
Adding MyInterceptor to the busAdding MyInterceptor to your server:
Code Block |
---|
|
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="MyInterceptor" class="demo.interceptor.MyInterceptor"/>
<!-- We are adding the interceptors to the bus as we will have only one endpoint/service/bus. -->
<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl">/service/bus. -->
<cxf:bus>
<property name="inInterceptors"> <cxf:inInterceptors>
<list>
<ref bean="MyInterceptor"/>
</list>
</property>cxf:inInterceptors>
<property name="outInterceptors"><cxf:outInterceptors>
<list>
<ref bean="MyInterceptor"/>
</list>
</property>cxf:outInterceptors>
</bean> cxf:bus>
</beans>
|
Adding MyInterceptor to your client:
Code Block |
---|
|
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:http="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<http:conduit name="{http://apache.org/hello_world_soap_http}SoapPort9001.http-conduit">
<http:client DecoupledEndpoint="http://localhost:9990/decoupled_endpoint"/>
</http:conduit>
<bean id="MyInterceptor" class="demo.interceptor.MyInterceptor"/>
<!-- We are adding the interceptors to the bus as we will have only one endpoint/service/bus. -->
<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl">
<property name="inInterceptors">
<list>
<ref bean="MyInterceptor"/>
</list>
</property>
<property name="outInterceptors">
<list>
<ref bean="MyInterceptor"/>
</list>
</property>
</bean>
</beans>
|