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
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T extends AbstractSlingClient>
TadaptTo(Class<T> clientClass)
Returns an instance of any class extending the AbstractSlingClient.void
addValue(String key, String value)
Adds the extrakey, value
to the generic valuesvoid
close()
SlingHttpResponse
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.SlingHttpResponse
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 usingresponse.getContent()
.SlingHttpResponse
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 usingresponse.getContent()
.SlingHttpResponse
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 usingresponse.getContent()
.SlingHttpResponse
doHead(String requestPath, List<org.apache.http.NameValuePair> parameters, List<org.apache.http.Header> headers, int... expectedStatus)
Executes a HEAD requestSlingHttpResponse
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.SlingHttpResponse
doPost(String requestPath, org.apache.http.HttpEntity entity, int... expectedStatus)
Executes a POST request and consumes the entity in the response.SlingHttpResponse
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.SlingHttpResponse
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.SlingHttpResponse
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.SlingHttpResponse
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.SlingHttpResponse
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.SlingHttpResponse
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.SlingHttpResponse
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
getClientId()
Returns the unique id for this client, generated automatically during instantiation.org.apache.http.conn.ClientConnectionManager
getConnectionManager()
Deprecated.org.apache.http.client.CookieStore
getCookieStore()
org.apache.http.client.CredentialsProvider
getCredentialsProvider()
org.apache.http.params.HttpParams
getParams()
Deprecated.String
getPassword()
Returns the password of the user that will be used to authenticate the requests (by basic auth, if not replaced).URI
getPath(String url)
Extracts the relative sling path (to server url) from an url.URI
getPath(URI url)
Transforms an externalurl
into a sling path, by subtracting theserver url
(incl.URI
getUrl()
Base HTTP URI of the server under test.URI
getUrl(String path)
Gets the full URL for a given path.URI
getUrl(String path, List<org.apache.http.NameValuePair> parameters)
Creates a full URL for a given path with additional parameters.String
getUser()
Returns the name of the user that will be used to authenticate the requests (by basic auth, if not replaced).String
getValue(String key)
Gets the value forkey
from the generic valuesMap<String,String>
getValues()
Returns the extra values mapboolean
hasValue(String key)
Checks whether the handler has the given generic value
-
-
-
Method Detail
-
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 URINullPointerException
- 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 asgetUrl(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 pathparameters
- url parameters to be added to the url. If the given argument isnull
, 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 URINullPointerException
- if path is null
-
getPath
public URI getPath(URI url)
Transforms an external
url
into a sling path, by subtracting theserver url
(incl. contextPath). The returned path will not contain the context path, so it can be used withgetUrl(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 togetPath(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 parsedNullPointerException
- 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
andSlingClientConfig
- 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 forkey
from the generic values- Parameters:
key
- the key- Returns:
- the value
-
addValue
public void addValue(String key, String value)
Adds the extrakey, value
to the generic values- Parameters:
key
- the key for witch to add a valuevalue
- 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 executedheaders
- optional headers to be added to the requestexpectedStatus
- 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 executeduri
- the uri to be sent as it is (will not prepend the context path)headers
- optional headers to be added to the requestexpectedStatus
- 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 urlparameters
- optional url parameters to be addedheaders
- optional headers to be addedexpectedStatus
- 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 urlentity
- http entity to be sent by POSTheaders
- optional headers to be addedexpectedStatus
- 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 executedheaders
- optional headers to be added to the requestexpectedStatus
- 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 urlparameters
- optional url parameters to be addedheaders
- optional headers to be addedexpectedStatus
- 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 urlparameters
- optional url parameters to be addedexpectedStatus
- 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 urlexpectedStatus
- 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 urlparameters
- optional url parameters to be addedheaders
- optional headers to be addedexpectedStatus
- 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 urlentity
- the entity to be added to requestheaders
- optional headers to be addedexpectedStatus
- 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 urlentity
- the entity to be added to requestexpectedStatus
- 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 urlentity
- the entity to be added to requestheaders
- optional url parameters to be addedexpectedStatus
- 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 urlentity
- the entity to be added to requestheaders
- optional url parameters to be addedexpectedStatus
- 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 urlparameters
- optional url parameters to be addedheaders
- optional url parameters to be addedexpectedStatus
- 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
getParams
@Deprecated public org.apache.http.params.HttpParams getParams()
Deprecated.- Specified by:
getParams
in interfaceorg.apache.http.client.HttpClient
-
getConnectionManager
@Deprecated public org.apache.http.conn.ClientConnectionManager getConnectionManager()
Deprecated.- Specified by:
getConnectionManager
in interfaceorg.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 interfaceorg.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 interfaceorg.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 interfaceorg.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 interfaceorg.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 interfaceorg.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 interfaceorg.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 interfaceorg.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 interfaceorg.apache.http.client.HttpClient
- Throws:
IOException
org.apache.http.client.ClientProtocolException
-
-