Search in sources :

Example 1 with StringResult

use of org.springframework.xml.transform.StringResult in project spring-integration by spring-projects.

the class StringSourceFactory method createStringSourceForDocument.

private StringSource createStringSourceForDocument(Document document) {
    try {
        StringResult result = new StringResult();
        Transformer transformer = getTransformer();
        transformer.transform(new DOMSource(document), result);
        return new StringSource(result.toString());
    } catch (Exception e) {
        throw new MessagingException("failed to create StringSource from document", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) MessagingException(org.springframework.messaging.MessagingException) StringResult(org.springframework.xml.transform.StringResult) StringSource(org.springframework.xml.transform.StringSource) MessagingException(org.springframework.messaging.MessagingException)

Example 2 with StringResult

use of org.springframework.xml.transform.StringResult in project spring-integration by spring-projects.

the class XPathMessageSplitter method splitDocument.

@SuppressWarnings("unchecked")
private Object splitDocument(Document document) throws Exception {
    Object nodes = splitNode(document);
    final Transformer transformer;
    synchronized (this.transformerFactory) {
        transformer = this.transformerFactory.newTransformer();
    }
    if (this.outputProperties != null) {
        transformer.setOutputProperties(this.outputProperties);
    }
    if (nodes instanceof List) {
        List<Node> items = (List<Node>) nodes;
        List<String> splitStrings = new ArrayList<String>(items.size());
        for (Node nodeFromList : items) {
            StringResult result = new StringResult();
            transformer.transform(new DOMSource(nodeFromList), result);
            splitStrings.add(result.toString());
        }
        return splitStrings;
    } else {
        return new TransformFunctionIterator((Iterator<Node>) nodes, node -> {
            StringResult result = new StringResult();
            try {
                transformer.transform(new DOMSource(node), result);
            } catch (TransformerException e) {
                throw new IllegalStateException("failed to create DocumentBuilder", e);
            }
            return result.toString();
        });
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) StringResult(org.springframework.xml.transform.StringResult) TransformerException(javax.xml.transform.TransformerException)

Example 3 with StringResult

use of org.springframework.xml.transform.StringResult in project spring-integration by spring-projects.

the class SimpleWebServiceOutboundGatewayTests method testDomPoxMessageFactory.

@Test
public void testDomPoxMessageFactory() throws Exception {
    String uri = "http://www.example.org";
    SimpleWebServiceOutboundGateway gateway = new SimpleWebServiceOutboundGateway(uri);
    gateway.setBeanFactory(mock(BeanFactory.class));
    final SettableListenableFuture<WebServiceMessage> requestFuture = new SettableListenableFuture<>();
    ClientInterceptorAdapter interceptorAdapter = new ClientInterceptorAdapter() {

        @Override
        public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
            requestFuture.set(messageContext.getRequest());
            return super.handleRequest(messageContext);
        }
    };
    gateway.setInterceptors(interceptorAdapter);
    gateway.setMessageFactory(new DomPoxMessageFactory());
    gateway.afterPropertiesSet();
    String request = "<test>foo</test>";
    try {
        gateway.handleMessage(new GenericMessage<>(request));
        fail("Expected MessageHandlingException");
    } catch (MessageHandlingException e) {
    // expected
    }
    WebServiceMessage requestMessage = requestFuture.get(10, TimeUnit.SECONDS);
    assertNotNull(requestMessage);
    assertThat(requestMessage, instanceOf(PoxMessage.class));
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringResult stringResult = new StringResult();
    transformer.transform(requestMessage.getPayloadSource(), stringResult);
    assertEquals(request, stringResult.toString());
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DomPoxMessageFactory(org.springframework.ws.pox.dom.DomPoxMessageFactory) ClientInterceptorAdapter(org.springframework.ws.client.support.interceptor.ClientInterceptorAdapter) MessageHandlingException(org.springframework.messaging.MessageHandlingException) WebServiceMessage(org.springframework.ws.WebServiceMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageContext(org.springframework.ws.context.MessageContext) StringResult(org.springframework.xml.transform.StringResult) PoxMessage(org.springframework.ws.pox.PoxMessage) Test(org.junit.Test)

Example 4 with StringResult

use of org.springframework.xml.transform.StringResult in project spring-integration by spring-projects.

the class SimpleWebServiceOutboundGatewayTests method testAttachments.

@Test
public void testAttachments() throws Exception {
    String uri = "http://www.example.org";
    SimpleWebServiceOutboundGateway gateway = new SimpleWebServiceOutboundGateway(uri);
    gateway.setBeanFactory(mock(BeanFactory.class));
    final SettableListenableFuture<WebServiceMessage> requestFuture = new SettableListenableFuture<>();
    ClientInterceptorAdapter interceptorAdapter = new ClientInterceptorAdapter() {

        @Override
        public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
            requestFuture.set(messageContext.getRequest());
            return super.handleRequest(messageContext);
        }
    };
    gateway.setInterceptors(interceptorAdapter);
    gateway.afterPropertiesSet();
    WebServiceMessageFactory messageFactory = new SaajSoapMessageFactory(MessageFactory.newInstance());
    MimeMessage webServiceMessage = (MimeMessage) messageFactory.createWebServiceMessage();
    String request = "<test>foo</test>";
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new StringSource(request), webServiceMessage.getPayloadResult());
    webServiceMessage.addAttachment("myAttachment", new ByteArrayResource("my_data".getBytes()), "text/plain");
    try {
        gateway.handleMessage(new GenericMessage<>(webServiceMessage));
        fail("Expected MessageHandlingException");
    } catch (MessageHandlingException e) {
    // expected
    }
    WebServiceMessage requestMessage = requestFuture.get(10, TimeUnit.SECONDS);
    assertNotNull(requestMessage);
    assertThat(requestMessage, instanceOf(MimeMessage.class));
    transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringResult stringResult = new StringResult();
    transformer.transform(requestMessage.getPayloadSource(), stringResult);
    assertEquals(request, stringResult.toString());
    Attachment myAttachment = ((MimeMessage) requestMessage).getAttachment("myAttachment");
    assertNotNull(myAttachment);
    assertEquals("text/plain", myAttachment.getContentType());
    assertEquals("my_data", StreamUtils.copyToString(myAttachment.getInputStream(), Charset.forName("UTF-8")));
}
Also used : WebServiceMessageFactory(org.springframework.ws.WebServiceMessageFactory) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Attachment(org.springframework.ws.mime.Attachment) SaajSoapMessageFactory(org.springframework.ws.soap.saaj.SaajSoapMessageFactory) ByteArrayResource(org.springframework.core.io.ByteArrayResource) ClientInterceptorAdapter(org.springframework.ws.client.support.interceptor.ClientInterceptorAdapter) MessageHandlingException(org.springframework.messaging.MessageHandlingException) WebServiceMessage(org.springframework.ws.WebServiceMessage) MimeMessage(org.springframework.ws.mime.MimeMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageContext(org.springframework.ws.context.MessageContext) StringSource(org.springframework.xml.transform.StringSource) StringResult(org.springframework.xml.transform.StringResult) Test(org.junit.Test)

Example 5 with StringResult

use of org.springframework.xml.transform.StringResult in project spring-integration by spring-projects.

the class SimpleWebServiceOutboundGateway method doHandle.

@Override
protected Object doHandle(String uri, final Message<?> requestMessage, final WebServiceMessageCallback requestCallback) {
    Object requestPayload = requestMessage.getPayload();
    Result responseResultInstance = null;
    if (requestPayload instanceof String) {
        responseResultInstance = new StringResult();
    } else if (requestPayload instanceof Document) {
        responseResultInstance = new DOMResult();
    }
    return getWebServiceTemplate().sendAndReceive(uri, new SimpleRequestMessageCallback(requestCallback, requestMessage), new SimpleResponseMessageExtractor(responseResultInstance));
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) StringResult(org.springframework.xml.transform.StringResult) Document(org.w3c.dom.Document) StringResult(org.springframework.xml.transform.StringResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult)

Aggregations

StringResult (org.springframework.xml.transform.StringResult)18 Transformer (javax.xml.transform.Transformer)8 DOMSource (javax.xml.transform.dom.DOMSource)7 Test (org.junit.Test)7 StringSource (org.springframework.xml.transform.StringSource)4 Document (org.w3c.dom.Document)3 Result (javax.xml.transform.Result)2 Source (javax.xml.transform.Source)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 DOMResult (javax.xml.transform.dom.DOMResult)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 StubStringResult (org.springframework.integration.xml.config.StubResultFactory.StubStringResult)2 MessageChannel (org.springframework.messaging.MessageChannel)2 MessageHandlingException (org.springframework.messaging.MessageHandlingException)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)2 WebServiceMessage (org.springframework.ws.WebServiceMessage)2 ClientInterceptorAdapter (org.springframework.ws.client.support.interceptor.ClientInterceptorAdapter)2 MessageContext (org.springframework.ws.context.MessageContext)2 IOException (java.io.IOException)1