public class RestSession
extends java.lang.Object
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);
}
}
}
| Modifier and Type | Class and Description |
|---|---|
class |
RestSession.ConnectionAwareWeakReference<T>
A class representing a very specialised form of weak refernece which, in addition to the expected reference, also contains
a regular reference to a connection.
|
static class |
RestSession.FORMAT |
static class |
RestSession.PARTITION |
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CLASS_VERSION
Class version string
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
clearOrphanedConnections()
Clear any remaining connections which have been orphaned.
|
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.util.Set<java.lang.String> |
getCookieFields()
Retrieves the value of the cookie fields which will be sent
with every request if resend cookie is set.
|
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 |
isResendCookie()
Retrieve a flag stating whether the cookies defined by 'cookie fields'
should be sent with each request.
|
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 |
setCookieFields(java.util.Set<java.lang.String> pCookieFields)
Sets the value of the cookie fields which will be sent
with every request if resend cookie is set.
|
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 |
setResendCookie(boolean pResendCookie)
Set a flag stating whether the cookies defined by 'cookie fields'
should be sent with each request.
|
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.
|
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 boolean isResendCookie()
public void setResendCookie(boolean pResendCookie)
public void setCookieFields(java.util.Set<java.lang.String> pCookieFields)
public java.util.Set<java.lang.String> getCookieFields()
public 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 setpublic 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 passwordpublic 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 idpublic static RestSession createSession(java.lang.String pHost, int pPort)
pHost - the host to connect topPort - the port the server is listening onpublic 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 idpublic java.lang.String login(java.lang.String pUsername,
java.lang.String pPassword)
throws RestClientException
pUsername - the usernamepPassword - the user's passwordRestClientException - if an error occurs issuing the requestpublic java.lang.String login()
throws RestClientException
RestClientException - if an error occurs issuing the requestpublic void startSession()
throws RestClientException
login(String, String) after this method to log the session
in.RestClientExceptionpublic RestResult logout() throws RestClientException
RestClientException - if an error occurs issuing the requestprotected void clearOrphanedConnections()
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 DELETERestClientException - if any of the parameters cannot be convertedjava.io.IOException - if an IO error occurspublic 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 typeRestClientException - if any of the parameters cannot be convertedpublic 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 typeRestClientException - if any of the parameters cannot be convertedprotected 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 typeRestClientException - if an error occurs issuing the requestpublic java.lang.String getHostString()