Class Connection
Create an instance of this class for each server you wish to interact with.
If the commands you execute require a login, you must also configure
authentication via one of the supported methods: retrieving email address
and password from a .netrc/_netrc file, providing an api key, or providing
email address and password directly (which could be obtained from the
program's environment, such as via command-line parameters, environment
variables, a properties file, etc. See the individual constructors and
implementations of CredentialsProvider
for more details.
Command.execute()
method.
Example:
Connection cn = new Connection("https://www.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.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConnection
(String baseUrl) Constructs a new Connection object with a base URL that attempts authentication via .netrc/_netrc entry, if present.Connection
(String baseUrl, String email, String password) Constructs a new Connection object for a base URL that attempts basic authentication.Connection
(String baseUrl, CredentialsProvider credentialsProvider) Constructs a new Connection object given a base URL and a credentials provider.Connection
(URI baseURI, CredentialsProvider credentialsProvider) Constructs a new Connection object given a base URL and a credentials provider. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
protected void
beforeExecute
(org.apache.hc.core5.http.HttpRequest request) If first request, prime the Connection by forcing authentication and populating CSRF and session info.Returns the base URI for this connection.org.apache.hc.client5.http.impl.classic.CloseableHttpClient
Returns the CloseableHttpClient object to use for this connection.int
The timeout used for Commands that have not established their own timeouts.impersonate
(String email) For site-admins only, start impersonating a user.impersonate
(String email, String projectPath) For site-admins or project-admins only, start impersonating a user.boolean
Returns true if the connection should accept a self-signed SSL certificate when using HTTPS, false otherwise.void
logout()
Invalidates the current HTTP session on the server, if anysetAcceptSelfSignedCerts
(boolean acceptSelfSignedCerts) Sets the accept-self-signed certificates option.Sets the proxy host and port for this Connection.
NOTE: Changing this setting will force the underlying http client to be recreated.setTimeout
(Integer timeout) Set a default timeout for Commands that have not established their own timeouts.void
setUserAgent
(String userAgent) Stop impersonating a user.
-
Field Details
-
X_LABKEY_CSRF
- See Also:
-
JSESSIONID
- See Also:
-
-
Constructor Details
-
Connection
Constructs a new Connection object given a base URL and a credentials provider.The baseURI 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:
new URI("http://localhost:8080/labkey")
Note that https may also be used for the protocol. By default the Connection is configured to deny self-signed SSL certificates. If you want to accept self-signed certificates, use
setAcceptSelfSignedCerts(false)
to enable this behavior.- Parameters:
baseURI
- Base URI for this ConnectioncredentialsProvider
- A credentials provider
-
Connection
Constructs a new Connection object given a base URL and a credentials provider.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
- Parameters:
baseUrl
- The base URLcredentialsProvider
- A credentials provider- See Also:
-
Connection
Constructs a new Connection object with a base URL that attempts authentication via .netrc/_netrc entry, if present. If not present, connects as guest.- Parameters:
baseUrl
- The base URL- Throws:
IOException
- if there are problems reading the credentials- See Also:
-
Connection
Constructs a new Connection object for a base URL that attempts basic authentication.This is equivalent to calling
Connection(baseUrl, new BasicAuthCredentialsProvider(email, password))
. The email and password should correspond to a valid user email and password on the target server.- Parameters:
baseUrl
- The base URLemail
- The user email address to pass for authenticationpassword
- The user password to send for authentication- See Also:
-
-
Method Details
-
getBaseURI
Returns the base URI for this connection.- Returns:
- The target LabKey instance's base URI
-
getHttpClient
public org.apache.hc.client5.http.impl.classic.CloseableHttpClient getHttpClient()Returns the CloseableHttpClient object to use for this connection.- Returns:
- The CloseableHttpClient object to use.
-
beforeExecute
protected void beforeExecute(org.apache.hc.core5.http.HttpRequest request) If first request, prime the Connection by forcing authentication and populating CSRF and session info.- Parameters:
request
- HttpRequest that is about to be executed
-
afterExecute
protected void afterExecute() -
logout
Invalidates the current HTTP session on the server, if any- Throws:
IOException
- if there is an IO problem executing the command to ensure logoutCommandException
- if the server returned a non-success status code.
-
impersonate
For site-admins only, start impersonating a user.Admins may impersonate other users to perform actions on their behalf. Site admins may impersonate any user in any project. Project admins must provide a
projectPath
and may only impersonate within the project in which they have admin permission.- Parameters:
email
- The user to impersonate- Returns:
- this connection
- Throws:
IOException
- Thrown if there was an IO problem.CommandException
- if the server returned a non-success status code.- See Also:
-
impersonate
public Connection impersonate(String email, String projectPath) throws IOException, CommandException For site-admins or project-admins only, start impersonating a user.Admins may impersonate other users to perform actions on their behalf. Site admins may impersonate any user in any project. Project admins must provide a
projectPath
and may only impersonate within the project in which they have admin permission.- Parameters:
email
- The user to impersonateprojectPath
- The project path within which the user will be impersonated.- Returns:
- this connection
- Throws:
IOException
- Thrown if there was an IO problem.CommandException
- if the server returned a non-success status code.- See Also:
-
stopImpersonating
Stop impersonating a user.- Returns:
- this connection
- Throws:
IOException
- Thrown if there was an IO problem.CommandException
- if the server returned a non-success status code.
-
setTimeout
Set a default timeout for Commands that have not established their own timeouts. Null resets the Connection to the default timeout (60 seconds). 0 means the request should never timeout. NOTE: Changing this setting will force the underlying http client to be recreated.- Parameters:
timeout
- the length of the timeout waiting for the server response, in milliseconds- Returns:
- this connection
-
getTimeout
public int getTimeout()The timeout used for Commands that have not established their own timeouts. 0 means the request should never timeout.- Returns:
- the length of the timeout waiting for the server response, in milliseconds
-
setProxy
Sets the proxy host and port for this Connection.
NOTE: Changing this setting will force the underlying http client to be recreated.- Parameters:
host
- the proxy hostport
- the proxy port- Returns:
- this connection
-
isAcceptSelfSignedCerts
public boolean isAcceptSelfSignedCerts()Returns true if the connection should accept a self-signed SSL certificate when using HTTPS, false otherwise. Defaults to true.- Returns:
- true or false
-
setAcceptSelfSignedCerts
Sets the accept-self-signed certificates option. Set to false to disable automatic acceptance of self-signed SSL certificates when using HTTPS.
NOTE: Changing this setting will force the underlying http client to be recreated.- Parameters:
acceptSelfSignedCerts
- set to false to not accept self-signed certificates- Returns:
- this connection
-
getUserAgent
-
setUserAgent
-
getCredentialsProvider
-
addCookie
public Connection addCookie(String name, String value, String domain, String path, Date expiry, boolean isSecure) - Parameters:
name
- The cookie namevalue
- The cookie valuedomain
- The domain to which the cookie is visiblepath
- The path to which the cookie is visibleexpiry
- The cookie's expiration dateisSecure
- Whether the cookie requires a secure connection- Returns:
- this connection
-