Sample Application Using Java
The Java code samples provided along with Sentinel LDK-EMS use Apache HTTPClient to access the Sentinel LDK-EMS web services.
The web services require authentication before you can use them in your code. This section first explains how to authenticate using a Sentinel LDK-EMS user name and password, and then provides an example to call a web service for getting details of a Product.
NOTE If you are using CASTOR for marshalling operation, you need to modify the castor.properties file and set the value org.exolab.castor.naming=mixed
for proper marshalling.
Getting Ready
1.Create a new Java project, say, JavaWSSample, from the source located at %EMS_HOME%\Sample/JavaWSSample
2.Update the configure.properties file and provide actual values for following properties:
•trustStore
=C:/Program Files/apache-tomcat-6.0.33/ems.keystore
•trustStorePassword
=changeit
•serverUrl
=https://localhost:443/
The Concept
The general process for executing Sentinel LDK-EMS web service URLs can be divided into the following steps.
Read Properties
Read different values like server URL, trust store location, password from property file and set these values to system (refer to the init()
method in JavaWSSample/EMSActivationSample.java).
Properties props=new Properties(); FileInputStream in = new FileInputStream("configure.properties"); props.load(in); trustStore = props.getProperty("trustStore"); trustStorePass =props.getProperty("trustStorePassword"); serverUrl=props.getProperty("serverUrl");
Set System Properties
Set javax.net.ssl.trustStore
and javax.net.ssl.trustStorePassword
to
authenticate the Sentinel LDK-EMS Server specified in server
property.
System.setProperty("javax.net.ssl.trustStore", trustStore); System.setProperty("javax.net.ssl.trustStorePassword",trustStorePass);
Log In
To access a web service, you need to first log on using login.ws. To implement this,
create an object of HttpContext
and call the Sentinel LDK-EMS log-in URL using POST method as shown below:
HttpContext context = new BasicHttpContext();
HttpPost post = new HttpPost(serverUrl+"ems/v90/ws/login.ws");
String authenticationDetail = WSUtil.readFileAsString
("AuthenticationDetailSample.xml");
post.setEntity(new StringEntity(authenticationDetail, HTTP.UTF_8));
HttpResponse emsResponse = httpclient.execute(post, context);
principal = (Principal) context
.getAttribute(ClientContext.USER_TOKEN);
if (emsResponse.getStatusLine().getStatusCode()==200)
return true;
else
return false;
Refer to the verifyLogin()
method in JavaWSSample/EMSActivationSample.java for details.
NOTE Once login is successful, store the ClientContext.USER_TOKEN
in a variable to use in further calls. This will
help you to authenticate once for one session of your application.
Call the Web Services
After login is successful, you can call any web service method to get/create/update different Sentinel LDK-EMS entities (refer to the sample at JavaWSSample/WSUtil.java for doput(), doGet(), doPost() methods).
GET Sample
The code sample uses HTTPGet class to call GET web service and set search parameters (if any) to URL.
HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.USER_TOKEN, principal); // Use HttpGet class to call HTTP Get method HttpGet get = new HttpGet(url); StringBuffer query = new StringBuffer(); // If url have some search parameters if (urlParams!=null) { for (String paramName : urlParams.keySet()) { String paramValue = urlParams.get(paramName); setParameter(query, paramName, paramValue); } get.setURI(new URI(URLDecoder.decode(get.getURI()
+ query.toString(), "UTF-8"))); } HttpResponse response = httpclient.execute(get, context);
PUT Sample
The code sample uses HTTPPut class to call PUT web service and set input XML to request body. You can call POST and DELETE web services in a similar way.
NOTE If you need to use characters like "<" and "&" in your XML elements, enclose the element within a CDATA section. A CDATA section begins with "<![CDATA[" and ends with "]]>". All text in an XML document is parsed by the parser. But text inside CDATA section are ignored by the parser.
HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.USER_TOKEN, principal); // Use HttpPut class to call HTTP Put method HttpPut put = new HttpPut(url); // Set Input Xml to request body put.setEntity(new StringEntity(inputXml, HTTP.UTF_8)); HttpResponse response = httpclient.execute(put, context);
Handling Response
PUT Response
If the PUT call is successful, HTTP status code 201, Created, is returned.
// Check for status code, if not 201 get error reason from response body. if(emsResponse.getStatusLine().getStatusCode()!=SC_CREATED){ throw new Exception("Create Entitlement Failed - " + WSUtil.getDataFromResponse (emsResponse)); }else{ //If successfully done Id of object created can be retrieved from header “Location” return emsResponse.getFirstHeader("Location").getValue(); }
POST response
If the POST call is successful, HTTP status code 200, OK, is returned.
// Check for status code, if not 200 get error reason from response body. if (emsResponse.getStatusLine().getStatusCode()!=SC_OK) throw new Exception(WSUtil.getDataFromResponse(emsResponse)); else{ //If successfully done response xml can be retrieved from response body. return WSUtil.getDataFromResponse(emsResponse); }
DELETE Response
If the DELETE call is successful, HTTP status code 204, No Content, is returned.
// Check for status code if not, 204 get error reason from response body. if (emsResponse.getStatusLine().getStatusCode()!=SC_NO_CONTENT) throw new Exception(WSUtil.getDataFromResponse(emsResponse)); else{ //If successfully done response xml can be retrieved from response body. return true; }
GET Response
If the GET call is successful, HTTP status code 200, OK, is returned.
// Check for status code, if not 200 get error reason from response body. if (emsResponse.getStatusLine().getStatusCode()!=SC_OK) throw new Exception(WSUtil.getDataFromResponse(emsResponse)); else{ //If successfully done response xml can be retrieved from response body. return WSUtil.getDataFromResponse(emsResponse); }