|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectatg.rest.client.RestSession
public class RestSession
A class which represents a session with an ATG server.
To obtain an anonymous (i.e. not logged in) session: RestSession session = RestSession.createSession(host, port); session.startSession(); [make REST calls here] optionally, if you decide to login later: session.login(username, password); session.logout();
To obtain a session that's logged in: RestSession session = RestSession.createSession(host, port, username, password); session.login(); [make REST calls here] session.logout();
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);
}
}
}
| Nested Class Summary | |
|---|---|
static class |
RestSession.FORMAT
|
static class |
RestSession.PARTITION
|
| Field Summary | |
|---|---|
static java.lang.String |
CLASS_VERSION
Class version string |
| Method Summary | |
|---|---|
protected java.net.HttpURLConnection |
createHttpConnection(java.lang.String pURL,
java.lang.Object pParams,
java.lang.String pMethodType)
Creates an http connection to the server with the given URL and parameters. |
RestResult |
createHttpRequest(java.lang.String pURL,
java.util.Map<java.lang.String,java.lang.Object> pParams,
java.lang.Object[] pArguments,
java.lang.String pMethodType)
Raw method for creating an http request. |
RestResult |
createHttpRequest(java.lang.String pURL,
java.util.Map<java.lang.String,java.lang.Object> pParams,
java.lang.String pMethodType)
Raw method for creating an http request. |
static RestSession |
createSession(java.lang.String pHost,
int pPort)
Factory method for creating RestSession |
static RestSession |
createSession(java.lang.String pHost,
int pPort,
RestSession.PARTITION pPartitionType,
java.lang.String pPartitionId)
Factory method for creating RestSession |
static RestSession |
createSession(java.lang.String pHost,
int pPort,
java.lang.String pUsername,
java.lang.String pPassword)
Factory method for creating RestSession |
static RestSession |
createSession(java.lang.String pHost,
int pPort,
java.lang.String pUsername,
java.lang.String pPassword,
RestSession.PARTITION pPartitionType,
java.lang.String pPartitionId)
Factory method for creating RestSession |
java.lang.String |
getEncoding()
|
java.lang.String |
getHost()
|
java.lang.String |
getHostString()
Returns a host string for this session object of the format scheme://host:port. |
RestSession.FORMAT |
getInputFormat()
Returns the value of the default input format on the server. |
RestSession.FORMAT |
getOutputFormat()
Returns the value of the default output format on the server. |
java.lang.String |
getPartitionId()
get PartitionId |
RestSession.PARTITION |
getPartitionType()
get PartitionType |
java.lang.String |
getPassword()
|
int |
getPort()
|
java.lang.String |
getRestContextRoot()
|
java.lang.String |
getScheme()
|
java.lang.String |
getSessionId()
|
java.lang.String |
getUserId()
|
java.lang.String |
getUsername()
|
boolean |
isUseHttpsForLogin()
|
boolean |
isUseInternalProfileForLogin()
|
java.lang.String |
login()
Logs the specified user into the specified server. |
java.lang.String |
login(java.lang.String pUsername,
java.lang.String pPassword)
Logs the specified user into the already specified server. |
RestResult |
logout()
Logs the specified user out of the specified server. |
protected java.lang.Object |
packageParameters(java.util.Map<java.lang.String,java.lang.Object> pParams,
java.lang.Object[] pArguments,
java.lang.String pMethodType)
This method packages parameters in either a JSON object, XML document, or parameter map depending on the setting in Session.mInputFormat |
void |
setEncoding(java.lang.String pEncoding)
|
void |
setHost(java.lang.String pHost)
|
void |
setInputFormat(RestSession.FORMAT pInputFormat)
|
void |
setOutputFormat(RestSession.FORMAT pOutputFormat)
|
void |
setPartitionId(java.lang.String pPartitionId)
set PartitionId |
void |
setPartitionType(RestSession.PARTITION pPartitionType)
set PartitionType |
void |
setPassword(java.lang.String pPassword)
|
void |
setPort(int pPort)
|
void |
setRestContextRoot(java.lang.String pRestContextRoot)
|
void |
setScheme(java.lang.String pScheme)
|
void |
setSessionId(java.lang.String pSessionId)
|
void |
setUseHttpsForLogin(boolean pUseHttpsForLogin)
|
void |
setUseInternalProfileForLogin(boolean pUseInternalProfileForLogin)
|
protected void |
setUserId(java.lang.String pUserId)
|
void |
setUsername(java.lang.String pUsername)
|
void |
startSession()
Initializes the RestSession without actually logging in. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static java.lang.String CLASS_VERSION
| Method Detail |
|---|
public java.lang.String getHost()
public void setHost(java.lang.String pHost)
pHost - the host to setpublic int getPort()
public void setPort(int pPort)
pPort - the port to setpublic java.lang.String getUsername()
public void setUsername(java.lang.String pUsername)
pUsername - the pUsername to setpublic java.lang.String getPassword()
public void setPassword(java.lang.String pPassword)
pPassword - the password to setpublic void setPartitionId(java.lang.String pPartitionId)
pPartitionId - the PartitionIdpublic java.lang.String getPartitionId()
public void setPartitionType(RestSession.PARTITION pPartitionType)
pPartitionType - the PartitionTypepublic RestSession.PARTITION getPartitionType()
public java.lang.String getScheme()
public void setScheme(java.lang.String pScheme)
pScheme - the scheme to setpublic java.lang.String getRestContextRoot()
public void setRestContextRoot(java.lang.String pRestContextRoot)
pRestContextRoot - the restContextRoot to setpublic boolean isUseInternalProfileForLogin()
public void setUseInternalProfileForLogin(boolean pUseInternalProfileForLogin)
pUseInternalProfileForLogin - the useInternalProfileForLogin to setpublic boolean isUseHttpsForLogin()
public void setUseHttpsForLogin(boolean pUseHttpsForLogin)
pUseHttpsForLogin - the useHttpsForLogin to setpublic java.lang.String getUserId()
protected void setUserId(java.lang.String pUserId)
pUserId - the userId to setpublic java.lang.String getSessionId()
public void setSessionId(java.lang.String pSessionId)
pSessionId - the sessionId to setpublic java.lang.String getEncoding()
public void setEncoding(java.lang.String pEncoding)
pEncoding - the encoding to setpublic RestSession.FORMAT getInputFormat()
public void setInputFormat(RestSession.FORMAT pInputFormat)
pInputFormat - the inputFormat to setpublic RestSession.FORMAT getOutputFormat()
public void setOutputFormat(RestSession.FORMAT pOutputFormat)
pOutputFormat - the outputFormat to set
public static RestSession createSession(java.lang.String pHost,
int pPort,
java.lang.String pUsername,
java.lang.String pPassword)
pHost - the host to connect topPort - the port the server is listening onpUsername - the usernamepPassword - the user's password
public static RestSession createSession(java.lang.String pHost,
int pPort,
java.lang.String pUsername,
java.lang.String pPassword,
RestSession.PARTITION pPartitionType,
java.lang.String pPartitionId)
pHost - the host to connect topPort - the port the server is listening onpUsername - the usernamepPassword - the user's passwordpPartitionType - must be one of PARTITION.SITE or PARTITION.PROFILE_REALMpPartitionId - either the site id or realm id
public static RestSession createSession(java.lang.String pHost,
int pPort)
pHost - the host to connect topPort - the port the server is listening on
public static RestSession createSession(java.lang.String pHost,
int pPort,
RestSession.PARTITION pPartitionType,
java.lang.String pPartitionId)
pHost - the host to connect topPort - the port the server is listening onpPartitionType - must be one of PARTITION.SITE or PARTITION.PROFILE_REALMpPartitionId - either the site id or realm id
public java.lang.String login(java.lang.String pUsername,
java.lang.String pPassword)
throws RestClientException
pUsername - the usernamepPassword - the user's password
RestClientException - if an error occurs issuing the request
public java.lang.String login()
throws RestClientException
RestClientException - if an error occurs issuing the request
public void startSession()
throws RestClientException
login(String, String) after this method to log the session in.
RestClientException
public RestResult logout()
throws RestClientException
RestClientException - if an error occurs issuing the request
protected java.net.HttpURLConnection createHttpConnection(java.lang.String pURL,
java.lang.Object pParams,
java.lang.String pMethodType)
throws RestClientException,
java.io.IOException
pURL - the url to requestpParams - any parameters for the requestpMethodType - the request type: GET, POST, PUT, or DELETE
RestClientException - if any of the parameters cannot be converted
java.io.IOException - if an IO error occurs
public RestResult createHttpRequest(java.lang.String pURL,
java.util.Map<java.lang.String,java.lang.Object> pParams,
java.lang.String pMethodType)
throws RestClientException
pURL - the URLpParams - any url parameters requiredpMethodType - the http method type
RestClientException - if any of the parameters cannot be converted
public RestResult createHttpRequest(java.lang.String pURL,
java.util.Map<java.lang.String,java.lang.Object> pParams,
java.lang.Object[] pArguments,
java.lang.String pMethodType)
throws RestClientException
pURL - the URLpParams - any url parameters requiredpArguments - an array of arguments to be passed to for a method call or null if there are no argumentspMethodType - the http method type
RestClientException - if any of the parameters cannot be converted
protected java.lang.Object packageParameters(java.util.Map<java.lang.String,java.lang.Object> pParams,
java.lang.Object[] pArguments,
java.lang.String pMethodType)
throws RestClientException
pParams - the parameter mappArguments - the argument list when executing a method, null otherwisepMethodType - the http method type
RestClientException - if an error occurs issuing the requestpublic java.lang.String getHostString()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||