Search in sources :

Example 21 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class ExceptionHandlerStreamCacheTest method doTestXmlPayload.

private void doTestXmlPayload(String xml) throws InterruptedException, TransformerException {
    successEndpoint.expectedMessageCount(0);
    exceptionEndpoint.expectedMessageCount(1);
    template.sendBody("direct:start", new StreamSource(new ByteArrayInputStream(xml.getBytes())));
    successEndpoint.assertIsSatisfied();
    exceptionEndpoint.assertIsSatisfied();
    StreamSource body = (StreamSource) exceptionEndpoint.getExchanges().get(0).getIn().getBody();
    assertEquals("Ensure message re-readability in the exception handler", xml, new XmlConverter().toString(body, null));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 22 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class CxfMtomProducerPayloadModeTest method testProducer.

@SuppressWarnings("unchecked")
@Test
public void testProducer() throws Exception {
    if (MtomTestHelper.isAwtHeadless(logger, null)) {
        return;
    }
    // START SNIPPET: producer
    Exchange exchange = context.createProducerTemplate().send("direct:testEndpoint", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            List<Source> elements = new ArrayList<Source>();
            elements.add(new DOMSource(StaxUtils.read(new StringReader(MtomTestHelper.REQ_MESSAGE)).getDocumentElement()));
            CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null);
            exchange.getIn().setBody(body);
            exchange.getIn().addAttachment(MtomTestHelper.REQ_PHOTO_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.REQ_PHOTO_DATA, "application/octet-stream")));
            exchange.getIn().addAttachment(MtomTestHelper.REQ_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.requestJpeg, "image/jpeg")));
        }
    });
    // process response 
    CxfPayload<SoapHeader> out = exchange.getOut().getBody(CxfPayload.class);
    Assert.assertEquals(1, out.getBody().size());
    Map<String, String> ns = new HashMap<String, String>();
    ns.put("ns", MtomTestHelper.SERVICE_TYPES_NS);
    ns.put("xop", MtomTestHelper.XOP_NS);
    XPathUtils xu = new XPathUtils(ns);
    Element oute = new XmlConverter().toDOMElement(out.getBody().get(0));
    Element ele = (Element) xu.getValue("//ns:DetailResponse/ns:photo/xop:Include", oute, XPathConstants.NODE);
    // skip "cid:"
    String photoId = ele.getAttribute("href").substring(4);
    ele = (Element) xu.getValue("//ns:DetailResponse/ns:image/xop:Include", oute, XPathConstants.NODE);
    // skip "cid:"
    String imageId = ele.getAttribute("href").substring(4);
    DataHandler dr = exchange.getOut().getAttachment(decodingReference(photoId));
    Assert.assertEquals("application/octet-stream", dr.getContentType());
    MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
    dr = exchange.getOut().getAttachment(decodingReference(imageId));
    Assert.assertEquals("image/jpeg", dr.getContentType());
    BufferedImage image = ImageIO.read(dr.getInputStream());
    Assert.assertEquals(560, image.getWidth());
    Assert.assertEquals(300, image.getHeight());
// END SNIPPET: producer
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Source(javax.xml.transform.Source) BufferedImage(java.awt.image.BufferedImage) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) Exchange(org.apache.camel.Exchange) XPathUtils(org.apache.cxf.helpers.XPathUtils) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 23 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class DumpModelAsXmlSplitNestedChoiceEndRouteTest method testDumpModelAsXml.

public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);
    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("split");
    assertEquals(1, nodes.getLength());
    Element node = (Element) nodes.item(0);
    // there is an empty text document as we pretty print the xml, so need to do 2 x next sibling
    Element last = (Element) node.getNextSibling().getNextSibling();
    assertEquals("mock:last", last.getAttribute("uri"));
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 24 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class CxfPayLoadMessageRouterTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            // START SNIPPET: payload
            from(routerEndpointURI).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    CxfPayload<?> payload = exchange.getIn().getBody(CxfPayload.class);
                    List<Source> elements = payload.getBodySources();
                    assertNotNull("We should get the elements here", elements);
                    assertEquals("Get the wrong elements size", elements.size(), 1);
                    Element el = new XmlConverter().toDOMElement(elements.get(0));
                    assertEquals("Get the wrong namespace URI", el.getNamespaceURI(), "http://cxf.component.camel.apache.org/");
                }
            }).to(serviceEndpointURI);
        // END SNIPPET: payload
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 25 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class ModelHelper method newXmlConverter.

/**
     * Creates a new {@link XmlConverter}
     *
     * @param context CamelContext if provided
     * @return a new XmlConverter instance
     */
private static XmlConverter newXmlConverter(CamelContext context) {
    XmlConverter xmlConverter;
    if (context != null) {
        TypeConverterRegistry registry = context.getTypeConverterRegistry();
        xmlConverter = registry.getInjector().newInstance(XmlConverter.class);
    } else {
        xmlConverter = new XmlConverter();
    }
    return xmlConverter;
}
Also used : TypeConverterRegistry(org.apache.camel.spi.TypeConverterRegistry) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Aggregations

XmlConverter (org.apache.camel.converter.jaxp.XmlConverter)29 Element (org.w3c.dom.Element)14 Exchange (org.apache.camel.Exchange)10 Document (org.w3c.dom.Document)10 Source (javax.xml.transform.Source)9 Processor (org.apache.camel.Processor)7 RouteBuilder (org.apache.camel.builder.RouteBuilder)6 NodeList (org.w3c.dom.NodeList)4 StringReader (java.io.StringReader)3 List (java.util.List)3 TransformerException (javax.xml.transform.TransformerException)3 DOMSource (javax.xml.transform.dom.DOMSource)3 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 JAXBContext (javax.xml.bind.JAXBContext)2