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);
}
}
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);
}
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);
}
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")));
}
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());
}
Aggregations