Sunday, September 8, 2013

SOAP handler with SAML Authentication


SOAP Handler
A SOAP message handler provides a mechanism for intercepting the SOAP message in both the request and response of the Web Service. You can create handlers in both the Web Service itself and the client applications that invoke the Web Service.
A simple example of using handlers is to access information in the header part of the SOAP message. You can use the SOAP header to store Web Service specific information and then use handlers to manipulate it.
You can also use SOAP message handlers to improve the performance of your Web Service. After your Web Service has been deployed for a while, you might discover that many consumers invoke it with the same parameters. You could improve the performance of your Web Service by caching the results of popular invokes of the Web Service (assuming the results are static) and immediately returning these results when appropriate, without ever invoking the back-end components that implement the Web Service. You implement this performance improvement by using handlers to check the request SOAP message to see if it contains the popular parameters.

Sample implementation of client handler with SAMP authentication - 

 public class SampleClientHandler implements Handler {

private final String CLASSNAME = "SampleClientHandler ";
public void destroy() {
// TODO Auto-generated method stub

}

public QName[] getHeaders() {
// TODO Auto-generated method stub
return null;
}

public boolean handleFault(MessageContext arg0) {
// TODO Auto-generated method stub
return false;
}

/**
 * Method to add the security part for saml in the soap header 
 */
public boolean handleRequest(MessageContext messageContext) {
try {
SOAPFactory soapFactory =SOAPFactory.newInstance();
SOAPElement security=soapFactory.createElement("Security", "wsse", ns);

String authNS = "urn:oasis:names:tc:SAML:1.0:assertion";
SOAPElement usernameToken = security.addChildElement("UsernameToken",                                            "wsse",authNS);
Name wsuId  = soapFactory.createName("wsu:Id");
Name xmlnsWsu  = soapFactory.createName("xmlns:wsu");
if(usernameToken==null){
System.out.println( "usernameToken ==null ");
}
usernameToken.addAttribute(wsuId, "UsernameToken-72");
usernameToken.addAttribute(xmlnsWsu, "xsd location");

SOAPElement userName = usernameToken.addChildElement("Username",                                                  "wsse",authNS);
userName.addTextNode(username);
SOAPElement password = usernameToken.addChildElement("Password", "wsse",authNS);
Name type  = soapFactory.createName("Type");
password.addAttribute(type,"PWD");

SOAPElement assertion = security.addChildElement("Assertion",                                                                 "ns1","urn:oasis:names:tc:SAML:1.0:assertion");
Name assertionID = soapFactory.createName("AssertionID");
Name issueInstant = soapFactory.createName("IssueInstant");
Name issuer = soapFactory.createName("Issuer");
Name majorVersion = soapFactory.createName("MajorVersion");
Name minorVersion = soapFactory.createName("MinorVersion");

assertion.addAttribute(assertionID, "disputes");
assertion.addAttribute(issueInstant, strSysdate);
assertion.addAttribute(issuer, "xyz.com");
assertion.addAttribute(majorVersion, "1");
assertion.addAttribute(minorVersion, "1");


SOAPElement authenticationStatement =                                                                                                      assertion.addChildElement("AuthenticationStatement", "ns1",authNS);
Name authenticationInstant = soapFactory.createName("AuthenticationInstant");
Name authenticationMethod = soapFactory.createName("AuthenticationMethod");
authenticationStatement.addAttribute(authenticationInstant, strSysdate);
authenticationStatement.addAttribute(authenticationMethod,                                                                          "urn:oasis:names:tc:SAML:1.0:am:password");

SOAPElement subject = authenticationStatement.addChildElement("Subject",                                                "ns1",authNS);
SOAPElement nameIdentifier = subject.addChildElement("NameIdentifier", "ns1",authNS);

nameIdentifier.addTextNode(strNameIdentifier);

SOAPElement subjectConfirmation = subject.addChildElement("SubjectConfirmation",                                  "ns1",authNS);
SOAPElement confirmationMethod =                                                                                                           subjectConfirmation.addChildElement("ConfirmationMethod","ns1",authNS);
confirmationMethod.addTextNode("urn:oasis:names:tc:SAML:1.0:cm:sender-vouches");

SOAPMessageContext soapMessageContext = (SOAPMessageContext)messageContext;
SOAPEnvelope soapEnvelop                                                                                                                       soapMessageContext.getMessage().getSOAPPart().getEnvelope();
soapEnvelop.getHeader().addChildElement(security);

return true;

} catch (SOAPException e) {
e.printstacktrace();
}
               return false;
}

public boolean handleResponse(MessageContext messageContext) {
//Log the respone received
SOAPMessageContext soapMessageContext = (SOAPMessageContext)messageContext;
logToSystem(soapMessageContext);

return false;
}

public void init(HandlerInfo arg0) {
// TODO Auto-generated method stub
}

/**
 * Method to log the resquest or response to the system.out printstream
 * @param soapMessageContext
 * @throws SOAPException
 * @throws IOException
 */
public void logToSystem(SOAPMessageContext soapMessageContext){

try {
SOAPMessage soapMessage  =soapMessageContext.getMessage();

soapMessage.writeTo(System.out);
//Log to logger file
/*ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
soapMessage.writeTo(byteArrayOutputStream);
String strXml = byteArrayOutputStream.toString();

logger.info(strXml);*/


} catch (Exception e) {
e.printstacktrace();
}

}

Creating unmodifiable static map



Unmodifiable static map


public static final Map unmodifiableMap = Collections.unmodifiableMap(
new HashMap(){{
    put("key1","value1");
    put("key2","value2");
}}
);