wrap the web service soap response to a self defined object
package com.metlife.ins.product.metguard.service.impl;
import java.util.HashMap;
import java.util.Map;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
import com.metlife.ins.product.metguard.framework.exception.ProxyConfigurationException;
import com.metlife.ins.product.metguard.model.account.ProfileModel;
/**
* This class is used to handle a get profile response element from a Web Service Proxy.
* The use's profile can be get from GetProfileResponse instance.
* Copyright: Copyright (c) 2011
* @author: cognizant
* @version 1.0
*/
public class GetProfileResponse extends ProxyResponse{
/**
* the SM element
*/
private Element sm;
/**
* the return Code
* returnCode = -1,SOAP-ENV:Fault. The user does not exist.
* returnCode = -2, response xml schema maybe changed. XML format error.
* returnCode = 0, OK.
*/
private int returnCode;
/**
* the return text
*/
private String returnText;
/**
* The map to store the profile info
*/
private Map<String, String> profileMap = null;
/**
* Overloaded Constructor that take a response element as an input argument
*/
public GetProfileResponse(Element response) {
super(response);
try {
if ("SOAP-ENV:Fault".equals(response.getNodeName()) == true) {
// get the faultcode
this.returnCode = -1;//XPathAPI.selectNodeIterator(response, "faultcode").nextNode().getFirstChild().getNodeValue();
// get the faultstring
this.returnText = XPathAPI.selectNodeIterator(response, "faultstring").nextNode().getFirstChild().getNodeValue();
//log the error
}else{
profileMap = new HashMap<String, String>();
// get the SM element
NodeIterator nodes = XPathAPI.selectNodeIterator(this.response,"SM");
this.sm = (Element)nodes.nextNode();
Node state = this.sm.getFirstChild();
NodeList stateChilNodelist = state.getChildNodes();
for (int i=0; i<stateChilNodelist.getLength(); i++) {
Node node = (Node)stateChilNodelist.item(i);
if ("CD".equalsIgnoreCase(node.getNodeName())) {
this.returnCode =Integer.parseInt(node.getFirstChild()==null?"-2":node.getFirstChild().getNodeValue());
}else if("TXT".equalsIgnoreCase(node.getNodeName())){
this.returnText =node.getFirstChild()==null?"": node.getFirstChild().getNodeValue();
}
}
/*
* Get the role id from response and put it into profile map.
*/
if(this.returnCode == 0){
Node Sm = response.getFirstChild();
NodeList chilNodelist = Sm.getChildNodes();
for (int i=0; i<chilNodelist.getLength(); i++) {
Node node = (Node)chilNodelist.item(i);
if ("U".equalsIgnoreCase(node.getNodeName())) {
setNodeData(node);
}else if ("R".equalsIgnoreCase(node.getNodeName())) {
profileMap.put(node.getNodeName(),node.getFirstChild()==null?"":
node.getFirstChild().getNodeValue());
}
}
}else{
this.returnText = "User does not exist.";
}
}
}
catch(javax.xml.transform.TransformerException e) {
throw new ProxyConfigurationException(e, "Error in GetProfileResponse. Probably xml format is incorrect");
}
}
/**
* Put the node data to the profile map. Key= node name, value = node value.
* @param nodeParam
*/
private void setNodeData(Node nodeParam){
NodeList chilNodelist = nodeParam.getChildNodes();
for (int i=0; i<chilNodelist.getLength(); i++) {
Node node = (Node)chilNodelist.item(i);
if (node.getFirstChild()!=null) {
profileMap.put(node.getNodeName(),node.getFirstChild().getNodeValue());
}
}
}
public ProfileModel getProfile(){
if(this.profileMap==null){
return null;
}else{
ProfileModel profile = new ProfileModel();
profile.setUserName(this.profileMap.get("UID")==null?"":this.profileMap.get("UID"));
profile.setFirstName(this.profileMap.get("FN")==null?"":this.profileMap.get("FN"));
profile.setLastName(this.profileMap.get("LN")==null?"":this.profileMap.get("LN"));
profile.setAddress(this.profileMap.get("ADR1")==null?"":this.profileMap.get("ADR1"));
profile.setEmailAddr(this.profileMap.get("EM")==null?"":this.profileMap.get("EM"));
profile.setMetRefId(this.profileMap.get("MID")==null?"":this.profileMap.get("MID"));
profile.setTin(this.profileMap.get("SSN")==null?"":this.profileMap.get("SSN"));
profile.setRoleId(this.profileMap.get("R")==null?"":this.profileMap.get("R"));
profile.setPwdStatusCode(this.profileMap.get("PSCD")==null?"":this.profileMap.get("PSCD"));
return profile;
}
}
public int getReturnCode() {
return returnCode;
}
public String getReturnText() {
return returnText;
}
}
package com.metlife.ins.product.metguard.service.impl;
import org.w3c.dom.Element;
/**
* Base class for all documents that are used to handle a response from a Web Service Proxy.
* Copyright: Copyright (c) 2011
* @author: cognizant
* @version 1.0
*/
public abstract class ProxyResponse extends ProxyDocument {
/**
* the root element of the response
*/
protected Element response;
/**
* Overloaded Constructor that take a response element as an input argument
*/
protected ProxyResponse(Element response) {
// set the response
this.response = response;
}
/**
* returns xml as a string but does not add a linefeed after each element
* or indent each child element
*/
public String getString() {
return getString(this.response, -1);
}
/**
* Returns xml as a formatted string. Each element is on an new line
* and the child elements are indented.
*/
public String toString() {
return getString(this.response, 0);
}
/**
* returns the response element of this class
*/
public Element getResponseElement() {
return this.response;
}
public abstract int getReturnCode();
public abstract String getReturnText();
}
package com.metlife.ins.product.metguard.service.impl;
import java.io.PrintWriter;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Abstract class for a document that is used by a Web Service Proxy. All this class
* has is methods that will render an org.w3c.dom.Node to a PrintWriter in a readable format.
* Copyright: Copyright (c) 2011
* @author: cognizant
* @version 1.0
*/
public abstract class ProxyDocument {
protected ProxyDocument() {
}
public String get(Node node) {
return getString(node, 0);
}
// return the string format of the Node and it's child nodes
protected String getString(Node node, int indent) {
// convert to a string
java.io.StringWriter writer = new java.io.StringWriter();
java.io.PrintWriter out = new java.io.PrintWriter(writer);
print(node, indent, out);
return writer.toString();
}
/**
* Prints the specified node, recursively. If the indent value is one
* then the output will not be formated with element on newlines and child elements
* embeded
*/
protected void print(Node node, int indent, PrintWriter out) {
boolean lBreak = false;
String strIndent = " ";
// is there anything to do?
if (node == null) {
return;
}
//"Debug# nodetype= " + node.getNodeType() + " name= " + node.getNodeName()
int type = node.getNodeType();
switch (type) {
// print document
case Node.DOCUMENT_NODE:
{
out.print("<?xml version=\"1.0\" encoding=\"UTF8\"?>");
if (indent >= 0) {
out.println();
}
NodeList children = node.getChildNodes();
for (int iChild = 0; iChild < children.getLength(); iChild++) {
lBreak = true;
print(children.item(iChild), indent < 0 ? indent : indent + 1, out);
}
if (indent >= 0) {
out.println();
}
break;
}
// print element with attributes
case Node.ELEMENT_NODE:
{
if (indent >= 0) {
out.println();
// print the indent
for (int i = 1; i <= indent; i++) {
out.print(strIndent);
}
}
out.print('<');
out.print(node.getNodeName());
Attr attrs[] = sortAttributes(node.getAttributes());
for (int i = 0; i < attrs.length; i++) {
Attr attr = attrs[i];
out.print(' ');
out.print(attr.getNodeName());
out.print("=\"");
out.print(normalize(attr.getNodeValue(), false));
out.print('"');
}
out.print('>');
NodeList children = node.getChildNodes();
if (children != null) {
int len = children.getLength();
Node child = null;
for (int i = 0; i < len; i++) {
child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE)
lBreak = true;
print(children.item(i), indent < 0 ? indent : indent + 1, out);
} // end for
} // end if
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
out.print('&');
out.print(node.getNodeName());
out.print(';');
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE:
{
out.print("<![CDATA[");
out.print(node.getNodeValue());
out.print("]]>");
break;
}
// print text
case Node.TEXT_NODE:
{
out.print(normalize(node.getNodeValue(), false));
break;
}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
out.print("<?");
out.print(node.getNodeName());
String data = node.getNodeValue();
if (data != null && data.length() > 0) {
out.print(' ');
out.print(data);
}
out.print("?>");
if (indent >= 0) {
out.println();
}
break;
}
} // end switch
if (type == Node.ELEMENT_NODE) {
if (lBreak && indent >= 0) {
out.println();
// print the indent
for (int i = 1; i <= indent; i++) {
out.print(strIndent);
}
}
out.print("</");
out.print(node.getNodeName());
out.print('>');
}
out.flush();
}
/**
* Normalizes the given string for XML.
*/
private String normalize (String s, boolean htmlBreaks) {
StringBuffer str = new StringBuffer();
int len = (s != null) ? s.length() : 0;
for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
switch (ch) {
case '<':
{
str.append("<");
break;
}
case '>':
{
str.append(">");
break;
}
case '&':
{
str.append("&");
break;
}
case '"':
{
str.append(""");
break;
}
case '\r':case '\n':
{
// htmlBreaks=false, do not append the line break
if (htmlBreaks) {
str.append("<BR />");
}
else {
break;
}
}
default:
{
str.append(ch);
}
} // end switch
} //end for
return (str.toString());
}
/**
* Returns a sorted list of attributes.
*/
private Attr[] sortAttributes (NamedNodeMap attrs) {
int len = (attrs != null) ? attrs.getLength() : 0;
Attr array[] = new Attr[len];
for (int i = 0; i < len; i++) {
array[i] = (Attr)attrs.item(i);
}
for (int i = 0; i < len - 1; i++) {
String name = array[i].getNodeName();
int index = i;
for (int j = i + 1; j < len; j++) {
String curName = array[j].getNodeName();
if (curName.compareTo(name) < 0) {
name = curName;
index = j;
} // end if
} //end for
if (index != i) {
Attr temp = array[i];
array[i] = array[index];
array[index] = temp;
} // end if
} // end for
return (array);
}
}