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