Add header for all outgoing CXF requests

Is it possible to add a header to all outgoing cxf connections from the client side.

Using Spring 3.0 and CXF 2.6.0

+6
source share
3 answers

this is how i did it

Spring.xml

<import resource="classpath:META-INF/cxf/cxf.xml" /> <bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus"> <property name="outInterceptors"> <list> <ref bean="headerInterceptor"/> </list> </property> <property name="inInterceptors"> <list> <ref bean="headerInterceptor"/> </list> </property> </bean> <bean id="headerInterceptor" class="logging.Interceptor"/> 

interceptor:

  public class UUIDHeaderInterceptor extends AbstractPhaseInterceptor { private static final Logger logger = LoggerFactory.getLogger(UUIDHeaderInterceptor.class); public UUIDHeaderInterceptor() { super(Phase.RECEIVE); } @Override public void handleMessage(Message message) throws Fault { Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS); headers.put(REQUEST_ID_ATTRIBUTE_NAME, Arrays.asList(new String[]{"TEST"})); } } @Override public void handleFault(Message message) { handleMessage(message); } 
+1
source

I would like to give my two cents here. I solve the same case here in my post -

http://saurzcode.in/2014/05/08/adding-header-to-soap-request-using-cxf-2/

Spring Configuration: -

 <jaxws:client id="mywebServiceClient" serviceClass="com.saurzcode.TestService" address="http://saurzcode.com:8088/mockTestService"> <jaxws:binding> <soap:soapBinding version="1.2" mtomEnabled="true" /> </jaxws:binding> </jaxws:client> <cxf:bus> <cxf:outInterceptors> <bean class="com.saurzcode.ws.caller.SoapHeaderInterceptor" /> </cxf:outInterceptors> </cxf:bus> 

CXF Interceptor -

 public class SoapHeaderInterceptor extends AbstractSoapInterceptor { public SoapHeaderInterceptor() { super(Phase.POST_LOGICAL); } @Override public void handleMessage(SoapMessage message) throws Fault { List<Header> headers = message.getHeaders(); TestHeader testHeader = new TestHeader(); JAXBElement<TestHeader> testHeaders = new ObjectFactory() .createTestHeader(testHeader); try { Header header = new Header(testHeaders.getName(), testHeader, new JAXBDataBinding(TestHeader.class)); headers.add(header); message.put(Header.HEADER_LIST, headers); } catch (JAXBException e) { e.printStackTrace(); } } 
+5
source

I already know two ways to do this. One of them is to create a SOAP handler and register it as a JAX-WS handler in the Spring configuration.

Check out my answer here how to create a SOAP handler. Since you want the header to appear in the response (outgoing request), do not forget that you need to check if the message is outgoing, something like this:

 Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outbound) { //Modify your header. } 

Another, maybe easier way. Place the header directly in the context of the CXF response. Please note that this example is just a proof of concept, I don’t know the actual situation where you need the credentials in the answer. It will show how to add a user credential object to the header, you should change it depending on your needs.

 private void modifyResponse(String username, String password) { UserCredentials authHeader = new UserCredentials(); authHeader.setUsername(username); authHeader.setPassword(password); ArrayList<Header> headers = new ArrayList<Header>(1); try { Header soapHeader = new Header( new QName("http://yournamespaceuri.com/something", "UserCredentials"), authHeader, new JAXBDataBinding(UserCredentials.class)); headers.add(soapHeader); } catch (JAXBException ex) { LOGGER.error("Exception trying to serialize header: {}", ex); } ((BindingProvider) proxy).getResponseContext().put(Header.HEADER_LIST, headers); } 

This method must be called immediately after the request of your client.

+2
source

Source: https://habr.com/ru/post/952811/


All Articles