@Immutable public class AbstractSlingClient extends Object implements org.apache.http.client.HttpClient, Closeable
Modifier and Type | Method and Description |
---|---|
<T extends AbstractSlingClient> |
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 |
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 using
response.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 using
response.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 using
response.getContent() . |
SlingHttpResponse |
doHead(String requestPath,
List<org.apache.http.NameValuePair> parameters,
List<org.apache.http.Header> headers,
int... expectedStatus)
Executes a HEAD request
|
SlingHttpResponse |
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.HttpHost target,
org.apache.http.HttpRequest request) |
org.apache.http.client.methods.CloseableHttpResponse |
execute(org.apache.http.HttpHost target,
org.apache.http.HttpRequest request,
org.apache.http.protocol.HttpContext context) |
<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.HttpResponse |
execute(org.apache.http.client.methods.HttpUriRequest request) |
org.apache.http.client.methods.CloseableHttpResponse |
execute(org.apache.http.client.methods.HttpUriRequest request,
org.apache.http.protocol.HttpContext context) |
<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) |
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 external
url into a sling path, by subtracting the server 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 for
key from the generic values |
Map<String,String> |
getValues()
Returns the extra values map
|
boolean |
hasValue(String key)
Checks whether the handler has the given generic value
|
protected String getClientId()
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/
public String getUser()
public String getPassword()
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
path
- the relative pathIllegalArgumentException
- if path cannot be parsed into an URINullPointerException
- if path is nullpublic URI getUrl(String path, List<org.apache.http.NameValuePair> parameters)
getUrl(String)
, but adds the parameters in the URI.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 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.IllegalArgumentException
- if path or parameters cannot be parsed into an URINullPointerException
- if path is nullpublic 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)
url
- full urlpublic URI getPath(String url)
getPath(URI)
,
except that it also parses the String int URIurl
- string containing the full urlIllegalArgumentException
- if the parameter cannot be parsedNullPointerException
- if url is nullpublic <T extends AbstractSlingClient> T adaptTo(Class<T> clientClass) throws ClientException
Returns an instance of any class extending the AbstractSlingClient. The new client will
use the the same HttpClient
and SlingClientConfig
T
- any class extending the AbstractSlingClientclientClass
- the type of client requested, identified by its ClassClientException
- if client can't be instantiatedpublic String getValue(String key)
key
from the generic valueskey
- the keypublic void addValue(String key, String value)
key, value
to the generic valueskey
- the key for witch to add a valuevalue
- the valuepublic boolean hasValue(String key)
key
- the keypublic Map<String,String> getValues()
public org.apache.http.client.CookieStore getCookieStore()
public org.apache.http.client.CredentialsProvider getCredentialsProvider()
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
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 themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic 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()
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 themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic 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
requestPath
- path relative to client urlparameters
- optional url parameters to be addedexpectedStatus
- if passed, the response status will have to match one of themClientException
- if the request could not be executedpublic 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()
.
requestPath
- path relative to client urlexpectedStatus
- if passed, the response status will have to match one of themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic 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
requestPath
- path relative to client urlentity
- the entity to be added to requestexpectedStatus
- if passed, the response status will have to match one of themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic 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
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 themClientException
- if the request could not be executedpublic void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
@Deprecated public org.apache.http.params.HttpParams getParams()
getParams
in interface org.apache.http.client.HttpClient
@Deprecated public org.apache.http.conn.ClientConnectionManager getConnectionManager()
getConnectionManager
in interface org.apache.http.client.HttpClient
public org.apache.http.HttpResponse execute(org.apache.http.client.methods.HttpUriRequest request) throws IOException, org.apache.http.client.ClientProtocolException
execute
in interface org.apache.http.client.HttpClient
IOException
org.apache.http.client.ClientProtocolException
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
execute
in interface org.apache.http.client.HttpClient
IOException
org.apache.http.client.ClientProtocolException
public org.apache.http.HttpResponse execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request) throws IOException, org.apache.http.client.ClientProtocolException
execute
in interface org.apache.http.client.HttpClient
IOException
org.apache.http.client.ClientProtocolException
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
execute
in interface org.apache.http.client.HttpClient
IOException
org.apache.http.client.ClientProtocolException
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
execute
in interface org.apache.http.client.HttpClient
IOException
org.apache.http.client.ClientProtocolException
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
execute
in interface org.apache.http.client.HttpClient
IOException
org.apache.http.client.ClientProtocolException
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
execute
in interface org.apache.http.client.HttpClient
IOException
org.apache.http.client.ClientProtocolException
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
execute
in interface org.apache.http.client.HttpClient
IOException
org.apache.http.client.ClientProtocolException
Copyright © 2007–2018 The Apache Software Foundation. All rights reserved.