Java Code Examples for org.apache.cxf.staxutils.StaxUtils#writeNode()

The following examples show how to use org.apache.cxf.staxutils.StaxUtils#writeNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAegisTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected String renderWsdl(Document wsdlDoc) throws XMLStreamException {
    StringWriter out = new StringWriter();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
    StaxUtils.writeNode(wsdlDoc, writer, true);
    writer.flush();
    return out.toString();
}
 
Example 2
Source File: SchemaValidationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private boolean testInvalidArray(Object validationType) throws Exception {
    server.getEndpoint().getService().put(Message.SCHEMA_VALIDATION_ENABLED, validationType);
    Node r = invoke("Array", "/org/apache/cxf/aegis/integration/invalidArrayMessage.xml");
    assertNotNull(r);
    StringWriter out = new StringWriter();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
    StaxUtils.writeNode(r, writer, true);
    writer.flush();
    String m = out.toString();
    return m.contains("Fault");
}
 
Example 3
Source File: XMLStreamDataWriter.java    From cxf with Apache License 2.0 5 votes vote down vote up
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);
    }

}
 
Example 4
Source File: Soap12FaultSoapPayloadConverter.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Override
protected void handleFault(XMLStreamWriter writer, SoapFault soapFault, SoapVersion soapVersion) {
    try {
        Map<String, String> namespaces = soapFault.getNamespaces();
        for (Map.Entry<String, String> e : namespaces.entrySet()) {
            writer.writeNamespace(e.getKey(), e.getValue());
        }

        String ns = soapVersion.getNamespace();
        writer.writeStartElement(SOAP_PREFIX, "Fault", ns);

        writer.writeStartElement(SOAP_PREFIX, "Code", ns);
        writer.writeStartElement(SOAP_PREFIX, "Value", ns);

        writer.writeCharacters(soapFault.getCodeString(getFaultCodePrefix(writer, soapFault.getFaultCode()), SOAP_PREFIX));
        writer.writeEndElement();

        if (soapFault.getSubCodes() != null) {
            int fscCount = 0;
            for (QName fsc : soapFault.getSubCodes()) {
                writer.writeStartElement(SOAP_PREFIX, "Subcode", ns);
                writer.writeStartElement(SOAP_PREFIX, "Value", ns);
                writer.writeCharacters(getCodeString(getFaultCodePrefix(writer, fsc), SOAP_PREFIX, fsc));
                writer.writeEndElement();
                fscCount++;
            }
            while (fscCount > 0) {
                writer.writeEndElement();
                fscCount--;
            }
        }
        writer.writeEndElement();

        writer.writeStartElement(SOAP_PREFIX, "Reason", ns);
        writer.writeStartElement(SOAP_PREFIX, "Text", ns);
        String lang = soapFault.getLang();
        if (StringUtils.isEmpty(lang)) {
            lang = getLangCode();
        }
        writer.writeAttribute("xml", "http://www.w3.org/XML/1998/namespace", "lang", lang);
        writer.writeCharacters(getFaultMessage(soapFault));
        writer.writeEndElement();
        writer.writeEndElement();

        if (soapFault.getRole() != null) {
            writer.writeStartElement(SOAP_PREFIX, "Role", ns);
            writer.writeCharacters(soapFault.getRole());
            writer.writeEndElement();
        }

        prepareStackTrace(ns, soapFault);

        if (soapFault.hasDetails()) {
            Element detail = soapFault.getDetail();
            writer.writeStartElement(SOAP_PREFIX, "Detail", ns);

            Node node = detail.getFirstChild();
            while (node != null) {
                StaxUtils.writeNode(node, writer, true);
                node = node.getNextSibling();
            }

            // Details
            writer.writeEndElement();
        }

        // Fault
        writer.writeEndElement();
    } catch (Exception xe) {
        throw new RuntimeCamelException("XML Write Exception: " + xe.getMessage(), xe);
    }
}
 
Example 5
Source File: Soap11FaultSoapPayloadConverter.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Override
protected void handleFault(XMLStreamWriter writer, SoapFault fault, SoapVersion soapVersion) {
    try {
        Map<String, String> namespaces = fault.getNamespaces();
        for (Map.Entry<String, String> e : namespaces.entrySet()) {
            writer.writeNamespace(e.getKey(), e.getValue());
        }

        final String soapNamespace = soapVersion.getNamespace();
        writer.writeStartElement(SOAP_PREFIX, "Fault", soapNamespace);

        writer.writeStartElement("faultcode");

        String codeString = fault.getCodeString(getFaultCodePrefix(writer, fault.getFaultCode()), SOAP_PREFIX);

        writer.writeCharacters(codeString);
        writer.writeEndElement();

        writer.writeStartElement("faultstring");
        String lang = fault.getLang();
        if (!StringUtils.isEmpty(lang)) {
            writer.writeAttribute("xml", "http://www.w3.org/XML/1998/namespace", "lang", lang);
        }
        writer.writeCharacters(getFaultMessage(fault));
        writer.writeEndElement();
        prepareStackTrace(soapNamespace, fault);

        if (fault.getRole() != null) {
            writer.writeStartElement("faultactor");
            writer.writeCharacters(fault.getRole());
            writer.writeEndElement();
        }

        if (fault.hasDetails()) {
            Element detail = fault.getDetail();
            writer.writeStartElement("detail");

            Node node = detail.getFirstChild();
            while (node != null) {
                StaxUtils.writeNode(node, writer, true);
                node = node.getNextSibling();
            }

            // Details
            writer.writeEndElement();
        }

        // Fault
        writer.writeEndElement();
    } catch (Exception xe) {
        throw new RuntimeCamelException("XML write exception: " + xe.getMessage(), xe);
    }
}
 
Example 6
Source File: JAXRSDefaultFaultOutInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void handleMessage(Message message) throws Fault {
    if (PropertyUtils.isTrue(message.getExchange().get(JAXRSUtils.SECOND_JAXRS_EXCEPTION))) {
        return;
    }
    final Fault f = (Fault) message.getContent(Exception.class);

    Response r = JAXRSUtils.convertFaultToResponse(f.getCause(), message);
    if (r != null) {
        JAXRSUtils.setMessageContentType(message, r);
        message.setContent(List.class, new MessageContentsList(r));
        if (message.getExchange().getOutMessage() == null && message.getExchange().getOutFaultMessage() != null) {
            message.getExchange().setOutMessage(message.getExchange().getOutFaultMessage());
        }
        new JAXRSOutInterceptor().handleMessage(message);
        return;
    }

    ServerProviderFactory.releaseRequestState(message);
    if (mustPropogateException(message)) {
        throw f;
    }

    new StaxOutInterceptor().handleMessage(message);
    message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
    NSStack nsStack = new NSStack();
    nsStack.push();

    XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
    try {
        nsStack.add("http://cxf.apache.org/bindings/xformat");
        String prefix = nsStack.getPrefix("http://cxf.apache.org/bindings/xformat");
        StaxUtils.writeStartElement(writer, prefix, "XMLFault",
                                    "http://cxf.apache.org/bindings/xformat");
        StaxUtils.writeStartElement(writer, prefix, "faultstring",
                                    "http://cxf.apache.org/bindings/xformat");
        Throwable t = f.getCause();
        writer.writeCharacters(t == null ? f.getMessage() : t.toString());
        // fault string
        writer.writeEndElement();
        // call StaxUtils to write Fault detail.

        if (f.getDetail() != null) {
            StaxUtils.writeStartElement(writer, prefix, "detail", "http://cxf.apache.org/bindings/xformat");
            StaxUtils.writeNode(DOMUtils.getChild(f.getDetail(), Node.ELEMENT_NODE),
                                writer, false);
            writer.writeEndElement();
        }
        // fault root
        writer.writeEndElement();
        writer.flush();
    } catch (XMLStreamException xe) {
        throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
    }
}
 
Example 7
Source File: XMLFaultOutInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void handleMessage(Message message) throws Fault {

        if (mustPropogateException(message)) {
            throw (Fault) message.getContent(Exception.class);
        }

        Fault f = (Fault) message.getContent(Exception.class);
        message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
        NSStack nsStack = new NSStack();
        nsStack.push();

        XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
        XMLFault xmlFault = XMLFault.createFault(f);
        try {
            nsStack.add(XMLConstants.NS_XML_FORMAT);
            String prefix = nsStack.getPrefix(XMLConstants.NS_XML_FORMAT);
            StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_ROOT,
                    XMLConstants.NS_XML_FORMAT);
            StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_STRING,
                    XMLConstants.NS_XML_FORMAT);
            Throwable t = xmlFault.getCause();
            writer.writeCharacters(t == null ? xmlFault.getMessage() : t.toString());
            // fault string
            writer.writeEndElement();
            // call StaxUtils to write Fault detail.

            if (xmlFault.getDetail() != null) {
                Element detail = xmlFault.getDetail();
                StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_DETAIL,
                        XMLConstants.NS_XML_FORMAT);

                Node node = detail.getFirstChild();
                while (node != null) {
                    StaxUtils.writeNode(node, writer, false);
                    node = node.getNextSibling();
                }
                writer.writeEndElement();
            }
            // fault root
            writer.writeEndElement();
            writer.flush();
        } catch (XMLStreamException xe) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
        }
    }
 
Example 8
Source File: Soap12FaultOutInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void handleMessage(SoapMessage message) throws Fault {
    LOG.info(getClass() + (String) message.get(Message.CONTENT_TYPE));

    XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
    Fault f = (Fault)message.getContent(Exception.class);
    message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());

    SoapFault fault = SoapFault.createFault(f, message.getVersion());

    try {
        Map<String, String> namespaces = fault.getNamespaces();
        for (Map.Entry<String, String> e : namespaces.entrySet()) {
            writer.writeNamespace(e.getKey(), e.getValue());
        }

        String ns = message.getVersion().getNamespace();
        String defaultPrefix = writer.getPrefix(ns);
        if (defaultPrefix == null) {
            defaultPrefix = StaxUtils.getUniquePrefix(writer, ns, false);
            writer.writeStartElement(defaultPrefix, "Fault", ns);
            writer.writeNamespace(defaultPrefix, ns);
        } else {
            writer.writeStartElement(defaultPrefix, "Fault", ns);
        }

        writer.writeStartElement(defaultPrefix, "Code", ns);
        writer.writeStartElement(defaultPrefix, "Value", ns);

        writer.writeCharacters(fault.getCodeString(getFaultCodePrefix(writer, fault.getFaultCode()),
                                                   defaultPrefix));
        writer.writeEndElement();

        if (fault.getSubCodes() != null) {
            int fscCount = 0;
            for (QName fsc : fault.getSubCodes()) {
                writer.writeStartElement(defaultPrefix, "Subcode", ns);
                writer.writeStartElement(defaultPrefix, "Value", ns);
                writer.writeCharacters(getCodeString(getFaultCodePrefix(writer, fsc),
                                                     defaultPrefix, fsc));
                writer.writeEndElement();
                fscCount++;
            }
            while (fscCount-- > 0) {
                writer.writeEndElement();
            }
        }
        writer.writeEndElement();

        writer.writeStartElement(defaultPrefix, "Reason", ns);
        writer.writeStartElement(defaultPrefix, "Text", ns);
        String lang = f.getLang();
        if (StringUtils.isEmpty(lang)) {
            lang = getLangCode();
        }
        writer.writeAttribute("xml", "http://www.w3.org/XML/1998/namespace", "lang", lang);
        writer.writeCharacters(getFaultMessage(message, fault));
        writer.writeEndElement();
        writer.writeEndElement();

        if (fault.getRole() != null) {
            writer.writeStartElement(defaultPrefix, "Role", ns);
            writer.writeCharacters(fault.getRole());
            writer.writeEndElement();
        }

        prepareStackTrace(message, fault);

        if (fault.hasDetails()) {
            Element detail = fault.getDetail();
            writer.writeStartElement(defaultPrefix, "Detail", ns);

            Node node = detail.getFirstChild();
            while (node != null) {
                StaxUtils.writeNode(node, writer, true);
                node = node.getNextSibling();
            }

            // Details
            writer.writeEndElement();
        }

        // Fault
        writer.writeEndElement();
    } catch (Exception xe) {
        LOG.log(Level.WARNING, "XML_WRITE_EXC", xe);
        throw f;
    }
}
 
Example 9
Source File: Soap11FaultOutInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void handleMessage(SoapMessage message) throws Fault {
    XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
    Fault f = (Fault) message.getContent(Exception.class);

    SoapFault fault = SoapFault.createFault(f, message.getVersion());

    try {
        Map<String, String> namespaces = fault.getNamespaces();
        for (Map.Entry<String, String> e : namespaces.entrySet()) {
            writer.writeNamespace(e.getKey(), e.getValue());
        }

        String ns = message.getVersion().getNamespace();
        String defaultPrefix = writer.getPrefix(ns);
        if (defaultPrefix == null) {
            defaultPrefix = StaxUtils.getUniquePrefix(writer, ns, false);
            writer.writeStartElement(defaultPrefix, "Fault", ns);
            writer.writeNamespace(defaultPrefix, ns);
        } else {
            writer.writeStartElement(defaultPrefix, "Fault", ns);
        }

        writer.writeStartElement("faultcode");

        String codeString = fault.getCodeString(getFaultCodePrefix(writer, fault.getFaultCode()),
                defaultPrefix);

        writer.writeCharacters(codeString);
        writer.writeEndElement();

        writer.writeStartElement("faultstring");
        String lang = f.getLang();
        if (!StringUtils.isEmpty(lang)) {
            writer.writeAttribute("xml", "http://www.w3.org/XML/1998/namespace", "lang", lang);
        }
        writer.writeCharacters(getFaultMessage(message, fault));
        writer.writeEndElement();
        prepareStackTrace(message, fault);

        if (fault.getRole() != null) {
            writer.writeStartElement("faultactor");
            writer.writeCharacters(fault.getRole());
            writer.writeEndElement();
        }

        if (fault.hasDetails()) {
            Element detail = fault.getDetail();
            writer.writeStartElement("detail");

            Node node = detail.getFirstChild();
            while (node != null) {
                StaxUtils.writeNode(node, writer, true);
                node = node.getNextSibling();
            }

            // Details
            writer.writeEndElement();
        }

        // Fault
        writer.writeEndElement();
    } catch (Exception xe) {
        LOG.log(Level.WARNING, "XML_WRITE_EXC", xe);
        throw f;
    }
}
 
Example 10
Source File: ServiceWSDLBuilderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testXsdImportMultipleSchemas() throws Exception {
    setupWSDL(WSDL_XSD_IMPORT_PATH, true);

    Types types = newDef.getTypes();
    assertNotNull(types);

    Collection<ExtensibilityElement> schemas = CastUtils.cast(types.getExtensibilityElements(),
                                                              ExtensibilityElement.class);
    assertEquals(1, schemas.size());

    Schema schema = (Schema)schemas.iterator().next();

    assertEquals(1, schema.getImports().values().size());

    SchemaImport serviceTypesSchemaImport = getImport(schema.getImports(),
            "http://apache.org/hello_world_soap_http/servicetypes");

    Schema serviceTypesSchema = serviceTypesSchemaImport.getReferencedSchema();

    assertEquals(1, serviceTypesSchema.getImports().values().size());
    SchemaImport typesSchemaImport = getImport(serviceTypesSchema.getImports(),
            "http://apache.org/hello_world_soap_http/types");

    Schema typesSchema = typesSchemaImport.getReferencedSchema();

    Document doc = typesSchema.getElement().getOwnerDocument();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(outputStream, "utf-8");
    StaxUtils.writeNode(doc, writer, true);
    writer.close();

    // this is a test to make sure any embedded namespaces are properly included
    String savedSchema = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
    assertTrue(savedSchema.contains("http://www.w3.org/2005/05/xmlmime"));

    SchemaImport types2SchemaImport = getImport(typesSchema.getImports(),
            "http://apache.org/hello_world_soap_http/types2");

    Schema types2Schema = types2SchemaImport.getReferencedSchema();
    assertNotNull(types2Schema);
}