Class AbstractSlingClient

java.lang.Object
org.apache.sling.testing.clients.AbstractSlingClient
All Implemented Interfaces:
Closeable, AutoCloseable, org.apache.http.client.HttpClient
Direct Known Subclasses:
SlingClient

@Contract(threading=IMMUTABLE) public class AbstractSlingClient extends Object implements org.apache.http.client.HttpClient, Closeable
The abstract base client for all implementing integration test clients.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T extends AbstractSlingClient>
    T
    adaptTo(Class<T> clientClass)
    Returns an instance of any class extending the AbstractSlingClient.
    void
    addValue(String key, String value)
    Adds the extra key, value to the generic values
    void
     
    doDelete(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a DELETE request and consumes the entity in the response.
    doGet(String requestPath, int... expectedStatus)
    Executes a GET request and consumes the entity in the response (so the connection is closed immediately) The content is cached and can be retrieved using response.getContent().
    doGet(String requestPath, List<org.apache.http.NameValuePair> parameters, int... expectedStatus)
    Executes a GET request and consumes the entity in the response (so the connection is closed immediately) The content is cached and can be retrieved using response.getContent().
    doGet(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a GET request and consumes the entity in the response (so the connection is closed immediately) The content is cached and can be retrieved using response.getContent().
    doHead(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a HEAD request
    doPatch(String requestPath, org.apache.http.HttpEntity entity, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a PATCH request and consumes the entity in the response.
    doPost(String requestPath, org.apache.http.HttpEntity entity, int... expectedStatus)
    Executes a POST request and consumes the entity in the response.
    doPost(String requestPath, org.apache.http.HttpEntity entity, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a POST request and consumes the entity in the response.
    doPut(String requestPath, org.apache.http.HttpEntity entity, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a PUT request and consumes the entity in the response.
    doRawRequest(String method, String uri, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a raw HTTP request, WITHOUT consuming the entity in the response.
    doRequest(org.apache.http.client.methods.HttpUriRequest request, List<org.apache.http.Header> headers, int... expectedStatus)
    Execute an HTTP request and consumes the entity in the response.
    doStreamGet(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a GET request WITHOUT consuming the entity in the response.
    doStreamPost(String requestPath, org.apache.http.HttpEntity entity, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes a POST request WITHOUT consuming the entity in the response.
    doStreamRequest(org.apache.http.client.methods.HttpUriRequest request, List<org.apache.http.Header> headers, int... expectedStatus)
    Executes an HTTP request, WITHOUT consuming the entity in the response.
    org.apache.http.HttpResponse
    execute(org.apache.http.client.methods.HttpUriRequest request)
     
    <T> T
    execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.ResponseHandler<? extends T> responseHandler)
     
    <T> T
    execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.ResponseHandler<? extends T> responseHandler, org.apache.http.protocol.HttpContext context)
     
    org.apache.http.client.methods.CloseableHttpResponse
    execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.protocol.HttpContext context)
     
    org.apache.http.HttpResponse
    execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request)
     
    <T> T
    execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.client.ResponseHandler<? extends T> responseHandler)
     
    <T> T
    execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.client.ResponseHandler<? extends T> responseHandler, org.apache.http.protocol.HttpContext context)
     
    org.apache.http.client.methods.CloseableHttpResponse
    execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context)
     
    protected String
    Returns the unique id for this client, generated automatically during instantiation.
    org.apache.http.conn.ClientConnectionManager
    Deprecated.
    org.apache.http.client.CookieStore
     
    org.apache.http.client.CredentialsProvider
     
    org.apache.http.params.HttpParams
    Deprecated.
    Returns the password of the user that will be used to authenticate the requests (by basic auth, if not replaced).
    Extracts the relative sling path (to server url) from an url.
    getPath(URI url)
    Transforms an external url into a sling path, by subtracting the server url (incl.
    Base HTTP URI of the server under test.
    getUrl(String path)
    Gets the full URL for a given path.
    getUrl(String path, List<org.apache.http.NameValuePair> parameters)
    Creates a full URL for a given path with additional parameters.
    Returns the name of the user that will be used to authenticate the requests (by basic auth, if not replaced).
    Gets the value for key from the generic values
    Returns the extra values map
    boolean
    Checks whether the handler has the given generic value

    Methods inherited from class java.lang.Object

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

    • getClientId

      protected String getClientId()
      Returns the unique id for this client, generated automatically during instantiation.
      Returns:
      client's unique id
    • getUrl

      public URI getUrl()

      Base HTTP URI of the server under test. It includes the context path, if present, and always ends with a slash

      Example: http://localhost:8080/a/

      Returns:
      the server's URL
    • getUser

      public String getUser()
      Returns the name of the user that will be used to authenticate the requests (by basic auth, if not replaced).
      Returns:
      user's name
    • getPassword

      public String getPassword()
      Returns the password of the user that will be used to authenticate the requests (by basic auth, if not replaced).
      Returns:
      user's password
    • getUrl

      public URI getUrl(String path)

      Gets the full URL for a given path.

      The input path is considered relative to server url path ("/" or context path), even though it starts with a slash. The path is relativized and appended to the server url.

      Note: in the case of a server url with context path - the input path should not contain the context path, otherwise it will be duplicated in the resulting url

      Parameters:
      path - the relative path
      Returns:
      the absolute URI
      Throws:
      IllegalArgumentException - if path cannot be parsed into an URI
      NullPointerException - if path is null
    • getUrl

      public URI getUrl(String path, List<org.apache.http.NameValuePair> parameters)
      Creates a full URL for a given path with additional parameters. Same as getUrl(String), but adds the parameters in the URI.
      Parameters:
      path - path relative to server url; can start with / but should not include the server context path
      parameters - url parameters to be added to the url. If the given argument is null, nothing will be added to the url. If the given argument is an empty array, it will force a "?" at the end of the url.
      Returns:
      full url as URI
      Throws:
      IllegalArgumentException - if path or parameters cannot be parsed into an URI
      NullPointerException - if path is null
    • getPath

      public URI getPath(URI url)

      Transforms an external url into a sling path, by subtracting the server url (incl. contextPath). The returned path will not contain the context path, so it can be used with getUrl(String)

      The url can be absolute (incl. hostname) or relative to root (starts with "/").

      If the server url is not a prefix of the given url, it returns the given url

      If the url is just a path, it returns the path (with leading slash if not already present)

      Parameters:
      url - full url
      Returns:
      sling path
    • getPath

      public URI getPath(String url)
      Extracts the relative sling path (to server url) from an url. Identical to getPath(URI), except that it also parses the String int URI
      Parameters:
      url - string containing the full url
      Returns:
      relative path as URI
      Throws:
      IllegalArgumentException - if the parameter cannot be parsed
      NullPointerException - if url is null
    • adaptTo

      public <T extends AbstractSlingClient> T adaptTo(Class<T> clientClass) throws TestingValidationException

      Returns an instance of any class extending the AbstractSlingClient. The new client will use the the same HttpClient and SlingClientConfig

      Type Parameters:
      T - any class extending the AbstractSlingClient
      Parameters:
      clientClass - the type of client requested, identified by its Class
      Returns:
      instance of a class extending the AbstractSlingClient
      Throws:
      TestingValidationException - if client can't be instantiated
    • getValue

      public String getValue(String key)
      Gets the value for key from the generic values
      Parameters:
      key - the key
      Returns:
      the value
    • addValue

      public void addValue(String key, String value)
      Adds the extra key, value to the generic values
      Parameters:
      key - the key for witch to add a value
      value - the value
    • hasValue

      public boolean hasValue(String key)
      Checks whether the handler has the given generic value
      Parameters:
      key - the key
      Returns:
      true if the value was found
    • getValues

      public Map<String,String> getValues()
      Returns the extra values map
      Returns:
      the map of values
    • getCookieStore

      public org.apache.http.client.CookieStore getCookieStore()
      Returns:
      the cookie store reference
    • getCredentialsProvider

      public org.apache.http.client.CredentialsProvider getCredentialsProvider()
      Returns:
      the credentials provider
    • doStreamRequest

      public SlingHttpResponse doStreamRequest(org.apache.http.client.methods.HttpUriRequest request, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes an HTTP request, WITHOUT consuming the entity in the response. The caller is responsible for consuming the entity or closing the response's InputStream in order to release the connection. Otherwise, the client might run out of connections and will block

      Use this with caution and only if necessary for streaming, otherwise use the safe method doRequest(HttpUriRequest, List, int...)

      Adds the headers and checks the response against expected status

      Parameters:
      request - the request to be executed
      headers - optional headers to be added to the request
      expectedStatus - if passed, the response status is checked against it/them, and has to match at least one of them
      Returns:
      the response, with the entity not consumed
      Throws:
      ClientException - if the request could not be executed
    • doRawRequest

      public SlingHttpResponse doRawRequest(String method, String uri, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a raw HTTP request, WITHOUT consuming the entity in the response. The caller is responsible for consuming the entity or closing the response's InputStream in order to release the connection. Otherwise, the client might run out of connections and will block

      Use this with caution and only if necessary for custom methods or for paths that must not be encoded, otherwise use the safe method doRequest(HttpUriRequest, List, int...)

      It behaves as doStreamRequest(HttpUriRequest, List, int...), so the entity is not consumed.

      Adds the headers and checks the response against expected status

      Parameters:
      method - the request to be executed
      uri - the uri to be sent as it is (will not prepend the context path)
      headers - optional headers to be added to the request
      expectedStatus - if passed, the response status is checked against it/them, and has to match at least one of them
      Returns:
      the response, with the entity not consumed
      Throws:
      ClientException - if the request could not be executed
    • doStreamGet

      public SlingHttpResponse doStreamGet(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a GET request WITHOUT consuming the entity in the response. The caller is responsible to close the connection. Otherwise, the client might run out of connections and will block

      Use this with caution and only if necessary for streaming, otherwise use the safe method doGet(String, List, List, int...)

      Adds the given parameters and headers and checks the response against expected status

      Parameters:
      requestPath - path relative to client url
      parameters - optional url parameters to be added
      headers - optional headers to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity not consumed
      Throws:
      ClientException - if the request could not be executed
    • doStreamPost

      public SlingHttpResponse doStreamPost(String requestPath, org.apache.http.HttpEntity entity, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a POST request WITHOUT consuming the entity in the response. The caller is responsible to close the connection

      Use this with caution and only if necessary for streaming, otherwise use the safe method doPost(String, HttpEntity, List, int...)

      Adds the headers and checks the response against expected status

      Parameters:
      requestPath - path relative to client url
      entity - http entity to be sent by POST
      headers - optional headers to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity not consumed
      Throws:
      ClientException - if the request could not be executed
    • doRequest

      public SlingHttpResponse doRequest(org.apache.http.client.methods.HttpUriRequest request, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Execute an HTTP request and consumes the entity in the response. The content is cached and can be retrieved using response.getContent(). This method is safe to use because it closes the entity so the caller has no responsibility.

      This means the response entity SHOULD NOT BE USED to read the content, e.g. response.getEntity().getContent()

      Parameters:
      request - the request to be executed
      headers - optional headers to be added to the request
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed and the content cached
      Throws:
      ClientException - if the request could not be executed
    • doGet

      public SlingHttpResponse doGet(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a GET request and consumes the entity in the response (so the connection is closed immediately) The content is cached and can be retrieved using response.getContent().

      Adds the passed parameters and headers and checks the expected status

      Parameters:
      requestPath - path relative to client url
      parameters - optional url parameters to be added
      headers - optional headers to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed amd the content cached
      Throws:
      ClientException - if the request could not be executed
    • doGet

      public SlingHttpResponse doGet(String requestPath, List<org.apache.http.NameValuePair> parameters, int... expectedStatus) throws ClientException

      Executes a GET request and consumes the entity in the response (so the connection is closed immediately) The content is cached and can be retrieved using response.getContent().

      Adds the passed parameters and checks the expected status

      Parameters:
      requestPath - path relative to client url
      parameters - optional url parameters to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed amd the content cached
      Throws:
      ClientException - if the request could not be executed
    • doGet

      public SlingHttpResponse doGet(String requestPath, int... expectedStatus) throws ClientException

      Executes a GET request and consumes the entity in the response (so the connection is closed immediately) The content is cached and can be retrieved using response.getContent().

      Parameters:
      requestPath - path relative to client url
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed amd the content cached
      Throws:
      ClientException - if the request could not be executed
    • doHead

      public SlingHttpResponse doHead(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a HEAD request

      Adds the passed parameters and headers and checks the expected status

      Parameters:
      requestPath - path relative to client url
      parameters - optional url parameters to be added
      headers - optional headers to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response
      Throws:
      ClientException - if the request could not be executed
    • doPost

      public SlingHttpResponse doPost(String requestPath, org.apache.http.HttpEntity entity, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a POST request and consumes the entity in the response. The content is cached and be retrieved by calling response.getContent()

      Adds the passed entity and headers and checks the expected status

      Parameters:
      requestPath - path relative to client url
      entity - the entity to be added to request
      headers - optional headers to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed and the content cached
      Throws:
      ClientException - if the request could not be executed
    • doPost

      public SlingHttpResponse doPost(String requestPath, org.apache.http.HttpEntity entity, int... expectedStatus) throws ClientException

      Executes a POST request and consumes the entity in the response. The content is cached and be retrieved by calling response.getContent()

      Adds the passed entity and checks the expected status

      Parameters:
      requestPath - path relative to client url
      entity - the entity to be added to request
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed and the content cached
      Throws:
      ClientException - if the request could not be executed
    • doPut

      public SlingHttpResponse doPut(String requestPath, org.apache.http.HttpEntity entity, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a PUT request and consumes the entity in the response. The content is cached and be retrieved by calling response.getContent()

      Adds the passed entity and headers and checks the expected status

      Parameters:
      requestPath - path relative to client url
      entity - the entity to be added to request
      headers - optional url parameters to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed and the content cached
      Throws:
      ClientException - if the request could not be executed
    • doPatch

      public SlingHttpResponse doPatch(String requestPath, org.apache.http.HttpEntity entity, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a PATCH request and consumes the entity in the response. The content is cached and be retrieved by calling response.getContent()

      Adds the passed entity and headers and checks the expected status

      Parameters:
      requestPath - path relative to client url
      entity - the entity to be added to request
      headers - optional url parameters to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed and the content cached
      Throws:
      ClientException - if the request could not be executed
    • doDelete

      public SlingHttpResponse doDelete(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus) throws ClientException

      Executes a DELETE request and consumes the entity in the response. The content is cached and be retrieved by calling response.getContent()

      Adds the passed parameters and headers and checks the expected status

      Parameters:
      requestPath - path relative to client url
      parameters - optional url parameters to be added
      headers - optional url parameters to be added
      expectedStatus - if passed, the response status will have to match one of them
      Returns:
      the response with the entity consumed and the content cached
      Throws:
      ClientException - if the request could not be executed
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getParams

      @Deprecated public org.apache.http.params.HttpParams getParams()
      Deprecated.
      Specified by:
      getParams in interface org.apache.http.client.HttpClient
    • getConnectionManager

      @Deprecated public org.apache.http.conn.ClientConnectionManager getConnectionManager()
      Deprecated.
      Specified by:
      getConnectionManager in interface org.apache.http.client.HttpClient
    • execute

      public org.apache.http.HttpResponse execute(org.apache.http.client.methods.HttpUriRequest request) throws IOException, org.apache.http.client.ClientProtocolException
      Specified by:
      execute in interface org.apache.http.client.HttpClient
      Throws:
      IOException
      org.apache.http.client.ClientProtocolException
    • execute

      public org.apache.http.client.methods.CloseableHttpResponse execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.protocol.HttpContext context) throws IOException, org.apache.http.client.ClientProtocolException
      Specified by:
      execute in interface org.apache.http.client.HttpClient
      Throws:
      IOException
      org.apache.http.client.ClientProtocolException
    • execute

      public org.apache.http.HttpResponse execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request) throws IOException, org.apache.http.client.ClientProtocolException
      Specified by:
      execute in interface org.apache.http.client.HttpClient
      Throws:
      IOException
      org.apache.http.client.ClientProtocolException
    • execute

      public org.apache.http.client.methods.CloseableHttpResponse execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context) throws IOException, org.apache.http.client.ClientProtocolException
      Specified by:
      execute in interface org.apache.http.client.HttpClient
      Throws:
      IOException
      org.apache.http.client.ClientProtocolException
    • execute

      public <T> T execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.ResponseHandler<? extends T> responseHandler) throws IOException, org.apache.http.client.ClientProtocolException
      Specified by:
      execute in interface org.apache.http.client.HttpClient
      Throws:
      IOException
      org.apache.http.client.ClientProtocolException
    • execute

      public <T> T execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.ResponseHandler<? extends T> responseHandler, org.apache.http.protocol.HttpContext context) throws IOException, org.apache.http.client.ClientProtocolException
      Specified by:
      execute in interface org.apache.http.client.HttpClient
      Throws:
      IOException
      org.apache.http.client.ClientProtocolException
    • execute

      public <T> T execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.client.ResponseHandler<? extends T> responseHandler) throws IOException, org.apache.http.client.ClientProtocolException
      Specified by:
      execute in interface org.apache.http.client.HttpClient
      Throws:
      IOException
      org.apache.http.client.ClientProtocolException
    • execute

      public <T> T execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.client.ResponseHandler<? extends T> responseHandler, org.apache.http.protocol.HttpContext context) throws IOException, org.apache.http.client.ClientProtocolException
      Specified by:
      execute in interface org.apache.http.client.HttpClient
      Throws:
      IOException
      org.apache.http.client.ClientProtocolException