DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Consuming SOAP Service With Apache CXF and Spring
  • Web Service Testing Using Neoload
  • Combining gRPC With Guice
  • Implementing WOPI Protocol For Office Integration

Trending

  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Apache Spark 4.0: Transforming Big Data Analytics to the Next Level
  • How to Merge HTML Documents in Java
  • How GitHub Copilot Helps You Write More Secure Code
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. SOAP Webservices Using Apache CXF: Adding Custom Object as Header in Outgoing Requests

SOAP Webservices Using Apache CXF: Adding Custom Object as Header in Outgoing Requests

By 
Saurabh Chhajed user avatar
Saurabh Chhajed
·
May. 29, 14 · Interview
Likes (1)
Comment
Save
Tweet
Share
15.0K Views

Join the DZone community and get the full member experience.

Join For Free

What is CXF?

Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS etc.

How CXF Works?

As you can see here and here, how cxf service calls are processed,most of the functionality in the Apache CXF runtime is implemented by interceptors. Every endpoint created by the Apache CXF runtime has potential interceptor chains for processing messages. The interceptors in the these chains are responsible for transforming messages between the raw data transported across the wire and the Java objects handled by the endpoint’s implementation code. 

Interceptors in CXF

When a CXF client invokes a CXF server, there is an outgoing interceptor chain for the client and an incoming chain for the server. When the server sends the response back to the client, there is an outgoing chain for the server and an incoming one for the client. Additionally, in the case of SOAPFaults, a CXF web service will create a separate outbound error handling chain and the client will create an inbound error handling chain.

The interceptors are organized into phases to ensure that processing happens on the proper order.Various phases involved during the Interceptor chains are listed in CXF documentation here.

Adding your custom Interceptor involves extending one of the Abstract Intereceptor classes that CXF provides, and providing a phase when that interceptor should be invoked.

AbstractPhaseInterceptor class - This abstract class provides implementations for the phase management methods of the PhaseInterceptor interface. The AbstractPhaseInterceptor class also provides a default implementation of the handleFault() method.

Developers need to provide an implementation of the handleMessage() method. They can also provide a different implementation for the handleFault() method. The developer-provided implementations can manipulate the message data using the methods provided by the generic org.apache.cxf.message.Message interface.

For applications that work with SOAP messages, Apache CXF provides an AbstractSoapInterceptor class. Extending this class provides the handleMessage() method and the handleFault() method with access to the message data as an org.apache.cxf.binding.soap.SoapMessage object. SoapMessage objects have methods for retrieving the SOAP headers, the SOAP envelope, and other SOAP metadata from the message.


Below piece of code will show, how we can add a Custom Object as Header to an outgoing request –

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>

 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();

		}

	}

SOAP Web Protocols Apache CXF Object (computer science) Requests

Opinions expressed by DZone contributors are their own.

Related

  • Consuming SOAP Service With Apache CXF and Spring
  • Web Service Testing Using Neoload
  • Combining gRPC With Guice
  • Implementing WOPI Protocol For Office Integration

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!