Search in sources :

Example 11 with W3CDOMStreamWriter

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);
        }
    }
}
Also used : Locale(java.util.Locale) OperationInfo(org.apache.cxf.service.model.OperationInfo) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.message.Message) Schema(javax.xml.validation.Schema) Fault(org.apache.cxf.interceptor.Fault) WebFault(javax.xml.ws.WebFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) WebFault(javax.xml.ws.WebFault) FaultMode(org.apache.cxf.message.FaultMode) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion)

Example 12 with W3CDOMStreamWriter

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();
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) OutTransformWriter(org.apache.cxf.staxutils.transform.OutTransformWriter) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 13 with W3CDOMStreamWriter

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);
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 14 with W3CDOMStreamWriter

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());
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Fault(org.apache.cxf.interceptor.Fault) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source)

Example 15 with W3CDOMStreamWriter

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()));
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) Marshaller(javax.xml.bind.Marshaller) HashMap(java.util.HashMap) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Aggregations

W3CDOMStreamWriter (org.apache.cxf.staxutils.W3CDOMStreamWriter)60 Element (org.w3c.dom.Element)29 DOMSource (javax.xml.transform.dom.DOMSource)24 XMLStreamException (javax.xml.stream.XMLStreamException)15 Document (org.w3c.dom.Document)14 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Node (org.w3c.dom.Node)9 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)8 Fault (org.apache.cxf.interceptor.Fault)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)7 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)7 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)7 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)7 JAXBElement (javax.xml.bind.JAXBElement)6 JAXBException (javax.xml.bind.JAXBException)5 SOAPMessage (javax.xml.soap.SOAPMessage)5 SoapFault (org.apache.cxf.binding.soap.SoapFault)5 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4