{"id":4799,"date":"2017-04-27T18:25:31","date_gmt":"2017-04-27T18:25:31","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4799"},"modified":"2017-04-27T18:25:31","modified_gmt":"2017-04-27T18:25:31","slug":"interacting-with-soap-headers-using-cxf","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/interacting-with-soap-headers-using-cxf\/","title":{"rendered":"Interacting with SOAP headers using CXF"},"content":{"rendered":"<p>Sometimes you might want to interact with data being passed over SOAP within the SOAP headers, for example this is a technique used to pass security tokens or user information etc. <\/p>\n<p>CXF comes with quite a few &#8220;insertion points&#8221; whereby we can insert our code into the workflow of WSDL creation, SOAP calls etc. Here we&#8217;ll just look at the specifics of intercepting the SOAP call and extracting the header (ofcourse the reverse can also be implemented, whereby we intercept an outward bound call and insert a SOAP header item, but that&#8217;s for the reader to investigate).<\/p>\n<p><em>I&#8217;m only going to cover implementing this in code, but obviously this can also be setup via Spring configuration also.<\/em><\/p>\n<p>To add an interceptor to our JaxWsServerFactoryBean, we do the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nJaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();\r\n\/\/ set up the bean, address etc.\r\n\r\norg.apache.cxf.endpoint.Server server = factory.create();\r\nserver.getEndpoint().getInInterceptors().add(new SoapInterceptor());\r\n<\/pre>\n<p>Now let&#8217;s look at the SoapInterceptor<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SoapInterceptor extends AbstractSoapInterceptor {\r\n    public static final String SECURITY_TOKEN_ELEMENT = &quot;securityToken&quot;;\r\n\r\n    public SoapInterceptor() {\r\n        super(Phase.PRE_PROTOCOL);\r\n        addBefore(WSDLGetInterceptor.class.getName());\r\n    }\r\n    \r\n    @Override\r\n    public void handleMessage(SoapMessage message) throws Fault {\r\n        String securityToken = getTokenFromHeader(message);\r\n        \/\/ do something with the token, maybe save in a context\r\n    }\r\n\r\n    private String getTokenFromHeader(SoapMessage message) {\r\n        String securityToken = null;\r\n        try {\r\n            List&lt;Header&gt; list = message.getHeaders();\r\n            for(Header h : list) {\r\n                if(h.getName().getLocalPart() == SECURITY_TOKEN_ELEMENT) {\r\n                    Element token = (Element)h.getObject();\r\n                    if(token != null) {\r\n                        securityToken = token.getTextContent().toString();\r\n                        break;\r\n                    }\r\n                }\r\n            }\r\n        } catch (RuntimeException e) {\r\n            throw new JAXRPCException(&quot;Invalid User&quot;, e);\r\n        } catch (Exception e) {\r\n            throw new JAXRPCException(&quot;Security Token failure &quot;, e);\r\n        }\r\n        return securityToken;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you might want to interact with data being passed over SOAP within the SOAP headers, for example this is a technique used to pass security tokens or user information etc. CXF comes with quite a few &#8220;insertion points&#8221; whereby we can insert our code into the workflow of WSDL creation, SOAP calls etc. Here [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[163,161],"tags":[],"class_list":["post-4799","post","type-post","status-publish","format-standard","hentry","category-cxf","category-java"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4799"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4799\/revisions"}],"predecessor-version":[{"id":4838,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4799\/revisions\/4838"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}