In this article I will add a security header to the soap request. So I will create a class implementing the interface SOAPHandler and I will override the method handleMessage:
Now, I will add a simple call to this class in my implementation client:
@Override
public boolean handleMessage(SOAPMessageContext context) {
String prefixUri = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-";
String uri = prefixUri + "wssecurity-secext-1.0.xsd";
String uta = prefixUri + "wssecurity-utility-1.0.xsd";
String ta = prefixUri + "username-token-profile-1.0#PasswordText";
Boolean outboundProperty =
(Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
try {
SOAPEnvelope envelope = context.getMessage()
.getSOAPPart().getEnvelope();
SOAPFactory factory = SOAPFactory.newInstance();
String prefix = "wsse";
SOAPElement securityElem =
factory.createElement("Security",prefix,uri);
SOAPElement tokenElem =
factory.createElement("UsernameToken",prefix,uri);
tokenElem.addAttribute(QName.valueOf("wsu:Id"),"UsernameToken-2");
tokenElem.addAttribute(QName.valueOf("xmlns:wsu"), uta);
SOAPElement userElem =
factory.createElement("Username",prefix,uri);
userElem.addTextNode("myUser");
SOAPElement pwdElem =
factory.createElement("Password",prefix,uri);
pwdElem.addTextNode("myPwd");
pwdElem.addAttribute(QName.valueOf("Type"), ta);
tokenElem.addChildElement(userElem);
tokenElem.addChildElement(pwdElem);
securityElem.addChildElement(tokenElem);
SOAPHeader header = envelope.addHeader();
header.addChildElement(securityElem);
} catch (Exception e) {
System.out.println("Exception in handler: " + e);
}
}
return true;
}
Now, I will add a simple call to this class in my implementation client:
service.setHandlerResolver(new HandlerResolver() {
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerList = new ArrayList<Handler>();
handlerList.add(new MySoapHandler());
return handlerList;
}
});