Search in sources :

Example 1 with DomPoxMessageFactory

use of org.springframework.ws.pox.dom.DomPoxMessageFactory 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 2 with DomPoxMessageFactory

use of org.springframework.ws.pox.dom.DomPoxMessageFactory in project camel by apache.

the class BasicMessageFilterTest method nonSoapMessageShouldBeSkipped.

@Test
public void nonSoapMessageShouldBeSkipped() throws Exception {
    DomPoxMessage domPoxMessage = new DomPoxMessageFactory().createWebServiceMessage();
    filter.filterConsumer(exchange, domPoxMessage);
    filter.filterProducer(exchange, domPoxMessage);
}
Also used : DomPoxMessage(org.springframework.ws.pox.dom.DomPoxMessage) DomPoxMessageFactory(org.springframework.ws.pox.dom.DomPoxMessageFactory) Test(org.junit.Test)

Example 3 with DomPoxMessageFactory

use of org.springframework.ws.pox.dom.DomPoxMessageFactory in project spring-integration-samples by spring-projects.

the class InboundGatewayTests method testSendAndReceive.

/**
 * Emulate the Spring WS MessageDispatcherServlet by calling the gateway
 * with a DOMSource object representing the payload of the original SOAP
 * 'echoRequest' message.  Expect an 'echoResponse' DOMSource object
 * to be returned in synchronous fashion, which the MessageDispatcherServlet
 * would in turn wrap in a SOAP envelope and return to the client.
 */
@Test
public void testSendAndReceive() throws Exception {
    String xml = "<echoRequest xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoRequest>";
    DomPoxMessageFactory messageFactory = new DomPoxMessageFactory();
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(new InputSource(new StringReader(xml)));
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DomPoxMessage request = new DomPoxMessage(document, transformer, "text/xml");
    MessageContext messageContext = new DefaultMessageContext(request, messageFactory);
    gateway.invoke(messageContext);
    Object reply = messageContext.getResponse().getPayloadSource();
    assertThat(reply, is(instanceOf(DOMSource.class)));
    DOMSource replySource = (DOMSource) reply;
    Element element = (Element) replySource.getNode().getFirstChild();
    assertThat(element.getTagName(), equalTo("echoResponse"));
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) Element(org.w3c.dom.Element) DomPoxMessageFactory(org.springframework.ws.pox.dom.DomPoxMessageFactory) Document(org.w3c.dom.Document) DomPoxMessage(org.springframework.ws.pox.dom.DomPoxMessage) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) DefaultMessageContext(org.springframework.ws.context.DefaultMessageContext) DefaultMessageContext(org.springframework.ws.context.DefaultMessageContext) MessageContext(org.springframework.ws.context.MessageContext) Test(org.junit.Test)

Example 4 with DomPoxMessageFactory

use of org.springframework.ws.pox.dom.DomPoxMessageFactory in project spring-integration by spring-projects.

the class WebServiceOutboundGatewayWithHeaderMapperTests method process.

private Message<?> process(Object payload, String gatewayName, String channelName, final boolean soap) throws Exception {
    AbstractWebServiceOutboundGateway gateway = TestUtils.getPropertyValue(this.context.getBean(gatewayName), "handler", AbstractWebServiceOutboundGateway.class);
    if (!soap) {
        WebServiceTemplate template = TestUtils.getPropertyValue(gateway, "webServiceTemplate", WebServiceTemplate.class);
        template.setMessageFactory(new DomPoxMessageFactory());
    }
    WebServiceMessageSender messageSender = Mockito.mock(WebServiceMessageSender.class);
    WebServiceConnection wsConnection = Mockito.mock(WebServiceConnection.class);
    Mockito.when(messageSender.createConnection(Mockito.any(URI.class))).thenReturn(wsConnection);
    Mockito.when(messageSender.supports(Mockito.any(URI.class))).thenReturn(true);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        WebServiceMessage wsMessage = (WebServiceMessage) args[0];
        // }
        if (soap) {
            SoapHeader soapHeader = ((SoapMessage) wsMessage).getSoapHeader();
            assertNotNull(soapHeader.getAttributeValue(QNameUtils.parseQNameString("foo")));
            assertNotNull(soapHeader.getAttributeValue(QNameUtils.parseQNameString("foobar")));
            assertNotNull(soapHeader.getAttributeValue(QNameUtils.parseQNameString("abaz")));
            assertNull(soapHeader.getAttributeValue(QNameUtils.parseQNameString("bar")));
        }
        return null;
    }).when(wsConnection).send(Mockito.any(WebServiceMessage.class));
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        WebServiceMessageFactory factory = (WebServiceMessageFactory) args[0];
        String responseMessage = factory instanceof SoapMessageFactory ? responseSoapMessage : responseNonSoapMessage;
        WebServiceMessage wsMessage = factory.createWebServiceMessage(new ByteArrayInputStream(responseMessage.getBytes()));
        if (soap) {
            ((SoapMessage) wsMessage).getSoapHeader().addAttribute(QNameUtils.parseQNameString("bar"), "bar");
            ((SoapMessage) wsMessage).getSoapHeader().addAttribute(QNameUtils.parseQNameString("baz"), "baz");
        }
        // }
        return wsMessage;
    }).when(wsConnection).receive(Mockito.any(WebServiceMessageFactory.class));
    gateway.setMessageSender(messageSender);
    MessageChannel inputChannel = context.getBean(channelName, MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload(payload).setHeader("foo", "foo").setHeader("foobar", "foobar").setHeader("abaz", "abaz").setHeader("bar", "bar").setHeader(WebServiceHeaders.SOAP_ACTION, "someAction").build();
    inputChannel.send(message);
    QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
    return outputChannel.receive(0);
}
Also used : WebServiceMessageFactory(org.springframework.ws.WebServiceMessageFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) DomPoxMessageFactory(org.springframework.ws.pox.dom.DomPoxMessageFactory) WebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate) URI(java.net.URI) SoapMessage(org.springframework.ws.soap.SoapMessage) WebServiceMessage(org.springframework.ws.WebServiceMessage) SoapMessageFactory(org.springframework.ws.soap.SoapMessageFactory) WebServiceMessageSender(org.springframework.ws.transport.WebServiceMessageSender) MessageChannel(org.springframework.messaging.MessageChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) SoapHeader(org.springframework.ws.soap.SoapHeader) AbstractWebServiceOutboundGateway(org.springframework.integration.ws.AbstractWebServiceOutboundGateway) WebServiceConnection(org.springframework.ws.transport.WebServiceConnection)

Aggregations

DomPoxMessageFactory (org.springframework.ws.pox.dom.DomPoxMessageFactory)4 Test (org.junit.Test)3 Transformer (javax.xml.transform.Transformer)2 WebServiceMessage (org.springframework.ws.WebServiceMessage)2 MessageContext (org.springframework.ws.context.MessageContext)2 DomPoxMessage (org.springframework.ws.pox.dom.DomPoxMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringReader (java.io.StringReader)1 URI (java.net.URI)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 AbstractWebServiceOutboundGateway (org.springframework.integration.ws.AbstractWebServiceOutboundGateway)1 MessageChannel (org.springframework.messaging.MessageChannel)1 MessageHandlingException (org.springframework.messaging.MessageHandlingException)1 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)1 WebServiceMessageFactory (org.springframework.ws.WebServiceMessageFactory)1