Search in sources :

Example 1 with StringSource

use of org.springframework.xml.transform.StringSource 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 StringSource

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

the class UnmarshallingTransformerParserTests method testUnmarshallString.

@Test
public void testUnmarshallString() throws Exception {
    MessageChannel input = (MessageChannel) appContext.getBean("input");
    PollableChannel output = (PollableChannel) appContext.getBean("output");
    GenericMessage<Object> message = new GenericMessage<Object>("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>");
    input.send(message);
    Message<?> result = output.receive(0);
    assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
    assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) StringSource(org.springframework.xml.transform.StringSource) Test(org.junit.Test)

Example 3 with StringSource

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

the class UnmarshallingTransformerParserTests method testDefaultUnmarshall.

@Test
public void testDefaultUnmarshall() throws Exception {
    MessageChannel input = (MessageChannel) appContext.getBean("input");
    PollableChannel output = (PollableChannel) appContext.getBean("output");
    GenericMessage<Object> message = new GenericMessage<Object>(new StringSource("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
    input.send(message);
    Message<?> result = output.receive(0);
    assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
    assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) StringSource(org.springframework.xml.transform.StringSource) Test(org.junit.Test)

Example 4 with StringSource

use of org.springframework.xml.transform.StringSource 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 StringSource

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

the class WebServiceInboundGatewayJavaConfigTests method testWebServiceInboundGatewayJavaConfig.

@Test
public void testWebServiceInboundGatewayJavaConfig() throws Exception {
    MessageContext context = mock(MessageContext.class);
    SoapMessage request = mock(SoapMessage.class);
    SoapMessage response = mock(SoapMessage.class);
    SoapBody soapBody = mock(SoapBody.class);
    String input = "<hello/>";
    Source payloadSource = new StringSource(input);
    StringWriter output = new StringWriter();
    Result payloadResult = new StreamResult(output);
    when(context.getResponse()).thenReturn(response);
    when(response.getPayloadResult()).thenReturn(payloadResult);
    when(response.getSoapBody()).thenReturn(soapBody);
    when(context.getRequest()).thenReturn(request);
    when(request.getPayloadSource()).thenReturn(payloadSource);
    this.messageReceiver.receive(context);
    verify(soapBody).addServerOrReceiverFault(eq("503 Service Unavailable"), any(Locale.class));
    this.wsGateway.start();
    this.messageReceiver.receive(context);
    assertTrue(output.toString().endsWith(input));
    context = mock(MessageContext.class);
    request = mock(SoapMessage.class);
    payloadSource = new StringSource("<order/>");
    when(context.getRequest()).thenReturn(request);
    when(request.getPayloadSource()).thenReturn(payloadSource);
    this.messageReceiver.receive(context);
    Message<?> receive = this.webserviceRequestsQueue.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(Element.class));
    Element order = (Element) receive.getPayload();
    assertEquals("order", order.getLocalName());
}
Also used : Locale(java.util.Locale) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) SoapBody(org.springframework.ws.soap.SoapBody) MessageContext(org.springframework.ws.context.MessageContext) StringSource(org.springframework.xml.transform.StringSource) Source(javax.xml.transform.Source) StringSource(org.springframework.xml.transform.StringSource) SoapMessage(org.springframework.ws.soap.SoapMessage) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) Test(org.junit.Test)

Aggregations

StringSource (org.springframework.xml.transform.StringSource)27 Test (org.junit.Test)15 Source (javax.xml.transform.Source)7 Transformer (javax.xml.transform.Transformer)5 Test (org.junit.jupiter.api.Test)5 DOMSource (javax.xml.transform.dom.DOMSource)4 GenericMessage (org.springframework.messaging.support.GenericMessage)4 StringResult (org.springframework.xml.transform.StringResult)4 BufferedReader (java.io.BufferedReader)3 IOException (java.io.IOException)3 MessageChannel (org.springframework.messaging.MessageChannel)3 PollableChannel (org.springframework.messaging.PollableChannel)3 Unmarshaller (org.springframework.oxm.Unmarshaller)3 StringWriter (java.io.StringWriter)2 StreamResult (javax.xml.transform.stream.StreamResult)2 XmlMappingException (org.springframework.oxm.XmlMappingException)2 WebServiceMessage (org.springframework.ws.WebServiceMessage)2 MessageContext (org.springframework.ws.context.MessageContext)2 Document (org.w3c.dom.Document)2 ServiceException (com.consol.citrus.samples.incident.exception.ServiceException)1