protected Exception translateException(Method method, Exception t) {
if (!(t instanceof XFireFault)) {
LOG.debug("Exception is not an XFireFault");
return t;
}
XFireFault xfireFault = (XFireFault) t;
if (!xfireFault.hasDetails()) {
LOG.debug("XFireFault has no details");
return t;
}
// Get first child element of <detail/>
List details = xfireFault.getDetail().getContent();
Element detail = null;
for (Object o : details) {
if (o instanceof Element) {
detail = (Element) o;
break;
}
}
if (detail == null) {
LOG.debug("XFireFault has no element in <detail/>");
return t;
}
QName qname = new QName(detail.getNamespaceURI(),
detail.getName());
Class<?>[] exceptions = method.getExceptionTypes();
for (int i = 0; i < exceptions.length; i++) {
LOG.debug("Checking exception: " + exceptions[i]);
WebFault wf = exceptions[i].getAnnotation(WebFault.class);
if (wf == null) {
LOG.debug("No WebFault annotation");
continue;
}
QName exceptionName = new QName(wf.targetNamespace(), wf.name());
if (exceptionName.equals(qname)) {
try {
Method mth = exceptions[i].getMethod("getFaultInfo");
Class<?> infoClass = mth.getReturnType();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(baos);
new StaxSerializer().writeElement(detail, writer);
writer.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
JAXBElement<?> obj = JAXBContext.newInstance(infoClass).createUnmarshaller()
.unmarshal(new StreamSource(bais), infoClass);
Constructor<?> cst = exceptions[i].getConstructor(String.class, infoClass);
return (Exception) cst.newInstance(xfireFault.toString(), obj.getValue());
} catch (Throwable e) {
LOG.debug("Error: " + e);
}
} else {
LOG.debug("QName mismatch: element: " + qname + ", exception: " + exceptionName);