package org.jboss.axis;
import org.jboss.axis.encoding.SerializationContext;
import org.jboss.axis.encoding.Serializer;
import org.jboss.axis.message.SOAPEnvelopeAxisImpl;
import org.jboss.axis.message.SOAPFaultImpl;
import org.jboss.axis.message.SOAPHeaderElementAxisImpl;
import org.jboss.axis.soap.SOAPConstants;
import org.jboss.axis.utils.JavaUtils;
import org.jboss.axis.utils.XMLUtils;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.JAXRPCException;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
public class AxisFault extends java.rmi.RemoteException
{
private static Logger log = Logger.getLogger(AxisFault.class.getName());
protected QName faultCode;
protected Vector faultSubCode;
protected String faultString = "";
protected String faultActor;
protected Vector faultDetails; protected String faultNode;
protected ArrayList faultHeaders;
public static AxisFault makeFault(Exception e)
{
if (e instanceof InvocationTargetException)
{
InvocationTargetException ite = (InvocationTargetException)e;
Throwable t = ite.getTargetException();
if (t instanceof Exception)
e = (Exception)t;
}
if (e instanceof JAXRPCException)
{
JAXRPCException rte = (JAXRPCException)e;
Throwable t = rte.getLinkedCause();
if (t instanceof Exception)
e = (Exception)t;
}
if (e instanceof AxisFault)
{
return (AxisFault)e;
}
return new AxisFault(e);
}
public AxisFault(String code, String faultString, String actor, Element[] details)
{
this(new QName(Constants.NS_URI_AXIS, code),
faultString, actor, details);
}
public AxisFault(QName code, String faultString, String actor, Element[] details)
{
super(faultString);
setFaultCode(code);
setFaultString(faultString);
setFaultActor(actor);
setFaultDetail(details);
}
public AxisFault(QName code, QName[] subcodes, String faultString,
String actor, String node, Element[] details)
{
super(faultString);
setFaultCode(code);
if (subcodes != null)
{
for (int i = 0; i < subcodes.length; i++)
{
addFaultSubCode(subcodes[i]);
}
}
setFaultString(faultString);
setFaultActor(actor);
setFaultNode(node);
setFaultDetail(details);
}
protected AxisFault(Exception target)
{
super(target.getMessage(), assertTarget(target));
setFaultCodeAsString(Constants.FAULT_SERVER_USER);
initFromException(target);
}
private static Throwable assertTarget(Throwable target)
{
return target;
}
public AxisFault(String message)
{
super(message);
setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL);
setFaultString(message);
}
public AxisFault()
{
super();
setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL);
}
public AxisFault(String message, Throwable target)
{
super(message, assertTarget(target));
setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL);
setFaultString(getMessage());
}
private void initFromException(Exception target)
{
assertTarget(target);
String faultString = target.getMessage();
setFaultString(faultString != null ? faultString : target.toString());
Throwable cause = target;
if (target instanceof AxisFault)
cause = ((AxisFault)target).detail;
if (cause != null)
{
addFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE, JavaUtils.stackToString(cause));
}
}
private void initFaultDetails()
{
if (faultDetails == null)
{
faultDetails = new Vector();
}
}
public void clearFaultDetails()
{
faultDetails = null;
}
public void dump()
{
log.debug(dumpToString());
}
public String dumpToString()
{
String details = new String();
if (faultDetails != null)
{
for (int i = 0; i < faultDetails.size(); i++)
{
Element e = (Element)faultDetails.get(i);
String namespace = e.getNamespaceURI();
if (namespace == null)
{
namespace = "";
}
String partname = e.getLocalName();
if (partname == null)
{
partname = e.getNodeName();
}
details += JavaUtils.LS
+ "\t{" + namespace + "}"
+ partname + ": "
+ XMLUtils.getInnerXMLString(e);
}
}
String subCodes = new String();
if (faultSubCode != null)
{
for (int i = 0; i < faultSubCode.size(); i++)
{
subCodes += JavaUtils.LS
+ (QName)faultSubCode.elementAt(i);
}
}
String code = XMLUtils.xmlEncodeString(faultCode.toString());
String errorString = XMLUtils.xmlEncodeString(faultString);
String actor = XMLUtils.xmlEncodeString(faultActor);
String node = XMLUtils.xmlEncodeString(faultNode);
return "AxisFault" + JavaUtils.LS
+ " faultCode: " + code + JavaUtils.LS
+ " faultSubcode: " + subCodes + JavaUtils.LS
+ " faultString: " + errorString + JavaUtils.LS
+ " faultActor: " + actor + JavaUtils.LS
+ " faultNode: " + node + JavaUtils.LS
+ " faultDetail: " + details + JavaUtils.LS
;
}
public void setFaultCode(QName code)
{
faultCode = code;
}
public void setFaultCode(String code)
{
setFaultCodeAsString(code);
}
public void setFaultCodeAsString(String code)
{
SOAPConstants soapConstants = MessageContext.getCurrentContext() == null ?
SOAPConstants.SOAP11_CONSTANTS :
MessageContext.getCurrentContext().getSOAPConstants();
faultCode = new QName(soapConstants.getEnvelopeURI(), code);
}
public QName getFaultCode()
{
return (faultCode);
}
public void addFaultSubCodeAsString(String code)
{
initFaultSubCodes();
faultSubCode.add(new QName(Constants.NS_URI_AXIS, code));
}
protected void initFaultSubCodes()
{
if (faultSubCode == null)
{
faultSubCode = new Vector();
}
}
public void addFaultSubCode(QName code)
{
initFaultSubCodes();
faultSubCode.add(code);
}
public void clearFaultSubCodes()
{
faultSubCode = null;
}
public QName[] getFaultSubCodes()
{
if (faultSubCode == null)
{
return null;
}
QName[] q = new QName[faultSubCode.size()];
return (QName[])faultSubCode.toArray(q);
}
public void setFaultString(String str)
{
if (str != null)
{
faultString = str;
}
else
{
faultString = "";
}
}
public String getFaultString()
{
return (faultString);
}
public void setFaultReason(String str)
{
setFaultString(str);
}
public String getFaultReason()
{
return getFaultString();
}
public void setFaultActor(String actor)
{
faultActor = actor;
}
public String getFaultActor()
{
return (faultActor);
}
public String getFaultRole()
{
return getFaultActor();
}
public void setFaultRole(String role)
{
setFaultActor(role);
}
public String getFaultNode()
{
return (faultNode);
}
public void setFaultNode(String node)
{
faultNode = node;
}
public void setFaultDetail(Element[] details)
{
if (details == null)
{
faultDetails = null;
return;
}
faultDetails = new Vector(details.length);
for (int loop = 0; loop < details.length; loop++)
{
faultDetails.add(details[loop]);
}
}
public void setFaultDetailString(String details)
{
clearFaultDetails();
addFaultDetailString(details);
}
public void addFaultDetailString(String detail)
{
initFaultDetails();
try
{
Document doc = XMLUtils.newDocument();
Element element = doc.createElement("string");
Text text = doc.createTextNode(detail);
element.appendChild(text);
faultDetails.add(element);
}
catch (ParserConfigurationException e)
{
throw new InternalException(e);
}
}
public void addFaultDetail(Element detail)
{
initFaultDetails();
faultDetails.add(detail);
}
public void addFaultDetail(QName qname, String body)
{
Element detail = XMLUtils.StringToElement(qname.getNamespaceURI(),
qname.getLocalPart(),
body);
addFaultDetail(detail);
}
public Element[] getFaultDetails()
{
if (faultDetails == null)
{
return null;
}
Element result[] = new Element[faultDetails.size()];
for (int i = 0; i < result.length; i++)
result[i] = (Element)faultDetails.elementAt(i);
return result;
}
public Element lookupFaultDetail(QName qname)
{
if (faultDetails != null)
{
String searchNamespace = qname.getNamespaceURI();
String searchLocalpart = qname.getLocalPart();
Iterator it = faultDetails.iterator();
while (it.hasNext())
{
Element e = (Element)it.next();
String localpart = e.getLocalName();
if (localpart == null)
{
localpart = e.getNodeName();
}
String namespace = e.getNamespaceURI();
if (namespace == null)
{
namespace = "";
}
if (searchNamespace.equals(namespace)
&& searchLocalpart.equals(localpart))
{
return e;
}
}
}
return null;
}
public boolean removeFaultDetail(QName qname)
{
Element elt = lookupFaultDetail(qname);
if (elt == null)
{
return false;
}
else
{
return faultDetails.remove(elt);
}
}
public void output(SerializationContext context) throws Exception
{
SOAPConstants soapConstants = Constants.DEFAULT_SOAP_VERSION;
if (context.getMessageContext() != null)
soapConstants = context.getMessageContext().getSOAPConstants();
SOAPEnvelopeAxisImpl envelope = new SOAPEnvelopeAxisImpl(soapConstants);
SOAPFaultImpl fault = new SOAPFaultImpl(this);
envelope.addBodyElement(fault);
if (faultHeaders != null)
{
for (Iterator i = faultHeaders.iterator(); i.hasNext();)
{
SOAPHeaderElementAxisImpl header = (SOAPHeaderElementAxisImpl)i.next();
envelope.addHeader(header);
}
}
envelope.output(context);
}
public String toString()
{
return getClass().getName() + ": " + faultString;
}
public void printStackTrace(PrintStream ps)
{
ps.println(dumpToString());
super.printStackTrace(ps);
}
public void printStackTrace(java.io.PrintWriter pw)
{
pw.println(dumpToString());
super.printStackTrace(pw);
}
public void addHeader(SOAPHeaderElementAxisImpl header)
{
if (faultHeaders == null)
{
faultHeaders = new ArrayList();
}
faultHeaders.add(header);
}
public ArrayList getHeaders()
{
return faultHeaders;
}
public void clearHeaders()
{
faultHeaders = null;
}
public void writeDetails(QName qname, SerializationContext context) throws java.io.IOException
{
Object detailObject = this.detail;
if (detailObject == null)
return;
Serializer ser = context.getSerializerForJavaType(detailObject.getClass());
if (ser != null)
{
boolean oldMR = context.getDoMultiRefs();
context.setDoMultiRefs(false);
context.serialize(qname, null, detailObject);
context.setDoMultiRefs(oldMR);
}
}
}