atg.rest.client
Class RestComponentHelper

java.lang.Object
  extended by atg.rest.client.RestComponentHelper

public class RestComponentHelper
extends java.lang.Object

A client side helper class which provides useful methods for making calls into the ATG platform using Rest. By default, all methods will return their results in the format that is configured as the default on the server. To override the default, add the "atg-rest-output" parameter to the pParams argument of the appropriate method with a suitable value, "xml", "json", or the identifier for an appropriate output customizer. To begin a session, first call the login() method. Below is a sample application which demonstrates the basic use of this client library. import atg.rest.client.RestClientException; import atg.rest.client.RestComponentHelper; import atg.rest.client.RestResult; import atg.rest.client.RestSession; import java.io.IOException; 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 { mSession = RestSession.createSession(mHost, mPort, mUsername, mPassword); mSession.setUseHttpsForLogin(false); try { mSession.login(); println("Login Successful"); // add REST requests here } catch (Throwable t) { println(t); } finally { try { mSession.logout(); println("Logout Successful"); } catch (RestClientException e) { println(e); } } } public static void main(String[] args) { RestClientSample sample = new RestClientSample(); try { sample.parseArguments(args); sample.execute(); } catch (Throwable t) { sample.println(t); } } }


Field Summary
static java.lang.String CLASS_VERSION
          Class version string
 
Constructor Summary
RestComponentHelper()
           
 
Method Summary
static RestResult executeMethod(java.lang.String pComponentPath, java.lang.String pMethodName, java.lang.Object[] pArguments, java.util.Map<java.lang.String,java.lang.Object> pParams, RestSession pSession)
          Executes the specified method.
static RestResult getComponent(java.lang.String pComponentPath, java.util.Map<java.lang.String,java.lang.Object> pParams, RestSession pSession)
          Returns a representation of the given component
static RestResult getPropertyValue(java.lang.String pComponentPath, java.lang.String pProperty, java.util.Map<java.lang.String,java.lang.Object> pParams, RestSession pSession)
          Returns the value of the requested property or sub-property
static RestResult serviceRequest(java.lang.String pComponentPath, java.util.Map<java.lang.String,java.lang.Object> pParams, RestSession pSession)
          Services a request (the server redirects the request to a JSP which returns JSON or XML)
static RestResult setPropertyValue(java.lang.String pComponentPath, java.lang.String pProperty, java.lang.Object pValue, java.util.Map<java.lang.String,java.lang.Object> pParams, RestSession pSession)
          Sets the specified property or sub-property on the given component.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_VERSION

public static java.lang.String CLASS_VERSION
Class version string

Constructor Detail

RestComponentHelper

public RestComponentHelper()
Method Detail

getComponent

public static RestResult getComponent(java.lang.String pComponentPath,
                                      java.util.Map<java.lang.String,java.lang.Object> pParams,
                                      RestSession pSession)
                               throws RestClientException
Returns a representation of the given component

Parameters:
pComponentPath - the path of the desired component
pParams - a map of parameters to include in the request or null if no parameters are needed
pSession - the session object representing the current session
Returns:
the result of the request
Throws:
RestClientException - if an error occurs issuing the request

getPropertyValue

public static RestResult getPropertyValue(java.lang.String pComponentPath,
                                          java.lang.String pProperty,
                                          java.util.Map<java.lang.String,java.lang.Object> pParams,
                                          RestSession pSession)
                                   throws RestClientException
Returns the value of the requested property or sub-property

Parameters:
pComponentPath - the path of the desired component
pProperty - the name of the desired property or sub-property formatted as follows: "property/sub-property/sub-property"
pParams - a map of parameters to include in the request or null if no parameters are needed
pSession - the session object representing the current session
Returns:
the result of the request
Throws:
RestClientException - if an error occurs issuing the request

setPropertyValue

public static RestResult setPropertyValue(java.lang.String pComponentPath,
                                          java.lang.String pProperty,
                                          java.lang.Object pValue,
                                          java.util.Map<java.lang.String,java.lang.Object> pParams,
                                          RestSession pSession)
                                   throws RestClientException
Sets the specified property or sub-property on the given component. Note, this method is meant for setting properties on components only. To set properties on repository items, use the setRepositoryPropertyValue() method.

Parameters:
pComponentPath - the path of the desired component
pProperty - the name of the property to set
pValue - the value to set
pParams - a map of parameters to include in the request or null if no parameters are needed
pSession - the session object representing the current session
Returns:
the result of the request
Throws:
RestClientException - if an error occurs issuing the request

executeMethod

public static RestResult executeMethod(java.lang.String pComponentPath,
                                       java.lang.String pMethodName,
                                       java.lang.Object[] pArguments,
                                       java.util.Map<java.lang.String,java.lang.Object> pParams,
                                       RestSession pSession)
                                throws RestClientException
Executes the specified method.

Parameters:
pComponentPath - the path of the desired component
pMethodName - the name of the method to call
pArguments - an array of arguments to be passed to the method or null if there are no arguments
pParams - a map of parameters to include in the request or null if no parameters are needed
pSession - the session object representing the current session
Returns:
the result of the request
Throws:
RestClientException - if an error occurs issuing the request

serviceRequest

public static RestResult serviceRequest(java.lang.String pComponentPath,
                                        java.util.Map<java.lang.String,java.lang.Object> pParams,
                                        RestSession pSession)
                                 throws RestClientException
Services a request (the server redirects the request to a JSP which returns JSON or XML)

Parameters:
pComponentPath - the path of the desired component
pParams - a map of parameters to include in the request or null if no parameters are needed
pSession - the session object representing the current session
Returns:
the result of the request
Throws:
RestClientException - if an error occurs issuing the request