Search in sources :

Example 6 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 7 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)

Example 8 with StringSource

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

the class JaxbMarshallingIntegrationTests method testUnmarshalling.

@SuppressWarnings("unchecked")
@Test
public void testUnmarshalling() throws Exception {
    StringSource source = new StringSource("<person><firstname>bob</firstname></person>");
    unmarshallIn.send(new GenericMessage<Source>(source));
    GenericMessage<Object> res = (GenericMessage<Object>) unmarshallOut.receive(2000);
    assertNotNull("No response", res);
    assertTrue("Not a Person ", res.getPayload() instanceof JaxbAnnotatedPerson);
    JaxbAnnotatedPerson person = (JaxbAnnotatedPerson) res.getPayload();
    assertEquals("Worng firstname", "bob", person.getFirstName());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) StringSource(org.springframework.xml.transform.StringSource) StringSource(org.springframework.xml.transform.StringSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 9 with StringSource

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

the class StringSourceTests method testWithDocument.

@Test
public void testWithDocument() throws Exception {
    String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>";
    Document doc = XmlTestUtil.getDocumentForString(docString);
    StringSource source = (StringSource) sourceFactory.createSource(doc);
    BufferedReader reader = new BufferedReader(source.getReader());
    String docAsString = reader.readLine();
    assertXMLEqual("Wrong content in StringSource", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>", docAsString);
}
Also used : BufferedReader(java.io.BufferedReader) StringSource(org.springframework.xml.transform.StringSource) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 10 with StringSource

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

the class UnmarshallingTransformerTests method testStringSourceToString.

@Test
public void testStringSourceToString() {
    Unmarshaller unmarshaller = new TestUnmarshaller(false);
    UnmarshallingTransformer transformer = new UnmarshallingTransformer(unmarshaller);
    Object transformed = transformer.transformPayload(new StringSource("world"));
    assertEquals(String.class, transformed.getClass());
    assertEquals("hello world", transformed.toString());
}
Also used : StringSource(org.springframework.xml.transform.StringSource) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.Test)

Aggregations

StringSource (org.springframework.xml.transform.StringSource)22 Test (org.junit.Test)15 Source (javax.xml.transform.Source)7 Transformer (javax.xml.transform.Transformer)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 HashMap (java.util.HashMap)1