<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coding Clarity &#187; JAX-WS</title>
	<atom:link href="http://chrisdail.com/tag/jax-ws/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrisdail.com</link>
	<description>Writing simple, clear and readable code.</description>
	<lastBuildDate>Sun, 19 Jun 2011 15:54:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Getting access to SOAP Headers from an Apache CXF service implementation</title>
		<link>http://chrisdail.com/2008/11/17/getting-access-to-soap-headers-from-an-apache-cxf-service-implementation/</link>
		<comments>http://chrisdail.com/2008/11/17/getting-access-to-soap-headers-from-an-apache-cxf-service-implementation/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 13:42:33 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[CXF]]></category>
		<category><![CDATA[JAX-WS]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://chrisdail.com/2008/11/17/getting-access-to-soap-headers-from-an-apache-cxf-service-implementation/</guid>
		<description><![CDATA[For one project I was working on, I needed to get access to the SOAP headers of web service call. I am using the Apache CXF services stack. There were quite a few threads on how to get access to SOAP headers from an interceptor. In my case, I needed the contents of the header [...]]]></description>
			<content:encoded><![CDATA[<p>For one project I was working on, I needed to get access to the SOAP headers of web service call. I am using the Apache CXF services stack. There were quite a few threads on how to get access to SOAP headers from an interceptor. In my case, I needed the contents of the header inside the implementation.</p>
<p><a href="http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html">wsdl2java</a> does have the ability to create a java service class that provides access to the headers using the <i>-exsh</i> option. This was not an option for me since even with this flag on, the headers were not added to the service calls. I think it was how the WSDL I had was designed. This meant that I needed to do the work of pulling out the headers myself.</p>
<p>The SOAP headers can be retrieved from the JAX-WS SOAPMessageContext the easiest. Getting access to this was not trivial. I added the resource annotation to get access to the WebServiceContext. Unfortunately the MessageContext this gave me was not a SOAPMessageContext and provided no way to access the SOAP headers.</p>
<p>After looking through the code for the SoapMessage CXF class, I found how it gets the headers out of the Message. I came up with the following to access the headers from my implementation class:</p>
<pre class="brush: java; ">

@Resource
private WebServiceContext context;

private List&lt;Header&gt; getHeaders() {
    MessageContext messageContext = context.getMessageContext();
    if (messageContext == null || !(messageContext instanceof WrappedMessageContext)) {
        return null;
    }

    Message message = ((WrappedMessageContext) messageContext).getWrappedMessage();
    List&lt;Header&gt; headers = CastUtils.cast((List&lt;?&gt;) message.get(Header.HEADER_LIST));
    return headers;
}
</pre>
<p>This provides all of the headers available. To get the specific one I needed using JAXB I added the following to my code:</p>
<pre class="brush: java; ">

List&lt;Header&gt; headers = getHeaders();
if (headers != null) {
    for (Header h: headers) {
        Object o = h.getObject();

        // Unwrap the node using JAXB
        if (o instanceof Node) {
            o = getJaxb().createUnmarshaller().unmarshal((Node) o);
        }

        if (o instanceof DesiredHeaderType) {
            // Do whatever is required with the header object instance
        }
    }
}
</pre>
<p>This way of accessing the headers turns out to be much simpler than writing an interceptor and trying to stuff the results of that into the request.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisdail.com/2008/11/17/getting-access-to-soap-headers-from-an-apache-cxf-service-implementation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

