Apache CXF: How to add custom SOAP message headers to a web service request?


SOAP headers can be added to a Web service request in different ways, if you use Apache CXF. They way I prefer is the one I’ve mentioned here – as it doesn’t require changes to wsdl or method signatures and it’s much faster as it doesn’t break streaming and the memory overhead is less.

The headers in the list are streamed at the appropriate time to the wire according to the databinding object found in the Header object.

About SOAP headers,

Like any good messaging protocol, SOAP defines the concept of a message header. It is an optional part of any SOAP message. If it exists, the header contains application-specific information (like authentication, payment, etc) about the SOAP message i.e. information about the message, or about the context in which the message is sent, or basically whatever the creator of the message thought was a good idea to put there instead of the actual body of the message. If the Header element is present, it must be the first child element of the Envelope element.

/**
* @author Singaram Subramanian
*
*/

/* Create a ClientProxyFactoryBean reference and assign it an instance of JaxWsProxyFactoryBean, a factory for creating JAX-WS proxies. This class provides access to the internal properties used to set-up proxies. Using it provides more control than the standard JAX-WS APIs. */

ClientProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(singz.ws.cxf.sample.SampleServiceInterface.class);

// Set the web service endpoint URL here
factory.setAddress("http://xxx.xxx.com/services/SampleService/v1");

SampleServiceInterface serviceClient = (SampleServiceInterface) factory.create();

// Get the underlying Client object from the proxy object of service interface
Client proxy = ClientProxy.getClient(serviceClient);

// Creating SOAP headers to the web service request

// Create a list for holding all SOAP headers
List<Header> headersList = new ArrayList<Header>();

Header testSoapHeader1 = new Header(new QName("uri:singz.ws.sample", "soapheader1"), "SOAP Header Message 1", new JAXBDataBinding(String.class));
Header testSoapHeader2 = new Header(new QName("uri:singz.ws.sample", "soapheader2"), "SOAP Header Message 2", new JAXBDataBinding(String.class));

headersList.add(testSoapHeader1);
headersList.add(testSoapHeader2);

// Add SOAP headers to the web service request
proxy.getRequestContext().put(Header.HEADER_LIST, headersList);

More on this @ http://cxf.apache.org/faq.html#FAQ-HowcanIaddsoapheaderstotherequest%2Fresponse%3F

About these ads

4 comments

  1. [...] Apache CXF: How to add custom SOAP headers to the web service request? (singztechmusings.wordpress.com) [...]

  2. Hi Singaram,

    Thanks for posting this useful information.
    Can you please let us know, how we can add child element in a header element.
    e.g.

    1

    Thanks,
    Manoj

    1. Srinivas · · Reply

      I am using testNG test cases for testing cxf webservices as below,

      Here the problem is when I run the command like “ mvn test” then it is executing the test case as below code, it is invoking webservice project which is deployed in tomcat as a war and as well as invokes same project source code in eclipse. How avoid the webservice call in project source code in eclipse while running below test case. Whether it is current approach to add Soap header to cxf interceptor or any other ways to do it, please suggest some thing.

      @Test(enabled = true)
      public void testPassAuthenticationByAddingHeader()
      {
      JaxWsProxyFactoryBean factory =
      new JaxWsProxyFactoryBean();
      factory.setServiceClass(PersonService.class);
      factory.setAddress(“http://172.29.68.62:8080/CXFExampleService/personService”);
      //factory.
      PersonService serviceClient = (PersonService) factory.create();
      Client proxy = ClientProxy.getClient(serviceClient);

      // Creating SOAP headers to the web service request
      // Create a list for holding all SOAP headers
      List headersList = new ArrayList();
      QName qName = new QName(“http://172.29.68.62:8080/CXFExampleService”,
      “Person”);
      Person person = addAuthentication(“test”, “test”);
      JAXBDataBinding dataBinding = getJAXBDataBinding(person);
      Header testSoapHeader1 = new Header(qName, person, dataBinding);
      // Add SOAP headers to the web service request
      headersList.add(testSoapHeader1);
      proxy.getRequestContext().put(Header.HEADER_LIST, headersList);
      // call the webservice
      String message = serviceClient.greetPerson(“Test”);
      System.out.println(“Message in testInterceptorPassCase is ” + message);
      AssertJUnit.assertNotNull(message);
      }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 26 other followers

%d bloggers like this: