You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by An...@nokia.com on 2002/09/19 13:44:18 UTC

FW: add header to a thorwn fault

Hi,

Will Axis supports the SOAP headers addition to Fault ? Like in case of FaultDetails ?

Br,
Andras

andras.avar@nokia.com

-----Original Message-----
From: ext David Owens [mailto:dowens@synxis.com]
Sent: 18 September, 2002 16:28
To: Avar Andras (NMP/Budapest)
Subject: Re: add header to a thorwn fault


Hi there!

With the last release of Axis, that functionality got stomped on.
So we ended up basically doing the same thing in the invoke method.

So, we wrote a custom handler which looks for our custom exception
and puts stuff in the header so that the data is transfered.

Attached is the class.

We had to make some code changes to the axis code as documented in the
previous message.

Let me know if this helps.

|)ave


On Wed, 2002-09-18 at 06:28, Andras.Avar@nokia.com wrote:
> Hi David,
> 
> I have an important question according to the attached letter, can you add some header to a Thrown exception in onFault() function ? Or do you have any other solution for that ?
> 
> Thanks FW,
> Andras
> 
> > -----Original Message-----
> > From: David Owens [mailto:dowens@synxis.com]
> > Sent: Thursday, June 13, 2002 11:48 AM
> > To: axis-user@xml.apache.org
> > Subject: Re: Custom Exceptions
> > 
> > 
> > Sam/Sylvain
> > 
> > Bob is on vacation, but I worked with him on this stuff, so I
> > thought I'd post it out there for you so you could take a look
> > at it....
> > 
> > We got our custom exception to work.  We had to make one hack to
> > the axis code base to make it possible, and did the rest with a handler.
> > 
> > We have an exception called SrmsException which extends exception.
> > It has one data variable, and int called "code".  This exception
> > is thrown from the server.  From that, we code-generate a client
> > version which extends AxisFault, this is what we catch on the
> > client.  To get the data (code) into the exception we had to do
> > the following:
> > 
> > We hacked org.apache.axis.providers.java.JavaProvider.java  to
> > put the data we need into the messageContext.  The JavaProvider
> > catches the server exceptions (search for catch block in the file
> > near processMessage()) and then rethrows it.  We changed it to
> > catch it, put info in the context, and then throw it.  The code
> > looks like this:
> > 
> >             try {
> >                 processMessage(msgContext, clsName, allowedMethods,
> > reqEnv,
> >                                resEnv, jc, obj);
> >             } catch (Exception exp) {
> >                 msgContext.setProperty("ThrownException", exp);
> >                 throw exp;
> >             } finally {
> > 
> > 
> > That was the only change we had to make to axis.
> > 
> > Then we added a custome handler which sits in the chain on the request
> > to the server, in the chain for the response from the server to client.
> > (We did this by creating and adding the client-config.wsdd which
> > introduces our handler in the globalConfiguration portion.)
> > 
> > Here is the code for our handler:
> > 
> > package com.synxis.srms.webservices.handlers (Sorry about the
> > formatting):
> > 
> > import org.apache.axis.AxisFault;
> > import org.apache.axis.Message;
> > import org.apache.axis.MessageContext;
> > import org.apache.axis.Constants;
> > import org.apache.axis.message.SOAPEnvelope;
> > import org.apache.axis.message.SOAPHeaderElement;
> > import com.synxis.srms.webservices.client.exception.SrmsException;
> > 
> > 
> > import org.apache.commons.logging.Log;
> > import org.apache.commons.logging.LogFactory;
> > 
> > public class SrmsExceptionDataHandler extends
> > org.apache.axis.handlers.BasicHandler {
> > 
> >     public void invoke(MessageContext msgContext) throws AxisFault {
> > 
> >         if (msgContext.isClient()) {
> >            if (msgContext.getPastPivot()) {
> >                //System.out.println( "INVOKE CALLED" );
> >                // This is a response.  Check it for the session header.
> >                Message msg = msgContext.getResponseMessage();
> >                if (msg == null)
> >                    return;
> >                SOAPEnvelope env = null;
> >                try {
> >                    env = msg.getSOAPEnvelope();
> >                }
> >                catch (Exception e)
> >                {
> >                    System.out.println("Caught from getSOAPEnvelope,
> > trying again");
> >                    env = msg.getSOAPEnvelope();
> >                }
> >                SOAPHeaderElement header =
> > env.getHeaderByName("http://xml.apache.org/axis/exception",
> >                                                              
> > "exceptionCode");
> >                if (header == null)
> >                    return;
> > 
> >                env.clearBody();
> >                Integer code = null;
> >                // Got one!
> >                try {
> >                    code = (Integer)header.
> >                                 getValueAsType(Constants.XSD_INT);
> >                } catch (Exception e) {
> >                    throw AxisFault.makeFault(e);
> >                }
> > 
> >                if(code != null)
> >                {
> >                    throw new SrmsException(code.intValue());
> >                }
> >            }
> >         }
> > 
> >     }
> > 
> >     public void onFault(MessageContext msgContext)
> >     {      
> >        try
> >        {
> >            Exception e = (Exception)
> > msgContext.getProperty("ThrownException");
> > 
> >            if (e != null && e instanceof
> > java.lang.reflect.InvocationTargetException)
> >            {
> >                Throwable re =
> > ((java.lang.reflect.InvocationTargetException)e).getTargetException();
> >                if (re instanceof
> > com.synxis.srms.exception.SrmsException)
> >                {
> >                    com.synxis.srms.exception.SrmsException ex =
> > (com.synxis.srms.exception.SrmsException)re;
> >                    //System.out.println("Encoding exception code in to
> > header " + ex.getCode());
> >                    Message msg = msgContext.getRequestMessage();
> >                    if (msg == null)
> >                        return;
> > 
> >                    SOAPEnvelope env = msg.getSOAPEnvelope();
> >                    SOAPHeaderElement header = new
> > SOAPHeaderElement("http://xml.apache.org/axis/exception",
> >                                                                    
> > "exceptionCode",
> >                                                                     new
> > Integer(ex.getCode()));
> >                    env.addHeader(header);
> >                }
> >            }
> >        }
> >        catch(Exception e)
> >        {
> >            System.out.println("Got exception in OnFault in Handler");
> >            e.printStackTrace();
> >        }
> >     }
> > };
> > 
> > 
> > So, on the request, if there is an fault, onFault() is called.  If
> > the exception is one of our SrmsExceptions we get the code
> > (ex.getCode()) and stick it in the header (env.addHeader(header)).
> > 
> > Then, on the response, to the client, we check for an envelope
> > and a header.  If one exists we look for our header element and
> > get the data (code) from it.  We then create a new SrmsException
> > with the code as the value and throw that to be caught by the
> > client.
> > 
> > The only strange thing we had to do (besides the rest of it. ;])
> > was to clear the envelope body so that the original fault is
> > not maintained and sent back to the client.
> > 
> > I hope this isn't too confusing, and I hope it helps.  As you can
> > tell, we haven't cleaned it all up yet, but it works.  
> > 
> > |)ave
> > 
> > dowens@synxis.com