cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrei Shakirin <ashaki...@talend.com>
Subject RE: Custom fault response (JAX-WS)
Date Tue, 17 Dec 2013 08:37:05 GMT
Hi,

I see two possible cases here:

a) If you would like to keep the standard SOAP Fault and just customize the faultcode, faultstring,
details, the following interceptor can be configured in out fault chain:
 
public class CustomSoapFaultInterceptor extends AbstractPhaseInterceptor<SoapMessage>
{
	
	public CustomSoapFaultInterceptor() {
		super(Phase.WRITE);
	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		Exception e = message.getContent(Exception.class);
		if (e instanceof Fault) {
			Fault fault = (Fault) e;
			fault.setMessage("This is a critical fault");
			fault.setFaultCode(new QName("ns", "CriticalFault"));
			try {
				DocumentBuilder builder = getBuilder();
				Document doc = builder.parse(new ByteArrayInputStream("<detail><critical>critical
details</critical></detail>".getBytes()));
				fault.setDetail(doc.getDocumentElement());
			} catch (Exception e1) {
				System.out.println("Cannot build detail element: " + e.getMessage());
			}
		}
	}
...
}

b) If you would like to replace SOAP fault with normal SOAP message, you likely should replace
the fault with SOAPMessage in message content: http://stackoverflow.com/questions/8066474/how-to-transform-soapfault-to-soapmessage-via-interceptor-in-cxf


Regards,
Andrei.

> -----Original Message-----
> From: Przemyslaw Bielicki [mailto:pbielicki@gmail.com]
> Sent: Donnerstag, 12. Dezember 2013 16:50
> To: dev@cxf.apache.org
> Subject: Re: Custom fault response (JAX-WS)
> 
> OK I found a workaround that works but it's very obscure:
> 
> @Override
> public void handleFault(SoapMessage message) {
>   ...
>   Marshaller marshaller =
> JAXBContext.newInstance(MessageHeader.class).createMarshaller();
>   SOAPMessage soap = message.getContent(SOAPMessage.class);
>   soap.getSOAPHeader().removeContents();
>   marshaller.marshal(msgHeader, soap.getSOAPHeader());
>   HttpServletResponse response = (HttpServletResponse)
> message.get(AbstractHTTPDestination.HTTP_RESPONSE);
>   message.setContent(OutputStream.class, response.getOutputStream());
>   Exception e = message.getContent(Exception.class);
>   soap.getSOAPBody().removeContents();
>   marshaller.marshal(OneAXmlUtil.buildErrorList(msgHeader, e),
> soap.getSOAPBody());
>   soap.writeTo(response.getOutputStream());
>   response.getOutputStream().close();
> 
> Is there any "nice" way to do this?
> 
> Cheers,
> Przemyslaw
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Custom-
> fault-response-JAX-WS-tp5737771p5737775.html
> Sent from the cxf-dev mailing list archive at Nabble.com.

Mime
View raw message