The following code sample is a shell of a simple application. It does nothing more than login and logout. The most interesting portion of the following code sample is the execute method.

Note: You can perform multiple Legacy REST Web Services requests during an HTTP session. See information about HTTP sessions and the Legacy REST Web Services server in Logging In.

The sample first creates a RestSession object. It does this by calling the static RestSession.createSession() method. The parameters to createSession are the hostname, port, username, and password. By default all login calls are issued with HTTPS. If you do not want this functionality, set the value of the useHttpsForLogin flag to false on the session object, as the following sample does. The next section of code calls the login() method on the RestSession object. If the login attempt fails, then a RestClientException is thrown. The last section of code calls the logout() method and concludes the session.

 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 {
   String loginStatus = mSession.login();
   if(loginStatus == null || "null".equals(loginStatus)) {
     mSession = null;
     System.out.println("Login Failed");
   }
   else {
     System.out.println("Login Successful");
   }
 }

 catch (Throwable t) {
 println(t);
 }

 finally {
 try {
   if(mSession != null) {
     mSession.logout();
     System.out.println("Logout Successful");
   }
 }
 catch (RestClientException e) {
 println(e);
 }
 }
}

/**
 * @param args
 */
public static void main(String[] args) {
 RestClientSample sample = new RestClientSample();

 try {
 sample.parseArguments(args);
 sample.execute();
 }
 catch (Throwable t) {
 sample.println(t);
 }
}
 }

Copyright © 1997, 2013 Oracle and/or its affiliates. All rights reserved. Legal Notices