@Immutable public class SlingClient extends AbstractSlingClient
The Base class for all Integration Test Clients. It provides generic methods to send HTTP requests to a server.
It has methods to perform simple node operations on the server like creating and deleting nodes, etc. on the server using requests.
Modifier and Type | Class and Description |
---|---|
static class |
SlingClient.Builder |
static class |
SlingClient.InternalBuilder<T extends SlingClient>
Extensible InternalBuilder for SlingClient.
|
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_NODE_TYPE |
Constructor and Description |
---|
SlingClient(org.apache.http.impl.client.CloseableHttpClient http,
SlingClientConfig config)
Constructor used by Builders and adaptTo().
|
SlingClient(URI url,
String user,
String password)
Handy constructor easy to use in simple tests.
|
Modifier and Type | Method and Description |
---|---|
SlingHttpResponse |
createFolder(String folderName,
String folderTitle,
String parentPath,
int... expectedStatus)
Creates a new Folder of type sling:OrderedFolder.
|
SlingHttpResponse |
createNode(String path,
String nodeType)
Creates the node specified by a given path with the given node type.
If the given node type is null , the node will be created with the default type: "sling:OrderedFolder".If the node already exists, the method will return null, with no errors. The method ignores trailing slashes so a path like this /a/b/c/// is accepted and will create the c node if the rest of the path exists. |
SlingHttpResponse |
createNodeRecursive(String path,
String nodeType)
Recursively creates all the none existing nodes in the given path using the
createNode(String, String) method. |
SlingHttpResponse |
deletePath(String path,
int... expectedStatus)
Deletes a sling path (:operation delete)
|
org.codehaus.jackson.JsonNode |
doGetJson(String path,
int depth,
int... expectedStatus)
Returns the
JsonNode object corresponding to a content node. |
boolean |
exists(String path)
Checks whether a path exists or not by making a GET request to that path with the
json extension |
org.codehaus.jackson.JsonNode |
getJsonNode(String path,
int depth)
Deprecated.
use
waitExists(String, long, long) and doGetJson(String, int, int...) instead |
org.codehaus.jackson.JsonNode |
getJsonNode(String path,
int depth,
long waitMillis,
int retryNumber,
int... expectedStatus)
Deprecated.
use
waitExists(String, long, long) and doGetJson(String, int, int...) instead |
protected String |
getNodeNameFromPath(String path)
Extracts the node from path
|
protected String |
getParentPath(String path)
Extracts the parent path from the given String
|
String |
getUUId(org.codehaus.jackson.JsonNode jsonNode)
Get the UUID from a node that was already parsed in a
JsonNode |
String |
getUUID(String path)
Get the UUID of a repository path
|
SlingHttpResponse |
importContent(String parentPath,
String contentType,
File contentFile,
int... expectedStatus)
Create a tree structure under
parentPath by providing a contentFile in one
of the supported formats: xml, jcr.xml, json, jar, zip. |
SlingHttpResponse |
importContent(String parentPath,
String contentType,
String content,
int... expectedStatus)
Create a tree structure under
parentPath by providing a content in one
of the supported formats: xml, jcr.xml, json, jar, zip. |
SlingHttpResponse |
importJson(String parentPath,
org.codehaus.jackson.JsonNode json,
int... expectedStatus)
Wrapper method over
importContent(String, String, String, int...) for directly importing a json node |
SlingHttpResponse |
move(String srcPath,
String destPath,
int... expectedStatus)
Moves a sling path to a new location (:operation move)
|
SlingHttpResponse |
setPropertiesString(String nodePath,
List<org.apache.http.NameValuePair> properties,
int... expectedStatus)
Sets multiple String properties on a node in a single request
|
SlingHttpResponse |
setPropertyString(String nodePath,
String propName,
String propValue,
int... expectedStatus)
Sets String component property on a node.
|
SlingHttpResponse |
setPropertyStringArray(String nodePath,
String propName,
List<String> propValueList,
int... expectedStatus)
Sets a String[] component property on a node.
|
SlingHttpResponse |
upload(File file,
String mimeType,
String toPath,
boolean createFolders,
int... expectedStatus)
Uploads a file to the repository.
|
void |
waitExists(String path,
long timeout,
long delay)
Waits until a path exists by making successive GET requests to that path with the
json extension |
void |
waitUntilExists(String path,
long waitMillis,
int retryCount)
Deprecated.
use
waitExists(String, long, long) instead. |
adaptTo, addValue, close, doDelete, doGet, doGet, doGet, doHead, doPatch, doPost, doPost, doPut, doRawRequest, doRequest, doStreamGet, doStreamPost, doStreamRequest, execute, execute, execute, execute, execute, execute, execute, execute, getClientId, getConnectionManager, getCookieStore, getCredentialsProvider, getParams, getPassword, getPath, getPath, getUrl, getUrl, getUrl, getUser, getValue, getValues, hasValue
public static final String DEFAULT_NODE_TYPE
public SlingClient(org.apache.http.impl.client.CloseableHttpClient http, SlingClientConfig config) throws ClientException
http
- the underlying HttpClient to be usedconfig
- sling specific configsClientException
- if the client could not be createdAbstractSlingClient.AbstractSlingClient(CloseableHttpClient, SlingClientConfig)
public SlingClient(URI url, String user, String password) throws ClientException
Handy constructor easy to use in simple tests. Creates a client that uses basic authentication.
For constructing clients with complex configurations, use a SlingClient.InternalBuilder
For constructing clients with the same configuration, but a different class, use AbstractSlingClient.adaptTo(Class)
url
- url of the server (including context path)user
- username for basic authenticationpassword
- password for basic authenticationClientException
- never, kept for uniformity with the other constructorspublic SlingHttpResponse move(String srcPath, String destPath, int... expectedStatus) throws ClientException
srcPath
- source pathdestPath
- destination pathexpectedStatus
- list of accepted status codes in responseClientException
- if an error occurs during operationpublic SlingHttpResponse deletePath(String path, int... expectedStatus) throws ClientException
path
- path to be deletedexpectedStatus
- list of accepted status codes in responseClientException
- if an error occurs during operationpublic SlingHttpResponse createNodeRecursive(String path, String nodeType) throws ClientException
createNode(String, String)
method.
All the created nodes will have the given node type.path
- the path to use for creating all the none existing nodesnodeType
- the node type to use for the created nodesClientException
- if one of the nodes can't be createdpublic SlingHttpResponse createNode(String path, String nodeType) throws ClientException
null
, the node will be created with the default type: "sling:OrderedFolder".path
- the path to the node to createnodeType
- the type of the node to createClientException
- if the node can't be createdpublic boolean exists(String path) throws ClientException
Checks whether a path exists or not by making a GET request to that path with the json
extension
path
- path to be checkedClientException
- if the request could not be performedprotected String getParentPath(String path)
path
- string containing the pathprotected String getNodeNameFromPath(String path)
path
- string containing the path@Deprecated public void waitUntilExists(String path, long waitMillis, int retryCount) throws ClientException, InterruptedException
waitExists(String, long, long)
instead.Checks whether a path exists or not by making a GET request to that path with the json extension
It polls the server and waits until the path exists
path
- path to be checkedwaitMillis
- time to wait between retriesretryCount
- number of retries before throwing an exceptionClientException
- if the path was not foundInterruptedException
- to mark this operation as "waiting"public void waitExists(String path, long timeout, long delay) throws TimeoutException, InterruptedException
Waits until a path exists by making successive GET requests to that path with the json extension
Polls the server until the path exists or until timeout is reached
path
- path to be checkedtimeout
- max total time to wait, in millisecondsdelay
- time to wait between checks, in millisecondsTimeoutException
- if the path was not found before timeoutInterruptedException
- to mark this operation as "waiting", should be rethrown by callerspublic SlingHttpResponse setPropertyString(String nodePath, String propName, String propValue, int... expectedStatus) throws ClientException
nodePath
- path to the node to be editedpropName
- name of the property to be editedpropValue
- value of the property to be editedexpectedStatus
- list of expected HTTP Status to be returned, if not set, 200 is assumed.ClientException
- if something fails during the request/response cyclepublic SlingHttpResponse setPropertyStringArray(String nodePath, String propName, List<String> propValueList, int... expectedStatus) throws ClientException
nodePath
- path to the node to be editedpropName
- name of the property to be editedpropValueList
- List of String valuesexpectedStatus
- list of expected HTTP Status to be returned, if not set, 200 is assumed.ClientException
- if something fails during the request/response cyclepublic SlingHttpResponse setPropertiesString(String nodePath, List<org.apache.http.NameValuePair> properties, int... expectedStatus) throws ClientException
nodePath
- path to the node to be editedproperties
- list of NameValue pairs with the name and value for each property. String[] properties can be defined
by adding multiple time the same property name with different valuesexpectedStatus
- list of expected HTTP Status to be returned, if not set, 200 is assumed.ClientException
- if the operation could not be completed@Deprecated public org.codehaus.jackson.JsonNode getJsonNode(String path, int depth) throws ClientException, InterruptedException
waitExists(String, long, long)
and doGetJson(String, int, int...)
insteadJsonNode
.path
- the path to the content nodedepth
- the number of levels to go down the tree, -1 for infinityJsonNode
mapping to the requested content node.ClientException
- if something fails during request/response processingInterruptedException
- to mark this operation as "waiting"@Deprecated public org.codehaus.jackson.JsonNode getJsonNode(String path, int depth, long waitMillis, int retryNumber, int... expectedStatus) throws ClientException, InterruptedException
waitExists(String, long, long)
and doGetJson(String, int, int...)
insteadJsonNode
.path
- the path to the content nodedepth
- the number of levels to go down the tree, -1 for infinitywaitMillis
- how long it should wait between requestsretryNumber
- number of retries before throwing an exceptionexpectedStatus
- list of allowed HTTP Status to be returned. If not set,
http status 200 (OK) is assumed.JsonNode
mapping to the requested content node.ClientException
- if something fails during request/response cycleInterruptedException
- to mark this operation as "waiting"public org.codehaus.jackson.JsonNode doGetJson(String path, int depth, int... expectedStatus) throws ClientException
JsonNode
object corresponding to a content node.path
- the path to the content nodedepth
- the number of levels to go down the tree, -1 for infinityexpectedStatus
- list of allowed HTTP Status to be returned. If not set, 200 (OK) is assumed.JsonNode
mapping to the requested content node.ClientException
- if the path does not exist or something fails during request/response cyclepublic SlingHttpResponse upload(File file, String mimeType, String toPath, boolean createFolders, int... expectedStatus) throws ClientException
nt:file
. The intermediary nodes are created with
type "sling:OrderedFolder" if parameter createFolders
is truefile
- the file to be uploadedmimeType
- the MIME Type of the filetoPath
- the complete path of the file in the repository including file namecreateFolders
- if true, all non existing parent nodes will be created using node type sling:OrderedFolder
expectedStatus
- list of expected HTTP Status to be returned, if not set, 201 is assumed.ClientException
- if something fails during the request/response cyclepublic SlingHttpResponse createFolder(String folderName, String folderTitle, String parentPath, int... expectedStatus) throws ClientException
New Folder...
in the Site Admin.folderName
- The name of the folder to be used in the URL.folderTitle
- Title of the Folder to be set in jcr:titleparentPath
- The parent path where the folder gets added.expectedStatus
- list of expected HTTP Status to be returned, if not set, 201 is assumed.ClientException
- if something fails during the request/response cyclepublic SlingHttpResponse importContent(String parentPath, String contentType, String content, int... expectedStatus) throws ClientException
Create a tree structure under parentPath
by providing a content
in one
of the supported formats: xml, jcr.xml, json, jar, zip.
This is the implementation of :operation import
, as documented in
importing-content-structures
parentPath
- path where the tree is createdcontentType
- format of the contentcontent
- string expressing the structure to be created, in the specified formatexpectedStatus
- list of expected HTTP Status to be returned, if not set, 201 is assumedClientException
- if something fails during the request/response cyclepublic SlingHttpResponse importContent(String parentPath, String contentType, File contentFile, int... expectedStatus) throws ClientException
Create a tree structure under parentPath
by providing a contentFile
in one
of the supported formats: xml, jcr.xml, json, jar, zip.
This is the implementation of :operation import
, as documented in
importing-content-structures
parentPath
- path where the tree is createdcontentType
- format of the contentcontentFile
- file containing the structure to be created, in the specified formatexpectedStatus
- list of expected HTTP Status to be returned, if not set, 200 is assumedClientException
- if something fails during the request/response cyclepublic SlingHttpResponse importJson(String parentPath, org.codehaus.jackson.JsonNode json, int... expectedStatus) throws ClientException
importContent(String, String, String, int...)
for directly importing a json nodeparentPath
- path where the tree is createdjson
- json node with the desired structureexpectedStatus
- list of expected HTTP Status to be returned, if not set, 201 is assumedClientException
- if something fails during the request/response cyclepublic String getUUID(String path) throws ClientException
path
- path in repositoryClientException
- if something fails during request/response cyclepublic String getUUId(org.codehaus.jackson.JsonNode jsonNode) throws ClientException
JsonNode
jsonNode
- JsonNode
object of the repository nodeClientException
- if something fails during request/response cycleCopyright © 2007–2018 The Apache Software Foundation. All rights reserved.