Search in sources :

Example 1 with StepException

use of com.twinsoft.convertigo.beans.steps.StepException in project convertigo by convertigo.

the class SOAPUtils method writeSoapFault.

public static String writeSoapFault(Throwable e, String encoding) throws IOException {
    String faultString = null;
    StepException stepException = null;
    if (e instanceof SOAPException) {
        Throwable cause = ((SOAPException) e).getCause();
        Engine.logEngine.error("A SOAP error has occured while processing the web service request.", e);
        Engine.logEngine.error("Cause:", cause);
        faultString = "A SOAP error has occured while processing the web service request: " + cause.getMessage();
    } else {
        if (System.getProperty("java.specification.version").compareTo("1.4") >= 0) {
            Throwable eCause = e;
            while ((eCause = eCause.getCause()) != null) {
                if (eCause instanceof StepException) {
                    stepException = (StepException) eCause;
                    break;
                }
            }
        }
        if (stepException == null) {
            Engine.logEngine.error("Unable to analyze or execute the web service.", e);
            faultString = "Unable to analyze or execute the web service.";
        }
    }
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage faultMessage = messageFactory.createMessage();
        SOAPPart sp = faultMessage.getSOAPPart();
        SOAPEnvelope se = sp.getEnvelope();
        SOAPBody sb = se.getBody();
        SOAPFault fault = sb.addFault();
        fault.setFaultString(stepException == null ? faultString : stepException.getErrorMessage());
        fault.setFaultCode("soapenv:Server");
        SOAPFactory soapFactory = defaultSOAPFactory.get();
        Name name;
        DetailEntry detailEntry;
        if (stepException == null) {
            Detail detail = fault.addDetail();
            String faultDetail = e.getMessage();
            if (faultDetail == null)
                faultDetail = "";
            name = soapFactory.createName(e.getClass().getName());
            detailEntry = detail.addDetailEntry(name);
            detailEntry.addTextNode(faultDetail == null ? "(no more information)" : faultDetail);
            if (System.getProperty("java.specification.version").compareTo("1.4") >= 0) {
                Throwable eCause = e;
                while ((eCause = eCause.getCause()) != null) {
                    faultDetail = eCause.getMessage();
                    name = soapFactory.createName(eCause.getClass().getName());
                    detailEntry = detail.addDetailEntry(name);
                    detailEntry.addTextNode(faultDetail == null ? "(no more information)" : faultDetail);
                }
            }
            name = soapFactory.createName("moreinfo");
            detailEntry = detail.addDetailEntry(name);
            detailEntry.addTextNode("See the Convertigo engine log files for more details...");
        } else {
            // Details property of ExceptionStep is not empty
            String details = stepException.getErrorDetails();
            if (!(("").equals(details) || details.startsWith("org.mozilla.javascript.Undefined"))) {
                Detail detail = fault.addDetail();
                // If step's exception detail is an XML document, insert it in the detail SOAP part
                try {
                    InputStream inputStream = (InputStream) new ByteArrayInputStream(details.getBytes("UTF-8"));
                    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                    documentBuilderFactory.setNamespaceAware(true);
                    documentBuilderFactory.setValidating(true);
                    DocumentBuilder docbuilder = documentBuilderFactory.newDocumentBuilder();
                    Document domDetails = docbuilder.parse(inputStream);
                    addDetails(detail, domDetails.getDocumentElement());
                } catch (Exception ee) {
                    // Probably not an XML DOM, insert as CDATA
                    name = soapFactory.createName("content");
                    detailEntry = detail.addDetailEntry(name);
                    detailEntry.addTextNode(details);
                }
            }
        }
        faultMessage.saveChanges();
        String sResponseMessage = "";
        sResponseMessage = SOAPUtils.toString(faultMessage, encoding);
        if (Engine.logEngine.isDebugEnabled()) {
            Engine.logEngine.debug("SOAP response:\n" + sResponseMessage);
        }
        return sResponseMessage;
    } catch (Throwable ee) {
        Engine.logEngine.error("Unable to send the SOAP FAULT message.", ee);
        String response = "";
        response += Log.getStackTrace(ee);
        response += "SOAP error: " + faultString + "\n";
        response += "Initial exception:\n";
        response += Log.getStackTrace(e);
        if (e instanceof SOAPException) {
            Throwable cause = ((SOAPException) e).getCause();
            response += "SOAP Cause:\n";
            response += Log.getStackTrace(cause);
        }
        return response;
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) MessageFactory(javax.xml.soap.MessageFactory) DetailEntry(javax.xml.soap.DetailEntry) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPFactory(javax.xml.soap.SOAPFactory) TransformerException(javax.xml.transform.TransformerException) SOAPException(javax.xml.soap.SOAPException) StepException(com.twinsoft.convertigo.beans.steps.StepException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Name(javax.xml.soap.Name) QName(javax.xml.namespace.QName) SOAPBody(javax.xml.soap.SOAPBody) StepException(com.twinsoft.convertigo.beans.steps.StepException) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) SOAPFault(javax.xml.soap.SOAPFault) Detail(javax.xml.soap.Detail)

Aggregations

StepException (com.twinsoft.convertigo.beans.steps.StepException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 QName (javax.xml.namespace.QName)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Detail (javax.xml.soap.Detail)1 DetailEntry (javax.xml.soap.DetailEntry)1 MessageFactory (javax.xml.soap.MessageFactory)1 Name (javax.xml.soap.Name)1 SOAPBody (javax.xml.soap.SOAPBody)1 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)1 SOAPException (javax.xml.soap.SOAPException)1 SOAPFactory (javax.xml.soap.SOAPFactory)1 SOAPFault (javax.xml.soap.SOAPFault)1 SOAPMessage (javax.xml.soap.SOAPMessage)1 SOAPPart (javax.xml.soap.SOAPPart)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1