Search in sources :

Example 6 with SoapHeader

use of org.springframework.ws.soap.SoapHeader in project spring-integration by spring-projects.

the class DefaultSoapHeaderMapper method extractUserDefinedHeaders.

@Override
protected Map<String, Object> extractUserDefinedHeaders(SoapMessage source) {
    Map<String, Object> headers = new HashMap<String, Object>();
    SoapHeader soapHeader = source.getSoapHeader();
    if (soapHeader != null) {
        Iterator<?> attributeIter = soapHeader.getAllAttributes();
        while (attributeIter.hasNext()) {
            Object name = attributeIter.next();
            if (name instanceof QName) {
                String qnameString = QNameUtils.toQualifiedName((QName) name);
                String value = soapHeader.getAttributeValue((QName) name);
                if (value != null) {
                    headers.put(qnameString, value);
                }
            }
        }
        Iterator<?> elementIter = soapHeader.examineAllHeaderElements();
        while (elementIter.hasNext()) {
            Object element = elementIter.next();
            if (element instanceof SoapHeaderElement) {
                QName qname = ((SoapHeaderElement) element).getName();
                String qnameString = QNameUtils.toQualifiedName(qname);
                headers.put(qnameString, element);
            }
        }
    }
    return headers;
}
Also used : SoapHeaderElement(org.springframework.ws.soap.SoapHeaderElement) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) SoapHeader(org.springframework.ws.soap.SoapHeader)

Example 7 with SoapHeader

use of org.springframework.ws.soap.SoapHeader 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)

Example 8 with SoapHeader

use of org.springframework.ws.soap.SoapHeader in project spring-integration by spring-projects.

the class DefaultSoapHeaderMapperTests method testFromHeadersToRequest.

@Test
public void testFromHeadersToRequest() throws SOAPException {
    DefaultSoapHeaderMapper mapper = new DefaultSoapHeaderMapper();
    mapper.setReplyHeaderNames("foo", "auth", "myHeader");
    Map<String, Object> headers = new HashMap<>();
    headers.put("foo", "bar");
    String docString = "<auth xmlns='http://test.auth.org'>" + "<username>user</username>" + "<password>pass</password>" + "</auth>";
    Source source = new StringSource(docString);
    headers.put("auth", source);
    headers.put("myHeader", new StringSource("<test xmlns='http://test.org'>TEST</test>"));
    SaajSoapMessage message = new SaajSoapMessage(MessageFactory.newInstance().createMessage());
    mapper.fromHeadersToReply(new MessageHeaders(headers), message);
    SoapHeader soapHeader = message.getSoapHeader();
    assertEquals("bar", soapHeader.getAttributeValue(QNameUtils.parseQNameString("foo")));
    Iterator<SoapHeaderElement> authIterator = soapHeader.examineHeaderElements(QNameUtils.parseQNameString("{http://test.auth.org}auth"));
    assertTrue(authIterator.hasNext());
    SoapHeaderElement auth = authIterator.next();
    DOMSource authSource = (DOMSource) auth.getSource();
    NodeList nodeList = authSource.getNode().getChildNodes();
    assertEquals("username", nodeList.item(0).getNodeName());
    assertEquals("user", nodeList.item(0).getFirstChild().getNodeValue());
    assertEquals("password", nodeList.item(1).getNodeName());
    assertEquals("pass", nodeList.item(1).getFirstChild().getNodeValue());
    Iterator<SoapHeaderElement> testIterator = soapHeader.examineHeaderElements(QNameUtils.parseQNameString("{http://test.org}test"));
    assertTrue(testIterator.hasNext());
/*StringResult stringResult = new StringResult();
		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.transform(message.getEnvelope().getSource(), stringResult);
		System. out. println(stringResult.toString());*/
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SoapHeaderElement(org.springframework.ws.soap.SoapHeaderElement) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StringSource(org.springframework.xml.transform.StringSource) SaajSoapMessage(org.springframework.ws.soap.saaj.SaajSoapMessage) SoapHeader(org.springframework.ws.soap.SoapHeader) StringSource(org.springframework.xml.transform.StringSource) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Aggregations

SoapHeader (org.springframework.ws.soap.SoapHeader)8 QName (javax.xml.namespace.QName)5 SoapHeaderElement (org.springframework.ws.soap.SoapHeaderElement)4 SoapMessage (org.springframework.ws.soap.SoapMessage)4 SaajSoapMessage (org.springframework.ws.soap.saaj.SaajSoapMessage)3 HashMap (java.util.HashMap)2 Source (javax.xml.transform.Source)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URI (java.net.URI)1 HashSet (java.util.HashSet)1 Result (javax.xml.transform.Result)1 TransformerException (javax.xml.transform.TransformerException)1 DOMSource (javax.xml.transform.dom.DOMSource)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 AbstractWebServiceOutboundGateway (org.springframework.integration.ws.AbstractWebServiceOutboundGateway)1 MessageChannel (org.springframework.messaging.MessageChannel)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 WebServiceMessage (org.springframework.ws.WebServiceMessage)1 WebServiceMessageFactory (org.springframework.ws.WebServiceMessageFactory)1