public class Connection
extends java.lang.Object
Create an instance of this class for each server you wish to interact with. If the commands you execute reference data that requires a login, you must also set an email address and password to be used for authentication. Typically, developers will obtain these credentials from the program's environment, such as via command-line parameters, environment variables, a properties file, etc.
After creating and initializing the Connection instance, pass it to the
Command.execute() method.
Example:
Connection cn = new Connection("https://labkey.org");
SelectRowsCommand cmd = new SelectRowsCommand("study", "Physical Exam");
SelectRowsResponse response = cmd.execute(cn, "Home/Study/demo");
for(Map<String,Object> row : response.getRows())
{
System.out.println(row.get("ParticipantId") + " weighs " + row.get("Weight"));
}
Example using Authentication
//get the user email and password from command-line arguments,
//environment variables, a file, or some other mechanism.
String user = getUser();
String password = getPassword();
//create a new connection passing the user credentials
Connection cn = new Connection("https://localhost:8080/labkey", user, password);
SelectRowsCommand cmd = new SelectRowsCommand("lists", "People");
SelectRowsResponse response = cmd.execute(cn, "Api Test");
Note that this class is not thread-safe. Do not share instances of Connection between threads.
| Constructor and Description |
|---|
Connection(java.lang.String baseUrl)
Constructs a new Connection object given a base URL.
|
Connection(java.lang.String baseUrl,
java.lang.String email,
java.lang.String password)
Constructs a new Connection object given a base URL and user credentials.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
afterExecute(org.apache.commons.httpclient.HttpClient client,
org.apache.commons.httpclient.HttpMethod method,
int status) |
protected void |
beforeExecute(org.apache.commons.httpclient.HttpClient client,
org.apache.commons.httpclient.HttpMethod method) |
int |
executeMethod(org.apache.commons.httpclient.HttpMethod method) |
java.lang.String |
getBaseUrl()
Returns the base URL for this connection.
|
protected org.apache.commons.httpclient.HttpConnectionManager |
getConnectionManager()
Returns the connection manager to use when initializing the
HttpClient object.
|
java.lang.String |
getEmail()
Returns the email address to use for authentication.
|
org.apache.commons.httpclient.HttpClient |
getHttpClient()
Returns the HttpClient object to use for this connection.
|
java.lang.String |
getPassword()
Returns the password to use for authentication.
|
java.lang.Integer |
getTimeout()
The default timeout for Commands that have not established their own timeouts.
|
boolean |
isAcceptSelfSignedCerts()
Returns true if the connection should accept a self-signed
SSL certificate when using HTTPS, false otherwise.
|
void |
setAcceptSelfSignedCerts(boolean acceptSelfSignedCerts)
Sets the accept self-signed certificates option.
|
void |
setEmail(java.lang.String email)
Sets the email address to use for authentication.
|
void |
setPassword(java.lang.String password)
Sets the password to use for authentication.
|
void |
setTimeout(java.lang.Integer timeout)
Set a default timeout for Commands that have not established their own timeouts.
|
public Connection(java.lang.String baseUrl)
This is equivalent to calling Connection(baseUrl, null, null).
baseUrl - The base URLConnection(String, String, String)public Connection(java.lang.String baseUrl,
java.lang.String email,
java.lang.String password)
The baseUrl parameter should include the protocol, domain name, port, and LabKey web application context path (if configured). For example in a typical localhost configuration, the base URL would be:
http://localhost:8080/labkey
Note that https may also be used for the protocol. By default the
Connection is configured to accept self-signed SSL certificates, which
is fairly common for LabKey Server installations. If you do not want
to accept self-signed certificates, use
setAcceptSelfSignedCerts(false) to disable this behavior.
The email name and password should correspond to a valid user email and password on the target server.
baseUrl - The base URLemail - The user email name to pass for authenticationpassword - The user password to send for authenticationpublic java.lang.String getBaseUrl()
public java.lang.String getEmail()
public void setEmail(java.lang.String email)
email - The email address to use.public java.lang.String getPassword()
public void setPassword(java.lang.String password)
password - The password to use.public boolean isAcceptSelfSignedCerts()
public void setAcceptSelfSignedCerts(boolean acceptSelfSignedCerts)
acceptSelfSignedCerts - set to false to not accept self-signed certificatespublic org.apache.commons.httpclient.HttpClient getHttpClient()
throws org.apache.commons.httpclient.URIException
org.apache.commons.httpclient.URIException - Thrown if the base URL could not be converted
into a legal URI.protected void beforeExecute(org.apache.commons.httpclient.HttpClient client,
org.apache.commons.httpclient.HttpMethod method)
protected void afterExecute(org.apache.commons.httpclient.HttpClient client,
org.apache.commons.httpclient.HttpMethod method,
int status)
public int executeMethod(org.apache.commons.httpclient.HttpMethod method)
throws java.io.IOException,
org.apache.commons.httpclient.HttpException
java.io.IOExceptionorg.apache.commons.httpclient.HttpExceptionprotected org.apache.commons.httpclient.HttpConnectionManager getConnectionManager()
public void setTimeout(java.lang.Integer timeout)
timeout - the length of the timeout waiting for the server response, in millisecondspublic java.lang.Integer getTimeout()