The interceptor-based solution provides a very flexible way of configuring custom Header for Apache CXF based SOAP clients.For custom Header configuration per operation(Methods defined inside WSDL), will use an interceptor that sets a custom header at request.
Java Interceptor :
This handleMessage method takes a soap message and set header with it
Spring Configuration :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<cxf:cxfEndpoint id="bookService"
address="http://locathost:8080/someServiceName"
serviceClass="someServiceClass" />
<cxf:outInterceptors>
<ref bean="customSOAPHeader" />
</cxf:outInterceptors>
</cxf:cxfEndpoint>
<bean id="customSOAPHeader" class="sample.camel.SOAPCustomHeaderInterceptor"
</bean>
</key>
</entry>
</map>
</property>
</bean>
</beans>
Above configuration defines custom header per operation wise.
Sample Example output of above things for custom SOAP header :
Java Interceptor :
com.interceptor;
import java.util.List;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.headers.Header.Direction;
import org.apache.cxf.interceptor.MessageSenderInterceptor;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.phase.Phase;
import org.springframework.beans.factory.annotation.Value;
import com.temp.THeader;
public class SOAPCustomHeaderInterceptor extends AbstractSoapInterceptor {
public SOAPCustomHeaderInterceptor(String phase) {
super(phase);
addBefore(MessageSenderInterceptor.class.getName());
}
public SOAPCustomHeaderInterceptor() {
this(Phase.PREPARE_SEND);
}
/**
* Setting Custom Headers
*
* @param message
*/
@Override
public void handleMessage(SoapMessage message) {
List<Header> headers = message.getHeaders();
THeader theader = new THeader();
theader.setCountry("IN");
theader.setState("TN");
SoapHeader newHeader;
try {
newHeader = new SoapHeader(new QName("ns2:subHeader"), theader, new JAXBDataBinding(THeader.class));
newHeader.setDirection(Direction.DIRECTION_IN);
headers.add(newHeader);
} catch (JAXBException exception) {
exception.printStackTrace();
}
}
}
Spring Configuration :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<cxf:cxfEndpoint id="bookService"
address="http://locathost:8080/someServiceName"
serviceClass="someServiceClass" />
<cxf:outInterceptors>
<ref bean="customSOAPHeader" />
</cxf:outInterceptors>
</cxf:cxfEndpoint>
<bean id="customSOAPHeader" class="sample.camel.SOAPCustomHeaderInterceptor"
</bean>
</key>
</entry>
</map>
</property>
</bean>
</beans>
Above configuration defines custom header per operation wise.
Sample Example output of above things for custom SOAP header :
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<ns2:subHeader xmlns:ns2="http://www.test.com">
<ns2:Country>IN</ns2:Country>
<ns2:State>TN</ns2:State>
</ns2:subHeader>
</soap:Header>
<soap:Body>
<ns2:changeName xmlns:ns2="http://book.com/">
<arg0>
<name>Rockey</name>
</arg0>
</ns2:changeName>
</soap:Body>
</soap:Envelope>
No comments:
Post a Comment