MySoapHandler

import java.util.Set;

import javax.annotation.PostConstruct;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import javax.xml.ws.soap.SOAPFaultException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@Component
public class MySoapHandler implements SOAPHandler<SOAPMessageContext> {

private final String VALID_PROPERTY = “RANDOM”;
@Override
public boolean handleMessage(SOAPMessageContext context) {

System.out.println(“Server executing SOAP Handler”);

System.out.println(context);
Boolean outBoundProperty = (Boolean) context
.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

// if this is an incoming message from the client
if (!outBoundProperty) {

try {

// Get the SOAP Message and grab the headers
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();

NodeList nl = soapHeader.getChildNodes();
for ( int i=0; i< nl.getLength();i++)
{
Node node = nl.item(i);
String nodeName = node.getNodeName();
String nodeLocalName= node.getLocalName();
//
String nodeTextContent= node.getTextContent();

if ( nodeLocalName.equalsIgnoreCase(“AuthToken”) || nodeName.equalsIgnoreCase(“ffi:AuthToken”) )
{
System.out.println(“nodeName:”+nodeName);
System.out.println(“nodeLocalName:”+nodeLocalName);
System.out.println(“nodeTextContent:”+nodeTextContent);
//Call Securoty Controller
String userId = callSecurityController(nodeTextContent);
//SimpleExceptionBean simpleExceptionBean = new SimpleExceptionBean()l
if ( null == userId)
{

/*System.out.println(“Null vlaue”);
throw new RuntimeException(“Invalid OBSSOCookie”);*/

generateSOAPErrMessage(soapMsg, “Invalid OBSSOCookie”, “Null vlaue”, “Null vlaue”);
//return false;

}else if (!userId.equalsIgnoreCase(“wmg11”) )
{
//throw new SimpleException(“Invalid User”);
System.out.println(“Not Proceed”);
return false;
}

}
}

} catch (SOAPException e) {
System.err.println(e);
}

}

return true;
}

private void generateSOAPErrMessage(SOAPMessage msg, String code,
String reason, String detail) {
try {
SOAPBody soapBody = msg.getSOAPPart().getEnvelope().getBody();
SOAPFault soapFault = soapBody.addFault();
soapFault.setFaultCode(code);
soapFault.setFaultString(reason);

// Manually crate a failure element in order to guarentee that this
// authentication handler returns the same type of soap fault as the
// rest
// of the application
QName failureElement = new QName(
http://yournamespacehere.com&#8221;, “Failure”, “ns3”);
QName codeElement = new QName(“Code”);
QName reasonElement = new QName(“Reason”);
QName detailElement = new QName(“Detail”);

soapFault.addDetail().addDetailEntry(failureElement)
.addChildElement(codeElement).addTextNode(code)
.getParentElement().addChildElement(reasonElement)
.addTextNode(reason).getParentElement()
.addChildElement(detailElement).addTextNode(detail);

throw new SOAPFaultException(soapFault);
} catch (SOAPException e) {
}
}
/*@Override
public Set<QName> getHeaders() {
System.out.println(“Inside SOAP handler of get Headers”);
QName securityUsernameHeader = new QName(“urn:com.intertech.secty”,
“AuthToken”);
QName securityPasswordHeader = new QName(“urn:com.intertech.secty”,
“password”);
HashSet<QName> headers = new HashSet<QName>();
headers.add(securityUsernameHeader);
//.add(securityPasswordHeader);
System.out.println(“got Headers: ” + headers);
return headers;
}
*/

@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}

@Override
public void close(MessageContext context) {
}

@Override
public Set<QName> getHeaders() {
return null;
}

@PostConstruct
public void init() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s