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;
}
}
Aggregations