Friday, December 23, 2011

How to use ATG REST for self service application


REST in ATG self service customer application , we need to write custom component on the ATG side .
This concept can you useful to customize the ATG default self service UI :
So this is two step process :
1) Server ATG side :Create a custom component inside ATG9.1 folder as below :

ATG9.1
|
SelfServiceREST
├───config
├───lib
└───META-INF
a)Inside config folder , place .. RestfulSearch.properties
$class=demo.rest.RestfulSearch
$scope=global
b)Inside lib folder place — demo-rest.jar ( compile the below component class and jar it)
c) Iniside META-INF –MANIFEST.MF ,place below content :
Manifest-Version: 1.0
ATG-Product: MotorolaREST
ATG-Config-Path: config/
ATG-Class-Path: lib/demo-rest.jar
ATG-Client-Class-Path: lib/demo-rest.jar
ATG-Required: Service.SelfService

ATG custom component code as below :
package demo.rest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import atg.nucleus.GenericService;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import atg.servlet.ServletUtil;
import atg.svc.ui.formhandlers.SearchFormHandler;
public class RestfulSearch extends GenericService
{
/* method that submits the search */
public String processServiceSearch(Map searchMap) throws IOException, ServletException
{
System.out.println(“RestfulSearch::processServiceSearch() BEGIN”);
DynamoHttpServletRequest request = ServletUtil.getCurrentRequest();
DynamoHttpServletResponse response = ServletUtil.getCurrentResponse();
System.out.println(“DEBUG :: request=”+request+”, response “+response);
SearchFormHandler formHandler = (SearchFormHandler)request.resolveName( “/atg/svc/ui/formhandlers/SearchFormHandler” );
formHandler.setSearchTextValues((String[])searchMap.get(“searchTextValues”));
try
{
boolean result = formHandler.handleInitialSearch( request, response);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(“RestfulSearch::processServiceSearch() END”);
List result = formHandler.restoreSearchInfo().getCurrentPage();
return result.toString();
}
}
2)Client side code using REST :
package com.atg.demo;
import java.util.HashMap;
import java.util.Map;
import atg.rest.client.RestClientException;
import atg.rest.client.RestComponentHelper;
import atg.rest.client.RestResult;
import atg.rest.client.RestSession;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import atg.servlet.ServletUtil;
import atg.svc.ui.formhandlers.SearchFormHandler;
public class RestClientSample {
private String mUsername = null;
private String mPassword = null;
private String mHost = “localhost”;
private int mPort = 80;
private RestSession mSession = null;
public RestClientSample() {
}
protected void parseArguments(String[] pArgs) throws Exception {
for (int i = 0; i < pArgs.length; i++) {
String arg = pArgs[i];
if (arg.equals(“-user”))
mUsername = pArgs[i + 1];
else if (arg.equals(“-password”))
mPassword = pArgs[i + 1];
else if (arg.equals(“-host”))
mHost = pArgs[i + 1];
else if (arg.equals(“-port”))
mPort = Integer.parseInt(pArgs[i + 1]);
}
if (isBlank(mUsername))
throw new Exception(“Must supply username”);
if (isBlank(mPassword))
throw new Exception(“Must supply password”);
}
protected boolean isBlank(String pStr) {
return (pStr == null || pStr.length() == 0 || pStr.trim().length() == 0);
}
protected void println(String s) {
System.out.println(s);
}
protected void println(Throwable t) {
t.printStackTrace(System.out);
}
protected void execute() throws RestClientException {
RestResult result = null;
mSession = RestSession.createSession(mHost, mPort, mUsername, mPassword);
mSession.setUseHttpsForLogin(false);
try {
mSession.login();
println(“Login Successful”);
try
{
System.out.println(“try block”);
Map searchParams = new HashMap();
searchParams.put(“atg-rest-input”, “xml”);
searchParams.put(“atg-rest-depth”, “1″);
searchParams.put(“atg-rest-index”, “0″);
searchParams.put(“atg-rest-count”, “20″);
searchParams.put(“searchTextValues”, “atg is tough”);
try
{
result = RestComponentHelper.executeMethod(“/demo/rest/RestfulSearch”, “processServiceSearch”, new Object[]{searchParams}, null, mSession);
}
catch (Exception e)
{
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
String responseData = result.readInputStream();
System.out.println(“xxxxxxxxxxx Response Data “+responseData);
result.close();
}
catch (Throwable t) {
println(t);
}
finally {
if (result != null)
result.close();
try {
mSession.logout();
println(“Logout Successful”);
}
catch (RestClientException e) {
println(e);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
RestClientSample sample = new RestClientSample();
args = new String[8];
args[0] = “-user”;
args[1] = “admin”;
args[2] = “-password”;
args[3] = “admin”;
args[4] = “-host”;
args[5] = “localhost”;
args[6] = “-port”;
args[7] = “10180″;
try {
sample.parseArguments(args);
sample.execute();
} catch (Throwable t) {
sample.println(t);
}
}
}



No comments:

Popular Posts