package com.rest;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import org.apache.axis.encoding.Base64;
public class WsrpcClientCall {
//http://h10077.www1.hp.com/wsrpc/services/WsrpcServiceXfireImpl?wsdl
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String endPoint = "http://h10077.www1.hp.com/wsrpc/services/WsrpcServiceXfireImpl";
String xmlns = "http://common.wsrpc.hp.com";
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
//=======================================USERNAMETOKEN - START====================================================
String pass = "password";
String user = "username";
WsrpcClientCall call = new WsrpcClientCall();
String nonceString = call.getNonce();
String dig=calculatePasswordDigest(nonceString, new Date().toGMTString(), pass);
//SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPElement security =
header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement usernameToken =
security.addChildElement("UsernameToken", "wsse");
SOAPElement username =
usernameToken.addChildElement("Username", "wsse");
username.addTextNode(user);
SOAPElement password =
usernameToken.addChildElement("Password", "wsse");
password
.setAttribute(
"Type",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
password.addTextNode(dig);
SOAPElement nonce =
usernameToken.addChildElement("Nonce", "wsse");
nonce.addTextNode(Base64.encode(hexEncode(nonceString).getBytes()));
SOAPElement created = usernameToken.addChildElement("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
created.addTextNode(new Date().toString());
//==============================================USERNAMETOKEN - END=================================================
SOAPBody mbody = message.getSOAPBody();
SOAPElement query = mbody.addChildElement("query", "ns0", xmlns);
SOAPElement checkAccEl = query.addChildElement("QueryRequest", "ns0", xmlns);
checkAccEl.addChildElement("countryCode", "ns0", xmlns).addTextNode("UK");
checkAccEl.addChildElement("languageCode", "ns0", xmlns).addTextNode("en");
checkAccEl.addChildElement("languageFallback", "ns0", xmlns).addTextNode("false");
checkAccEl.addChildElement("layoutCode", "ns0", xmlns).addTextNode("overview");
checkAccEl.addChildElement("skuList", "ns0", xmlns).addTextNode("KK581ET");
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
String strMsg = new String(out.toByteArray());
System.out.println(strMsg);
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
URL url = new URL(endPoint);
SOAPMessage soapResponse = soapConnection.call(message, url);
System.out.println(soapResponse);
ByteArrayOutputStream resultOut = new ByteArrayOutputStream();
soapResponse.writeTo(resultOut);
String responsexml = new String(resultOut.toByteArray());
Map<String,List> specs = ReadRPCResponseXML.readXML(responsexml);
System.out.println("=======================================================");
for (Map.Entry<String, List> entry : specs.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
System.out.println("=======================================================");
System.out.println(specs.get("TECHSPEC"));
//readXML(finalReslt);
//System.out.println("===="+responsexml);
}
private String getNonce() {
// TODO Auto-generated method stub
return Integer.toString(this.hashCode());
}
private static String calculatePasswordDigest(String nonce, String created, String password) {
String encoded = null;
try {
String pass = hexEncode(nonce) + created + password;
MessageDigest md = MessageDigest.getInstance( "SHA1" );
md.update( pass.getBytes() );
byte[] encodedPassword = md.digest();
encoded = Base64.encode(encodedPassword);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return encoded;
}
private static String hexEncode(String in) {
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < (in.length() - 2) + 1; i = i + 2) {
int c = Integer.parseInt(in.substring(i, i + 2), 16);
char chr = (char) c;
sb.append(chr);
}
return sb.toString();
}
}
/*
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<wsse:Security
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password
Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">LgXEwKzGfYeZayoLW2H1s6AXY+c=</wsse:Password>
<wsse:Nonce>MAI/cA==</wsse:Nonce>
<wsu:Created
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">Thu Feb 20 16:00:51 IST 2014</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns0:query xmlns:ns0="http://common.wsrpc.hp.com">
<ns0:QueryRequest>
<ns0:countryCode>UK</ns0:countryCode>
<ns0:languageCode>en</ns0:languageCode>
<ns0:languageFallback>false</ns0:languageFallback>
<ns0:layoutCode>overview</ns0:layoutCode>
<ns0:skuList>KK581ET</ns0:skuList>
</ns0:QueryRequest>
</ns0:query>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import org.apache.axis.encoding.Base64;
public class WsrpcClientCall {
//http://h10077.www1.hp.com/wsrpc/services/WsrpcServiceXfireImpl?wsdl
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String endPoint = "http://h10077.www1.hp.com/wsrpc/services/WsrpcServiceXfireImpl";
String xmlns = "http://common.wsrpc.hp.com";
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
//=======================================USERNAMETOKEN - START====================================================
String pass = "password";
String user = "username";
WsrpcClientCall call = new WsrpcClientCall();
String nonceString = call.getNonce();
String dig=calculatePasswordDigest(nonceString, new Date().toGMTString(), pass);
//SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPElement security =
header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement usernameToken =
security.addChildElement("UsernameToken", "wsse");
SOAPElement username =
usernameToken.addChildElement("Username", "wsse");
username.addTextNode(user);
SOAPElement password =
usernameToken.addChildElement("Password", "wsse");
password
.setAttribute(
"Type",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
password.addTextNode(dig);
SOAPElement nonce =
usernameToken.addChildElement("Nonce", "wsse");
nonce.addTextNode(Base64.encode(hexEncode(nonceString).getBytes()));
SOAPElement created = usernameToken.addChildElement("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
created.addTextNode(new Date().toString());
//==============================================USERNAMETOKEN - END=================================================
SOAPBody mbody = message.getSOAPBody();
SOAPElement query = mbody.addChildElement("query", "ns0", xmlns);
SOAPElement checkAccEl = query.addChildElement("QueryRequest", "ns0", xmlns);
checkAccEl.addChildElement("countryCode", "ns0", xmlns).addTextNode("UK");
checkAccEl.addChildElement("languageCode", "ns0", xmlns).addTextNode("en");
checkAccEl.addChildElement("languageFallback", "ns0", xmlns).addTextNode("false");
checkAccEl.addChildElement("layoutCode", "ns0", xmlns).addTextNode("overview");
checkAccEl.addChildElement("skuList", "ns0", xmlns).addTextNode("KK581ET");
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
String strMsg = new String(out.toByteArray());
System.out.println(strMsg);
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
URL url = new URL(endPoint);
SOAPMessage soapResponse = soapConnection.call(message, url);
System.out.println(soapResponse);
ByteArrayOutputStream resultOut = new ByteArrayOutputStream();
soapResponse.writeTo(resultOut);
String responsexml = new String(resultOut.toByteArray());
Map<String,List> specs = ReadRPCResponseXML.readXML(responsexml);
System.out.println("=======================================================");
for (Map.Entry<String, List> entry : specs.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
System.out.println("=======================================================");
System.out.println(specs.get("TECHSPEC"));
//readXML(finalReslt);
//System.out.println("===="+responsexml);
}
private String getNonce() {
// TODO Auto-generated method stub
return Integer.toString(this.hashCode());
}
private static String calculatePasswordDigest(String nonce, String created, String password) {
String encoded = null;
try {
String pass = hexEncode(nonce) + created + password;
MessageDigest md = MessageDigest.getInstance( "SHA1" );
md.update( pass.getBytes() );
byte[] encodedPassword = md.digest();
encoded = Base64.encode(encodedPassword);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return encoded;
}
private static String hexEncode(String in) {
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < (in.length() - 2) + 1; i = i + 2) {
int c = Integer.parseInt(in.substring(i, i + 2), 16);
char chr = (char) c;
sb.append(chr);
}
return sb.toString();
}
}
/*
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<wsse:Security
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password
Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">LgXEwKzGfYeZayoLW2H1s6AXY+c=</wsse:Password>
<wsse:Nonce>MAI/cA==</wsse:Nonce>
<wsu:Created
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">Thu Feb 20 16:00:51 IST 2014</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns0:query xmlns:ns0="http://common.wsrpc.hp.com">
<ns0:QueryRequest>
<ns0:countryCode>UK</ns0:countryCode>
<ns0:languageCode>en</ns0:languageCode>
<ns0:languageFallback>false</ns0:languageFallback>
<ns0:layoutCode>overview</ns0:layoutCode>
<ns0:skuList>KK581ET</ns0:skuList>
</ns0:QueryRequest>
</ns0:query>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
No comments:
Post a Comment