You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Faz <ar...@gmail.com> on 2013/10/31 12:29:28 UTC

HandleFault in CXF

Hi ALL,

I'm using CXf's fault out interceptor to intercept the exception from the
web services method. That works fine within the handleMessage method.. Now I
have two queries,

1. How do I had extra information to the Fault object apart from setting the
faultcode.?
2. Say if there is any error in the handleMessage method, the control goes
to handleFault method but in the handle fault method it simply unwinds and
logs something like - *GetMth has thrown exception, unwinding now:
java.lang.Exception:*. But the fault message(in xml format) is not being
sent to the client as Output mesasage, How do I make sure that the fault
message is sent as SOAPfault  to the clinet?
[in the hanldeMessage bleow, i have place *f=null *to introduce the fault ]

Handle message and handleFault methods:


         *public class SimpleSOAPFaultInterceptor extends
AbstractSoapInterceptor {
	private static Logger log = Logger.getLogger("org.apache");
	
	public SimpleSOAPFaultInterceptor() {
		super(Phase.MARSHAL);
	}
	
	public void handleMessage(SoapMessage message) throws Fault {
		// TODO Auto-generated method stub
		try {
		
			Fault f = (Fault) message.getContent(Exception.class);
			//f=null; /[Introduing an excpetion so that handleFault is called]/		
log.debug(" f ::::: "+ f.getCode());
			Throwable cause = f.getCause();
			if (cause instanceof MyServiceException) {
				f.setFaultCode(new QName("", String.valueOf("100000")));
			} else {
				log.warn("!!!!!!Unexpected Exception thrown ", cause);
			}
			} catch (SOAPException e) {
				// TODO Auto-generated catch block
				System.out.println("CAUGHT HERE "+e);
				e.printStackTrace();
			}  catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("CAUGHT HERE Exception "+e);
				throw new Fault(new Exception(
                        "Exception...."));
			}
	}
	
	
	
	@Override
	public void handleFault(SoapMessage message) {
		// TODO Auto-generated method stub
		super.handleFault(message);
		
		System.out.println("**handleFault the message ** "+message);
		Fault f = (Fault) message.getContent(Exception.class);
		System.out.println("**the f ** "+f);
	    //f.setFaultCode(new QName("", String.valueOf("8")));
		//SoapFault sf = new SoapFault("message",new QName("",
String.valueOf("99000")));
		//SoapFault.createFault(f, message.getVersion())
	    //			 .setMessage("INSIDE DIUDE");
		
	}*



--
View this message in context: http://cxf.547215.n5.nabble.com/HandleFault-in-CXF-tp5735813.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: HandleFault in CXF Interceptors - tried possible options

Posted by Atul <at...@gmail.com>.
Hi All,

I am facing a similar problem. 

JAXRSValidationInINterceptor is throwing an exception in PRE_INVOKE phase. I
want to catch this exception and return a custom json response to the
client. I have created a custom interceptor which catches the exception ,
takes out the message . I have one exception mapper registered. I am using
that but its not working. No json content is going to the client. Please
help. I want to send JSON response to the client.

 @Override
    public void handleMessage(Message aInMessage) throws Fault
    {
        Exception e = aInMessage.getContent(Exception.class);
        Fault f = (Fault)e;
        if(f.getCause() instanceof ConstraintViolationException){
            ConstraintViolationException exception =  
(ConstraintViolationException)f.getCause();
            String message =
exception.getConstraintViolations().iterator().next().getMessage();
            XRouterError error = new XRouterError("401",message);
            throw new OperationFailedException(error);
        }
    }





--
View this message in context: http://cxf.547215.n5.nabble.com/HandleFault-in-CXF-Interceptors-tried-possible-options-tp5735813p5754765.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: HandleFault in CXF

Posted by Daniel Kulp <dk...@apache.org>.
On Oct 31, 2013, at 12:29 PM, Faz <ar...@gmail.com> wrote:

> Hi ALL,
> 
> I'm using CXf's fault out interceptor to intercept the exception from the
> web services method. That works fine within the handleMessage method.. Now I
> have two queries,
> 
> 1. How do I had extra information to the Fault object apart from setting the
> faultcode.?

Most likely, what you would need/want to do is take the Exception/Fault out of the message, create a new SoapFault with the new information, and set that back in the message and let the rest of CXF’s chain do it’s thing:


public void handleMessage(SoapMessage message) throws Fault {
	Fault f = (Fault) message.getContent(Exception.class);
	SoapFault fault = SoapFault.createFault(f, message.getVersion());
        //modify the SoapFault or create a new one or something
        message.setContent(Exception.class, fault);
}

The default Soap[12|11]FaultOutInterceptor will then grab that SoapFault from the message and output it correctly.


> 2. Say if there is any error in the handleMessage method, the control goes
> to handleFault method but in the handle fault method it simply unwinds and
> logs something like - *GetMth has thrown exception, unwinding now:
> java.lang.Exception:*. But the fault message(in xml format) is not being
> sent to the client as Output mesasage, How do I make sure that the fault
> message is sent as SOAPfault  to the clinet?

Do not throw any exceptions from the Fault chain.   If there is a fault thrown from the fault chain, there is really no way for us to be able to know what can be done about, how to fix the situation, etc… The assumption is that if a fault is thrown from the fault chain, then something seriously has gone wrong and we cannot write anything out to the client.   The normal case that this occurs is if we cannot write to the OutputStream due to the network closing the connection or similar.   In that case, there is nothing we can do except log the issue and move on.

Dan


> [in the hanldeMessage bleow, i have place *f=null *to introduce the fault ]
> 
> Handle message and handleFault methods:
> 
> 
>         *public class SimpleSOAPFaultInterceptor extends
> AbstractSoapInterceptor {
> 	private static Logger log = Logger.getLogger("org.apache");
> 	
> 	public SimpleSOAPFaultInterceptor() {
> 		super(Phase.MARSHAL);
> 	}
> 	
> 	public void handleMessage(SoapMessage message) throws Fault {
> 		// TODO Auto-generated method stub
> 		try {
> 		
> 			Fault f = (Fault) message.getContent(Exception.class);
> 			//f=null; /[Introduing an excpetion so that handleFault is called]/		
> log.debug(" f ::::: "+ f.getCode());
> 			Throwable cause = f.getCause();
> 			if (cause instanceof MyServiceException) {
> 				f.setFaultCode(new QName("", String.valueOf("100000")));
> 			} else {
> 				log.warn("!!!!!!Unexpected Exception thrown ", cause);
> 			}
> 			} catch (SOAPException e) {
> 				// TODO Auto-generated catch block
> 				System.out.println("CAUGHT HERE "+e);
> 				e.printStackTrace();
> 			}  catch (Exception e) {
> 				// TODO Auto-generated catch block
> 				System.out.println("CAUGHT HERE Exception "+e);
> 				throw new Fault(new Exception(
>                        "Exception...."));
> 			}
> 	}
> 	
> 	
> 	
> 	@Override
> 	public void handleFault(SoapMessage message) {
> 		// TODO Auto-generated method stub
> 		super.handleFault(message);
> 		
> 		System.out.println("**handleFault the message ** "+message);
> 		Fault f = (Fault) message.getContent(Exception.class);
> 		System.out.println("**the f ** "+f);
> 	    //f.setFaultCode(new QName("", String.valueOf("8")));
> 		//SoapFault sf = new SoapFault("message",new QName("",
> String.valueOf("99000")));
> 		//SoapFault.createFault(f, message.getVersion())
> 	    //			 .setMessage("INSIDE DIUDE");
> 		
> 	}*
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/HandleFault-in-CXF-tp5735813.html
> Sent from the cxf-user mailing list archive at Nabble.com.

-- 
Daniel Kulp
dkulp@apache.org - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com


Re: HandleFault in CXF

Posted by Faz <ar...@gmail.com>.
Any help on this please?



--
View this message in context: http://cxf.547215.n5.nabble.com/HandleFault-in-CXF-Interceptors-tried-possible-options-tp5735813p5735945.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: HandleFault in CXF

Posted by Faz <ar...@gmail.com>.
I managed to write the xml content to the writer class with the below code,
but am unable to pass this message to the client. I mean even after writing
the message to the xmlwriter class and content being written fine to the
console log, its not been sent to the client. i.e, I dont see the outbound
message being sent nor printed when i turn on the monitors and the
interceptor outbound logs. Could anyone please help me with this?

                *MessageFactory factory = getFactory(message);
                SOAPMessage soapMessage = factory.createMessage();
                SOAPPart soapPart = soapMessage.getSOAPPart();
                SOAPBody soapBody = soapPart.getEnvelope().getBody();  
                SOAPFault soapFault = soapBody.addFault();  
                soapFault.setFaultString("Strange");
    	   W3CDOMStreamWriter writer1 = new SAAJStreamWriter(soapPart);
                message.setContent(XMLStreamWriter.class, writer1);
                message.setContent(SOAPMessage.class, soapMessage);
                message.setContent(Node.class, soapMessage.getSOAPPart());
               
message.getInterceptorChain().add(SAAJOutEndingInterceptor.INSTANCE);*

LOGS:
**saaj the factory **
com.sun.xml.messaging.saaj.soap.ver1_2.SOAPMessageFactory1_2Impl@146ee9c
**saaj the soapMessage **
com.sun.xml.messaging.saaj.soap.ver1_2.Message1_2Impl@1db52c8
**saaj the soapPart **
com.sun.xml.messaging.saaj.soap.ver1_2.SOAPPart1_2Impl@17d6c1
**saaj the soapsoapBodyPart ** [env:Body: null]
**saaj the soapFault ** [env:Fault: null]
**saaj the setFaultString ** Strange
**saaj the messg ** {org.apache.cxf.message.Message.RESPONSE_CODE=200,
HTTP.RESPONSE=org.apache.catalina.connector.ResponseFacade@2982bf,
org.apache.cxf.headers.Header.list=[], wrote.envelope.start=true,
org.apache.cxf.message.Message.ENCODING=UTF-8,
org.apache.cxf.message.FaultMode=CHECKED_APPLICATION_FAULT,
org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap12@b16f5f,
Content-Type=application/soap+xml,
org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor.original.xml.writer=[StreamWriter:
class com.ctc.wstx.sw.SimpleNsStreamWriter, underlying outputter:
com.ctc.wstx.sw.BufferingXmlWriter@1144823],
org.apache.cxf.interceptor.LoggingOutInterceptor.log-setup=true,
org.apache.cxf.service.model.BindingFaultInfo=org.apache.cxf.service.model.BindingFaultInfo@106def2}
**saaj the writer1 ** <env:Envelope
xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><env:Fault><env:Code><env:Value>env:Receiver</env:Value></env:Code><env:Reason><env:Text
xml:lang="en-US">Strange</env:Text></env:Reason></env:Fault></env:Body></env:Envelope>

Please advise.



--
View this message in context: http://cxf.547215.n5.nabble.com/HandleFault-in-CXF-tp5735813p5735868.html
Sent from the cxf-user mailing list archive at Nabble.com.