Class RestClient

java.lang.Object
com.gentics.contentnode.rest.client.RestClient
All Implemented Interfaces:
RestApi

public class RestClient extends Object implements RestApi

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 Summary

    Constructors
    Constructor
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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
    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
    jakarta.ws.rs.client.WebTarget
    Provides access to the WebTarget that is used as the base for all commands to the server
    Get the cookie handler
    List<org.apache.http.cookie.Cookie>
    Get the cookies currently stored in the client
    jakarta.ws.rs.client.Client
    Get the underlying Jersey Client, used to define filters and configure advanced settings
    void
    login(String username, String password)
    Logs the specified user into the system using the password given
    void
    Logs out the current user
    void
    setApiToken(String apiToken)
    Set the API Token, that should be used
    void
    Performs login on an SSO system - before this works, necessary filters have to be defined

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RestClient

      public 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
      Parameters:
      baseUrl - An URL pointing to the base of the RESTful service provider (Example: http://[hostname]/CNPortletapp/rest/)
    • RestClient

      public 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. The default Jersey Client is created like this:
       new RestClient(() -> {
                      ClientConfig clientConfig = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider());
                      return JerseyClientBuilder.createClient(clientConfig).register(JacksonFeature.class).register(MultiPartFeature.class);
       }, "http://myhost/CNPortletapp/rest");
       
      Parameters:
      clientSupplier - supplier for the Jersey Client
      baseUrl - URL pointing to the base of the REST service provider (Example: http://[hostname]/CNPortletapp/rest/)
  • Method Details

    • login

      public void login(String username, String password) throws RestException
      Logs the specified user into the system using the password given
      Specified by:
      login in interface RestApi
      Parameters:
      username - user name
      password - password
      Throws:
      RestException - If the login failed
    • ssologin

      public void ssologin() throws RestException
      Performs login on an SSO system - before this works, necessary filters have to be defined
      Specified by:
      ssologin in interface RestApi
      Throws:
      RestException - If the login via SSO failed
    • logout

      public void logout() throws RestException
      Logs out the current user
      Specified by:
      logout in interface RestApi
      Throws:
      RestException - If the logout failed
    • assertResponse

      public void assertResponse(GenericResponse response) throws RestException
      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
      Specified by:
      assertResponse in interface RestApi
      Parameters:
      response - The response of the request that needs to be checked
      Throws:
      RestException - Thrown if the request was not successful, and contains further information of the reason of failure
    • assertMatchingVersion

      public void assertMatchingVersion() throws RestException
      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
      Specified by:
      assertMatchingVersion in interface RestApi
      Throws:
      RestException - Mismatch between the versions detected
    • base

      public jakarta.ws.rs.client.WebTarget base()
      Provides access to the WebTarget that is used as the base for all commands to the server
      Specified by:
      base in interface RestApi
      Returns:
      The base resource
    • setApiToken

      public void setApiToken(String apiToken)
      Set the API Token, that should be used
      Specified by:
      setApiToken in interface RestApi
      Parameters:
      apiToken - api token
    • getJerseyClient

      public jakarta.ws.rs.client.Client getJerseyClient()
      Get the underlying Jersey Client, used to define filters and configure advanced settings
      Returns:
      Jersey Client
    • getCookies

      public List<org.apache.http.cookie.Cookie> getCookies() throws RestException
      Get the cookies currently stored in the client
      Specified by:
      getCookies in interface RestApi
      Returns:
      stored cookies
      Throws:
      RestException
    • getCookieHandler

      public CookieHandler getCookieHandler()
      Get the cookie handler
      Specified by:
      getCookieHandler in interface RestApi
      Returns:
      cookie handler