<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Spring-Framework on despatches</title><link>https://icle.es/tags/spring-framework/</link><description>Recent content in Spring-Framework on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Fri, 20 Jun 2025 09:25:00 +0100</lastBuildDate><atom:link href="https://icle.es/tags/spring-framework/index.xml" rel="self" type="application/rss+xml"/><item><title>Using CXF Interceptors to do some magic around your web service calls [1105]</title><link>https://icle.es/2011/11/06/using-cxf-interceptors-to-do-some-magic-around-your-web-service-calls-1105/</link><pubDate>Sun, 06 Nov 2011 12:24:28 +0000</pubDate><guid>https://icle.es/2011/11/06/using-cxf-interceptors-to-do-some-magic-around-your-web-service-calls-1105/</guid><description>&lt;p>We use JBossWS CXF for a heavily utilised enterprise system. It links into
spring to pick up and execute beans. We have a bunch of exceptions that could
get thrown.&lt;/p>
&lt;p>To simplify it, the code was originally written to create an anonymous class a
la Runnable which is wrapped around a try catch block. The exceptions that are
thrown are then converted to a soap fault and passed back.&lt;/p>
```java
private SOAPFaultException convertToSoapException(ApplicationException e)
{
 try {
 if(null == soapFactory) {
 soapFactory = SOAPFactory.newInstance();
 }
 SOAPFault sf = soapFactory.createFault();
 sf.setFaultString( e.getMessage() );
 sf.setFaultCode( Integer.toString(e.getErrorCode()) );
 return new SOAPFaultException( sf );
 } catch(SOAPException soapException) {
 throw new RuntimeException( soapException );
 }
}
```
&lt;p>Nothing inherently wrong with this. However, there are a couple of issues with
this in that each soap method is set to throw an &lt;em>ApplicationException&lt;/em> and
there is not further documentation of which of the subclasses are actually
relevant to that method.&lt;/p></description><content:encoded><![CDATA[<p>We use JBossWS CXF for a heavily utilised enterprise system. It links into
spring to pick up and execute beans. We have a bunch of exceptions that could
get thrown.</p>
<p>To simplify it, the code was originally written to create an anonymous class a
la Runnable which is wrapped around a try catch block. The exceptions that are
thrown are then converted to a soap fault and passed back.</p>
```java
private SOAPFaultException convertToSoapException(ApplicationException e)
{
    try {
        if(null == soapFactory) {
            soapFactory = SOAPFactory.newInstance();
        }
        SOAPFault sf = soapFactory.createFault();
        sf.setFaultString( e.getMessage() );
        sf.setFaultCode( Integer.toString(e.getErrorCode()) );
        return new SOAPFaultException( sf );
    } catch(SOAPException soapException) {
        throw new RuntimeException( soapException );
    }
}
```
<p>Nothing inherently wrong with this. However, there are a couple of issues with
this in that each soap method is set to throw an <em>ApplicationException</em> and
there is not further documentation of which of the subclasses are actually
relevant to that method.</p>
<p>In a runtime environment, this is not hugely relevant. However, when generating
documentation from the WSDL&rsquo;s, it is.</p>
<p>To resolve this, we changed each method to throw their relevant exception, and
wrote an interceptor to pick up the exception and convert it&hellip;</p>
<p>The first step was to write an interceptor which is surprisingly simple
and straightforward.</p>
```java
public class ExampleSoapFaultInterceptor extends AbstractSoapInterceptor {

    Logger log = Logger.getLogger(getClass());

    public QuarkSoapFaultInterceptor() {
        super(Phase.MARSHAL);
    }

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        Fault f = (Fault) message.getContent(Exception.class);

        Throwable cause = f.getCause();
        if (cause instanceof ApplicationException) {
            log.info("Exception Thrown", cause);
            QuarkException e = (QuarkException) cause;
            f.setFaultCode(new QName("", String.valueOf(e.getErrorCode())));

        } else {
            log.warn("Unexpected Exception thrown ", cause);
        }

    }
}
```
<p>The class doesn&rsquo;t need to extend the AbstractSoapInterceptor but you would then
have to manually implement a number of mechanisms that the abstract class
provides.</p>
<p>The constructor simply defines where this interceptor should be inserted into as
part of the chain. There is a whole bunch of different places where it can be
inserted based on this. Information can be
<a href="http://cxf.apache.org/docs/interceptors.html" title="Apache CXF Interceptors">found in their documentation.</a></p>
<p>To insert this interceptor into a web service, the service needs to be annotated
as follows:</p>
```java
@WebService(endpointInterface = "uk.co.kraya.example.WebService")
@OutFaultInterceptors(interceptors= {"uk.co.kraya.example.interceptors.ExampleSoapFaultInterceptor"})
public class ExampleWebService implements ExampleAPI {
```
<p>Each exception thrown from the web service will now be intercepted. You can then
include any further further information in the soap fault.</p>
<p>In this particular case, the only thing that gets done is to update the fault
code with the error code set against the exception. Any additional information
can be set against the soap fault at this point. You can also log as we are
doing.</p>
]]></content:encoded></item></channel></rss>