Reputation: 1331
I want to send a soap message with Spring Integration. I use Java Config. I've tried the flolowing interceptor, but the spring integration converts the angle brackets (<>) into html escape characters.
import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.SoapMessage;
public class MyAuthInterceptor implements ClientInterceptor {
@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();
SoapHeader sh = soapMessage.getSoapHeader();
QName name = new QName("http://...", "myAuth", "aut");
sh.addHeaderElement(name).setText("<username>TestUser</username>" + "<password>TestPass</password>");
return true;
}
Here is the generated soap header:
<SOAP-ENV:Header>
<aut:myAuth xmlns:aut="http://.../"><username>TestUser</username><password>TestPass</password></aut:myAuth>
</SOAP-ENV:Header>
Here is my configuration:
@Configuration
@EnableIntegration
public class SpringIntegrationConfiguration {
@Bean
public PublishSubscribeChannel inputChannel() {
return new PublishSubscribeChannel();
}
@Bean
public ClientInterceptor myAuthInterceptor() {
return new MyAuthInterceptor();
}
@Bean
@ServiceActivator(inputChannel = "inputChannel")
public SimpleWebServiceOutboundGateway myOutput(ClientInterceptor mekAuthInterceptor) {
SimpleWebServiceOutboundGateway simpleWebServiceOutboundGateway = new SimpleWebServiceOutboundGateway("http://...");
simpleWebServiceOutboundGateway.setInterceptors(myAuthInterceptor);
return simpleWebServiceOutboundGateway;
}
}
How can I set the soap header without escaping the angle brackets?
Upvotes: 0
Views: 1072
Reputation: 41
@gary-russell - Your answer made sense from a logical point of view, but there are no "addChildElement" method in org.springframework.ws.soap.. I found them in javax.xml.soap..
So my result looks like the following:
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
public class MySecurityInterceptor implements ClientInterceptor
{
private static final Logger log = LoggerFactory.getLogger(MySecurityInterceptor.class);
@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException
{
SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();
QName securityName = new QName(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
"Security",
XMLConstants.DEFAULT_NS_PREFIX);
QName usernameToken = new QName(null, "UsernameToken", XMLConstants.DEFAULT_NS_PREFIX);
QName username = new QName(null, "Username", XMLConstants.DEFAULT_NS_PREFIX);
QName password = new QName(null, "Password", XMLConstants.DEFAULT_NS_PREFIX);
try
{
SOAPMessage mySoapMessage = ((SaajSoapMessage) soapMessage).getSaajMessage();
SOAPHeader header = mySoapMessage.getSOAPHeader();
SOAPHeaderElement securityElement = header.addHeaderElement(securityName);
SOAPElement usernameTokenElement = securityElement.addChildElement(usernameToken);
SOAPElement usernameElement = usernameTokenElement.addChildElement(username);
SOAPElement passwordElement = usernameTokenElement.addChildElement(password);
usernameElement.setTextContent("[email protected]");
passwordElement.setTextContent("3Sg%T~1q4z!QnH6#+5pD");
}
catch (SOAPException e)
{
log.error("Error!", e);
}
return true;
}
@Override
public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException
{
// Auto-generated method stub
return false;
}
@Override
public boolean handleFault(MessageContext messageContext) throws WebServiceClientException
{
// Auto-generated method stub
return false;
}
@Override
public void afterCompletion(MessageContext messageContext, Exception ex) throws WebServiceClientException
{
// Auto-generated method stub
}
}
Upvotes: 0
Reputation: 174739
You have to build it up using addChildElement
s instead of setting it as text.
Upvotes: 1
Reputation: 104
You are setting the tags as text which escapes the string since it is being added to an xml. Those needs to be set as elements
http://docs.oracle.com/javaee/5/api/javax/xml/soap/SOAPHeaderElement.html
Check the methods in the doc above and use it appropriately. Leave a comment in case you need more help.
Upvotes: 0