use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class WebFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Fault f = (Fault) message.getContent(Exception.class);
if (f == null) {
return;
}
try {
Throwable thr = f.getCause();
SOAPFaultException sf = null;
if (thr instanceof SOAPFaultException) {
sf = (SOAPFaultException) thr;
} else if (thr.getCause() instanceof SOAPFaultException) {
sf = (SOAPFaultException) thr.getCause();
}
if (sf != null) {
SoapVersion soapVersion = (SoapVersion) message.get(SoapVersion.class.getName());
if (soapVersion != null && soapVersion.getVersion() != 1.1) {
if (f instanceof SoapFault) {
for (Iterator<QName> it = CastUtils.cast(sf.getFault().getFaultSubcodes()); it.hasNext(); ) {
((SoapFault) f).addSubCode(it.next());
}
}
if (sf.getFault().getFaultReasonLocales().hasNext()) {
Locale lang = (Locale) sf.getFault().getFaultReasonLocales().next();
String convertedLang = lang.getLanguage();
String country = lang.getCountry();
if (country.length() > 0) {
convertedLang = convertedLang + '-' + country;
}
f.setLang(convertedLang);
}
}
message.setContent(Exception.class, f);
}
} catch (Exception e) {
// do nothing;
}
Throwable cause = f.getCause();
WebFault fault = null;
if (cause != null) {
fault = getWebFaultAnnotation(cause.getClass());
if (fault == null && cause.getCause() != null) {
fault = getWebFaultAnnotation(cause.getCause().getClass());
if (fault != null || cause instanceof RuntimeException) {
cause = cause.getCause();
}
}
}
if (cause instanceof Exception && fault != null) {
Exception ex = (Exception) cause;
Object faultInfo;
try {
Method method = cause.getClass().getMethod("getFaultInfo", new Class[0]);
faultInfo = method.invoke(cause, new Object[0]);
} catch (NoSuchMethodException e) {
faultInfo = createFaultInfoBean(fault, cause);
} catch (InvocationTargetException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("INVOCATION_TARGET_EXC", BUNDLE), e);
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
}
Service service = message.getExchange().getService();
try {
DataWriter<XMLStreamWriter> writer = service.getDataBinding().createWriter(XMLStreamWriter.class);
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message)) {
Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
writer.setSchema(schema);
}
OperationInfo op = null;
// Prevent a NPE if we can't match the operation
if (message.getExchange().getBindingOperationInfo() != null) {
op = message.getExchange().getBindingOperationInfo().getOperationInfo();
}
QName faultName = getFaultName(fault, cause.getClass(), op);
MessagePartInfo part = op != null ? getFaultMessagePart(faultName, op) : null;
if (f.hasDetails()) {
writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getDetail()));
} else {
writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getOrCreateDetail()));
if (!f.getDetail().hasChildNodes()) {
f.setDetail(null);
}
}
f.setMessage(ex.getMessage());
} catch (Fault f2) {
message.setContent(Exception.class, f2);
super.handleMessage(message);
} catch (Exception nex) {
// if exception occurs while writing a fault, we'll just let things continue
// and let the rest of the chain try handling it as is.
LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", nex);
}
} else if (cause instanceof SOAPFaultException && ((SOAPFaultException) cause).getFault().hasDetail()) {
return;
} else if (f instanceof SoapFault && f.getCause() instanceof SOAPFaultException && ((SOAPFaultException) f.getCause()).getFault().hasDetail()) {
return;
} else {
FaultMode mode = message.get(FaultMode.class);
if (mode == FaultMode.CHECKED_APPLICATION_FAULT) {
// only convert checked exceptions with this
// otherwise delegate down to the normal protocol specific stuff
super.handleMessage(message);
}
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class JAXRSBPNamespaceHandler method transformElement.
private Element transformElement(Element element) {
final Map<String, String> transformMap = Collections.singletonMap("{" + element.getNamespaceURI() + "}*", "{http://cxf.apache.org/blueprint/jaxrs-client}*");
W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter();
OutTransformWriter transformWriter = new OutTransformWriter(domWriter, transformMap);
try {
StaxUtils.copy(element, transformWriter);
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return domWriter.getDocument().getDocumentElement();
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class XMLStreamDataWriter method writeNode.
private void writeNode(Node nd, XMLStreamWriter writer) throws XMLStreamException {
if (writer instanceof W3CDOMStreamWriter) {
W3CDOMStreamWriter dw = (W3CDOMStreamWriter) writer;
if (dw.getCurrentNode() != null) {
if (nd instanceof DocumentFragment && nd.getOwnerDocument() == dw.getCurrentNode().getOwnerDocument()) {
Node ch = nd.getFirstChild();
while (ch != null) {
nd.removeChild(ch);
dw.getCurrentNode().appendChild(org.apache.cxf.helpers.DOMUtils.getDomElement(ch));
ch = nd.getFirstChild();
}
} else if (nd.getOwnerDocument() == dw.getCurrentNode().getOwnerDocument()) {
dw.getCurrentNode().appendChild(nd);
return;
} else if (nd instanceof DocumentFragment) {
nd = dw.getDocument().importNode(nd, true);
dw.getCurrentNode().appendChild(nd);
return;
}
} else if (dw.getCurrentFragment() != null) {
if (nd.getOwnerDocument() == dw.getCurrentFragment().getOwnerDocument()) {
dw.getCurrentFragment().appendChild(nd);
return;
} else if (nd instanceof DocumentFragment) {
nd = dw.getDocument().importNode(nd, true);
dw.getCurrentFragment().appendChild(nd);
return;
}
}
}
if (nd instanceof Document) {
StaxUtils.writeDocument((Document) nd, writer, false, true);
} else {
StaxUtils.writeNode(nd, writer, true);
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class NodeDataWriter method write.
public void write(Object obj, Node n) {
try {
Source s = (Source) obj;
if (s instanceof DOMSource && ((DOMSource) s).getNode() == null) {
return;
}
XMLStreamWriter writer = new W3CDOMStreamWriter((Element) n);
StaxUtils.copy(s, writer);
} catch (XMLStreamException e) {
throw new Fault("COULD_NOT_WRITE_XML_STREAM_CAUSED_BY", LOG, e, e.getClass().getCanonicalName(), e.getMessage());
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class OutTransformWriterTest method testNamespaceConversion.
@Test
public void testNamespaceConversion() throws Exception {
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
JAXBContext context = JAXBContext.newInstance(TestBean.class);
Marshaller m = context.createMarshaller();
Map<String, String> outMap = new HashMap<>();
outMap.put("{http://testbeans.com}testBean", "{http://testbeans.com/v2}testBean");
outMap.put("{http://testbeans.com}bean", "{http://testbeans.com/v3}bean");
OutTransformWriter transformWriter = new OutTransformWriter(writer, outMap, Collections.<String, String>emptyMap(), Collections.<String>emptyList(), false, "");
m.marshal(new TestBean(), transformWriter);
Element el = writer.getDocument().getDocumentElement();
assertEquals("http://testbeans.com/v2", el.getNamespaceURI());
assertFalse(StringUtils.isEmpty(el.getPrefix()));
Element el2 = DOMUtils.getFirstElement(el);
assertEquals("http://testbeans.com/v3", el2.getNamespaceURI());
assertFalse(StringUtils.isEmpty(el2.getPrefix()));
}
Aggregations