public class RestClient extends Object
This client provides wrappers, helper-methods and exception-handling to facilitate requests to the REST API. It is initialized with a URL pointing to the base-location providing the services. After a successful login, a WebTarget-object can be retrieved that is then used to assemble and send requests; response-objects are returned from the server, containing the requested data and further information in the case of an error.
The Rest API client builds upon an underlying Jersey-client: detailed information about the use of the WebTarget-object (base) to build requests can be found at http://jersey.java.net/ .
Constructor and Description |
---|
RestClient(String baseUrl)
Initializes the REST-API client with a given base-URL and sets the necessary cookies;
prepares the client for a subsequent login
|
RestClient(java.util.function.Supplier<org.glassfish.jersey.client.JerseyClient> clientSupplier,
String baseUrl)
Initialize the REST-API client with a Jersey Client supplied by the given clientSupplier and the base URL.
|
Modifier and Type | Method and Description |
---|---|
void |
assertMatchingVersion()
Checks if the version of the REST API on the server is the same that is used
by the client; if there is a mismatch between the two versions, a RestException is thrown
|
void |
assertResponse(GenericResponse response)
Analyzes the response of a finished request and asserts that it was executed without errors;
if a problem occurred during the request, a specialized RestException is thrown
|
User |
authenticate(String sid,
String sessionSecret)
Authenticate with given sid and session secret
|
javax.ws.rs.client.WebTarget |
base()
Provides access to the WebTarget that is used as the base for all commands to the server
|
List<org.apache.http.cookie.Cookie> |
getCookies()
Get the cookies currently stored in the client
|
javax.ws.rs.client.Client |
getJerseyClient()
Get the underlying Jersey Client, used to define filters and configure advanced settings
|
String |
getSid()
Get the ID of the active session, as generated during login
|
void |
login(String username,
String password)
Logs the specified user into the system using the password given
|
void |
logout()
Logs out the current user
|
void |
setSid(String sid)
Set the ID of the session that should be used.
|
void |
ssologin()
Performs login on an SSO system - before this works,
necessary filters have to be defined
|
public RestClient(String baseUrl)
baseUrl
- An URL pointing to the base of the RESTful service provider
(Example: http://[hostname]/CNPortletapp/rest/)public RestClient(java.util.function.Supplier<org.glassfish.jersey.client.JerseyClient> clientSupplier, String baseUrl)
new RestClient(() -> { ClientConfig clientConfig = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider()); return JerseyClientBuilder.createClient(clientConfig).register(JacksonFeature.class).register(MultiPartFeature.class); }, "http://myhost/CNPortletapp/rest");
clientSupplier
- supplier for the Jersey ClientbaseUrl
- URL pointing to the base of the REST service provider (Example: http://[hostname]/CNPortletapp/rest/)public void login(String username, String password) throws RestException
username
- user namepassword
- passwordRestException
- If the login failedpublic void ssologin() throws RestException
RestException
- If the login via SSO failedpublic void logout() throws RestException
RestException
- If the logout failedpublic User authenticate(String sid, String sessionSecret) throws RestException
sid
- SIDsessionSecret
- session secretRestException
- if authentication failspublic void assertResponse(GenericResponse response) throws RestException
response
- The response of the request that needs to be checkedRestException
- Thrown if the request was not successful, and contains further information of the reason of failurepublic void assertMatchingVersion() throws RestException
RestException
- Mismatch between the versions detectedpublic javax.ws.rs.client.WebTarget base() throws RestException
RestException
- If no valid SID is registered with the clientpublic String getSid()
public void setSid(String sid)
public javax.ws.rs.client.Client getJerseyClient()
public List<org.apache.http.cookie.Cookie> getCookies() throws RestException
RestException
Copyright © 2024 Gentics Software. All rights reserved.