Search in sources :

Example 6 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class CxfPayloadConverterTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    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 = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    List<Source> body = new ArrayList<Source>();
    body.add(new DOMSource(document.getDocumentElement()));
    List<Source> staxbody = new ArrayList<Source>();
    staxbody.add(new StAXSource(StaxUtils.createXMLStreamReader(new FileInputStream(file), "utf-8")));
    payload = new CxfPayload<String[]>(new ArrayList<String[]>(), body, null);
    emptyPayload = new CxfPayload<String[]>(new ArrayList<String[]>(), new ArrayList<Source>(), null);
    staxpayload = new CxfPayload<String[]>(new ArrayList<String[]>(), staxbody, null);
    inputStream = new FileInputStream(file);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) StAXSource(javax.xml.transform.stax.StAXSource) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 7 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class CxfPayLoadSoapHeaderTest method createRouteBuilder.

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

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

                @SuppressWarnings("unchecked")
                public void process(Exchange exchange) throws Exception {
                    CxfPayload<SoapHeader> 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", 1, elements.size());
                    Element el = new XmlConverter().toDOMElement(elements.get(0));
                    elements.set(0, new DOMSource(el));
                    assertEquals("Get the wrong namespace URI", "http://camel.apache.org/pizza/types", el.getNamespaceURI());
                    List<SoapHeader> headers = payload.getHeaders();
                    assertNotNull("We should get the headers here", headers);
                    assertEquals("Get the wrong headers size", headers.size(), 1);
                    assertEquals("Get the wrong namespace URI", ((Element) (headers.get(0).getObject())).getNamespaceURI(), "http://camel.apache.org/pizza/types");
                    // alternatively you can also get the SOAP header via the camel header:
                    headers = exchange.getIn().getHeader(Header.HEADER_LIST, List.class);
                    assertNotNull("We should get the headers here", headers);
                    assertEquals("Get the wrong headers size", headers.size(), 1);
                    assertEquals("Get the wrong namespace URI", ((Element) (headers.get(0).getObject())).getNamespaceURI(), "http://camel.apache.org/pizza/types");
                }
            }).to(getServiceEndpointURI());
        // END SNIPPET: payload
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) List(java.util.List) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 8 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class CachedCxfPayloadTest method testCachedCxfPayloadDOMSource.

@Test
public void testCachedCxfPayloadDOMSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException {
    DOMSource source = context.getTypeConverter().mandatoryConvertTo(DOMSource.class, PAYLOAD);
    doTest(source, PAYLOAD);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Test(org.junit.Test)

Example 9 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class CxfMessageHeadersRelayTest method testInoutHeaderCXFClientNoServiceClassNoRelay.

@Test
public void testInoutHeaderCXFClientNoServiceClassNoRelay() throws Exception {
    // TODO: Fix this test later
    QName qname = QName.valueOf("{http://apache.org/camel/component/cxf/soap/headers}SOAPHeaderInfo");
    String uri = "cxf:bean:routerNoRelayNoServiceClassEndpoint?headerFilterStrategy=#dropAllMessageHeadersStrategy";
    String requestHeader = "<ns2:SOAPHeaderInfo xmlns:ns2=\"http://apache.org/camel/" + "component/cxf/soap/headers\"><originator>CxfSoapHeaderRoutePropagationTest.testInOutHeader Requestor" + "</originator><message>Invoking CxfSoapHeaderRoutePropagationTest.testInOutHeader() Request" + "</message></ns2:SOAPHeaderInfo>";
    String requestBody = "<ns2:inoutHeader xmlns:ns2=\"http://apache.org/camel/component/cxf/soap/headers\">" + "<requestType>CXF user</requestType></ns2:inoutHeader>";
    List<Source> elements = new ArrayList<Source>();
    elements.add(new DOMSource(StaxUtils.read(new StringReader(requestBody)).getDocumentElement()));
    final List<SoapHeader> headers = new ArrayList<SoapHeader>();
    headers.add(new SoapHeader(qname, StaxUtils.read(new StringReader(requestHeader)).getDocumentElement()));
    final CxfPayload<SoapHeader> cxfPayload = new CxfPayload<SoapHeader>(headers, elements, null);
    Exchange exchange = template.request(uri, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(cxfPayload);
            exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "inoutHeader");
            exchange.getIn().setHeader(Header.HEADER_LIST, headers);
        }
    });
    CxfPayload<?> out = exchange.getOut().getBody(CxfPayload.class);
    assertEquals(1, out.getBodySources().size());
    assertTrue(out.getBodySources().get(0) instanceof DOMSource);
    assertEquals(0, out.getHeaders().size());
    String responseExp = "<ns2:inoutHeaderResponse xmlns:ns2=\"http://apache.org/camel/" + "component/cxf/soap/headers\"><responseType>pass</responseType>" + "</ns2:inoutHeaderResponse>";
    String response = StaxUtils.toString(out.getBody().get(0));
    //REVISIT use a more reliable comparison to tolerate some namespaces being added to the root element
    assertTrue(response, response.startsWith(responseExp.substring(0, 87)) && response.endsWith(responseExp.substring(88, responseExp.length())));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) JAXBException(javax.xml.bind.JAXBException) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Test(org.junit.Test)

Example 10 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class CxfMtomDisabledProducerPayloadModeTest method testProducer.

@Override
public void testProducer() throws Exception {
    if (MtomTestHelper.isAwtHeadless(logger, null)) {
        return;
    }
    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.MTOM_DISABLED_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 - verify response attachments
    CxfPayload<?> out = exchange.getOut().getBody(CxfPayload.class);
    Assert.assertEquals(1, out.getBody().size());
    DataHandler dr = exchange.getOut().getAttachment(MtomTestHelper.RESP_PHOTO_CID);
    Assert.assertEquals("application/octet-stream", dr.getContentType());
    MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
    dr = exchange.getOut().getAttachment(MtomTestHelper.RESP_IMAGE_CID);
    Assert.assertEquals("image/jpeg", dr.getContentType());
    BufferedImage image = ImageIO.read(dr.getInputStream());
    Assert.assertEquals(560, image.getWidth());
    Assert.assertEquals(300, image.getHeight());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) AttachmentDataSource(org.apache.cxf.attachment.AttachmentDataSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) BufferedImage(java.awt.image.BufferedImage) Exchange(org.apache.camel.Exchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Aggregations

DOMSource (javax.xml.transform.dom.DOMSource)392 StreamResult (javax.xml.transform.stream.StreamResult)231 Transformer (javax.xml.transform.Transformer)204 Document (org.w3c.dom.Document)161 TransformerFactory (javax.xml.transform.TransformerFactory)112 TransformerException (javax.xml.transform.TransformerException)107 DocumentBuilder (javax.xml.parsers.DocumentBuilder)102 StringWriter (java.io.StringWriter)97 IOException (java.io.IOException)93 Element (org.w3c.dom.Element)81 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)77 Source (javax.xml.transform.Source)67 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)62 SAXException (org.xml.sax.SAXException)56 File (java.io.File)55 InputSource (org.xml.sax.InputSource)50 StreamSource (javax.xml.transform.stream.StreamSource)47 Node (org.w3c.dom.Node)45 InputStream (java.io.InputStream)35 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35