Search in sources :

Example 6 with Document

use of org.w3c.dom.Document in project camel by apache.

the class RawMessageWSDLGetOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Document doc = (Document) message.get(RawMessageWSDLGetInterceptor.DOCUMENT_HOLDER);
    if (doc == null) {
        return;
    }
    message.remove(RawMessageWSDLGetInterceptor.DOCUMENT_HOLDER);
    OutputStream out = message.getContent(OutputStream.class);
    String enc = null;
    try {
        enc = doc.getXmlEncoding();
    } catch (Exception ex) {
    //ignore - not dom level 3
    }
    if (enc == null) {
        enc = "utf-8";
    }
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out, enc);
    try {
        StaxUtils.writeNode(doc, writer, true);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new Fault(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) Fault(org.apache.cxf.interceptor.Fault) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 7 with Document

use of org.w3c.dom.Document in project camel by apache.

the class CxfJavaOnlyCamelContextAwareTest method testCxfEndpointHasCamelContext.

@Test
public void testCxfEndpointHasCamelContext() throws Exception {
    String s = "<GetPerson xmlns=\"http://camel.apache.org/wsdl-first/types\"><personId>123</personId></GetPerson>";
    Document xml = context.getTypeConverter().convertTo(Document.class, s);
    log.info("Endpoints: {}", context.getEndpoints());
    Object output = template.requestBody("personService", xml);
    assertNotNull(output);
    // using CxfPayload in payload mode
    CxfPayload<?> payload = (CxfPayload<?>) output;
    // convert the payload body to string
    String reply = context.getTypeConverter().convertTo(String.class, payload.getBody().get(0));
    assertNotNull(reply);
    assertTrue(reply.contains("<personId>123</personId"));
    assertTrue(reply.contains("<ssn>456</ssn"));
    assertTrue(reply.contains("<name>Donald Duck</name"));
    assertTrue(context.getEndpoint("personService") instanceof CamelContextAware);
    assertNotNull("CamelContext should be set on CxfEndpoint", context.getEndpoint("personService").getCamelContext());
}
Also used : CamelContextAware(org.apache.camel.CamelContextAware) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 8 with Document

use of org.w3c.dom.Document in project camel by apache.

the class ConverterTest method testFallbackConverter.

@Test
public void testFallbackConverter() throws Exception {
    CamelContext context = new DefaultCamelContext();
    Exchange exchange = new DefaultExchange(context);
    MessageContentsList list = new MessageContentsList();
    NodeListWrapper nl = new NodeListWrapper(new ArrayList<Element>());
    list.add(nl);
    exchange.getIn().setBody(list);
    Node node = exchange.getIn().getBody(Node.class);
    assertNull(node);
    File file = new File("src/test/resources/org/apache/camel/component/cxf/converter/test.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    List<Element> elements = new ArrayList<Element>();
    elements.add(document.getDocumentElement());
    nl = new NodeListWrapper(elements);
    list.clear();
    list.add(nl);
    exchange.getIn().setBody(list);
    node = exchange.getIn().getBody(Node.class);
    assertNotNull(node);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) MessageContentsList(org.apache.cxf.message.MessageContentsList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) Test(org.junit.Test)

Example 9 with Document

use of org.w3c.dom.Document in project camel by apache.

the class DefaultCxfBindingTest method testPayloadBodyNamespace.

@Test
public void testPayloadBodyNamespace() throws Exception {
    MessageImpl message = new MessageImpl();
    Map<String, String> nsMap = new HashMap<String, String>();
    Document document = getDocument(SOAP_MESSAGE_1);
    message.setContent(Node.class, document);
    DefaultCxfBinding.getPayloadBodyElements(message, nsMap);
    assertEquals(2, nsMap.size());
    assertEquals("http://www.mycompany.com/test/", nsMap.get("xmlns"));
    Element element = document.createElement("tag");
    DefaultCxfBinding.addNamespace(element, nsMap);
    assertEquals("http://www.mycompany.com/test/", element.getAttribute("xmlns"));
    assertEquals("http://www.mycompany.com/test/1/", element.getAttribute("xmlns:ns1"));
}
Also used : HashMap(java.util.HashMap) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 10 with Document

use of org.w3c.dom.Document in project camel by apache.

the class DefaultCxfBindingTest method getDocument.

private Document getDocument(String soapMessage) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(IOConverter.toInputStream(soapMessage, null));
    document.getDocumentElement().normalize();
    return document;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document)

Aggregations

Document (org.w3c.dom.Document)2446 Element (org.w3c.dom.Element)990 DocumentBuilder (javax.xml.parsers.DocumentBuilder)719 NodeList (org.w3c.dom.NodeList)648 Node (org.w3c.dom.Node)545 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)528 IOException (java.io.IOException)425 SAXException (org.xml.sax.SAXException)301 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)299 InputSource (org.xml.sax.InputSource)250 Test (org.junit.Test)233 File (java.io.File)190 StringReader (java.io.StringReader)182 ArrayList (java.util.ArrayList)174 InputStream (java.io.InputStream)167 DOMSource (javax.xml.transform.dom.DOMSource)161 ByteArrayInputStream (java.io.ByteArrayInputStream)154 Attr (org.w3c.dom.Attr)134 DOMException (org.w3c.dom.DOMException)129 XPath (javax.xml.xpath.XPath)107