Tuesday, November 11, 2014

How to trigger a web service from stand alone java

/*
Class EndPoint
*/

package better.faster.smarter.development;

public class EndPoint {
private String hostName="";
public void setHostName(String hostName){
this.hostName=hostName;
}
public String getHostName(){
return this.hostName;
}
}

------------------------------------------------------------------------------------------
/*
Class ExecuteWebService
*/

package better.faster.smarter.development;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;

public class ExecuteWebService {
//soapRequest is a string which is used for executing the web service
@SuppressWarnings("deprecation")
protected static String executeWebService(String soapRequest,EndPoint endPt,String signature)
{
String responseString="";
String hostName="";
URL url=null;
URLConnection urlC=null;
DataOutputStream out =null;
DataInputStream in = null;
String str="";
try{
//Validate the endPoint
if(null==endPt)
throw new RuntimeException("Cannot proceed with executing the web service as the end point is null");
//Get the hostName
hostName=endPt.getHostName();
//Validate the endPoint here as well
if("".equalsIgnoreCase(hostName))
throw new RuntimeException("Cannot proceed with executing the web service as the end point is empty");
//Validate the soap Request
if("".equalsIgnoreCase(soapRequest))
throw new RuntimeException("Cannot proceed with executing the web service as the soap request is invalid");
//Get the header value if header is present then add SOAPHeader else trigger direct soap request
if(signature==null)
signature="";
if(!"".equalsIgnoreCase(signature))
{
soapRequest = "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<SOAP:Header><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">" +
                   signature + "</wsse:Security></SOAP:Header><SOAP:Body>" + soapRequest + "</SOAP:Body></SOAP:Envelope>";
}
//Both soap request and end point are valid if the control comes here
//Initialize url and also urlConnection and all the related parameters. Hard coded few of them as 
//they are rarely used in our application
url = new URL(hostName);
urlC = url.openConnection();
urlC.setRequestProperty("Content-Type", "text/xml");
urlC.setDoInput(true);
urlC.setDoOutput(true);
urlC.setUseCaches(false);
out = new DataOutputStream(urlC.getOutputStream());
out.writeBytes(soapRequest);
in = new DataInputStream(urlC.getInputStream());
responseString="";
while(null!=((str=in.readLine())))
responseString+= str+"\n";
}
catch(Exception e)
{
CustomLogger.logEntry("Exception while executing the soap request with details "+e.getMessage());
}
finally{
hostName=null;
url=null;
urlC=null;
}
return responseString;
}
}
------------------------------------------------------------------------------------
/*
Class TriggerAssertion
*/

package better.faster.smarter.development;

public class TriggerAssertion {

public static void main(String[] args) {
// TODO Auto-generated method stub
EndPoint endPt = new EndPoint();
endPt.setHostName("http://192.168.1.10/cordys/com.eibus.web.soap.Gateway.wcp?");
String userName="icici";
String userPass="icici";
String requestString =
               "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
               "<SOAP:Header>" +
               "<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">" +
               "<wsse:UsernameToken xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">" +
               "<wsse:Username>" + userName + "</wsse:Username>" +
               "<wsse:Password>" + userPass + "</wsse:Password>" +
               "</wsse:UsernameToken>" +
               "</wsse:Security>" +
               "</SOAP:Header>" +
               "<SOAP:Body>" +
               "<samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" MinorVersion=\"1\">" +
               "<samlp:AuthenticationQuery>" +
               "<saml:Subject xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\">" +
               "<saml:NameIdentifier Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified\">" + userName + "</saml:NameIdentifier>" +
               "</saml:Subject>" +
               "</samlp:AuthenticationQuery>" +
               "</samlp:Request>" +
               "</SOAP:Body>" +
               "</SOAP:Envelope>";
String responseString = ExecuteWebService.executeWebService(requestString,endPt,"");
String getUserDetails="<GetUserDetails xmlns=\"http://schemas.cordys.com/notification/workflow/1.0\"></GetUserDetails>";
String sos= "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                "<SOAP:Body>" + getUserDetails + "</SOAP:Body></SOAP:Envelope>";
int ssss = responseString.indexOf("<Signature");
        int k = responseString.indexOf("</samlp:AssertionArtifact>");
        String sigresponse = responseString.substring(ssss, k+26);
responseString=ExecuteWebService.executeWebService(getUserDetails,endPt,sigresponse);
System.out.println(responseString);
}

}


No comments:

Post a Comment