Package org.apache.sling.testing.clients.osgi

# Class OsgiConsoleClient

java.lang.Object
org.apache.sling.testing.clients.AbstractSlingClient
org.apache.sling.testing.clients.SlingClient
org.apache.sling.testing.clients.osgi.OsgiConsoleClient

All Implemented Interfaces:
  `Closeable`, `AutoCloseable`, `org.apache.http.client.HttpClient`

public class OsgiConsoleClient extends SlingClient
A client that wraps the Felix OSGi Web Console REST API calls.

See Also:
  - Web Console RESTful API

## Nested Class Summary

## Nested classes/interfaces inherited from class org.apache.sling.testing.clients.SlingClient

`SlingClient.Builder, SlingClient.InternalBuilder<T extends SlingClient>`

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`JSON_KEY_DATA`
`static final String`
`JSON_KEY_ID`
`static final String`
`JSON_KEY_STATE`
`static final String`
`JSON_KEY_VERSION`

### Fields inherited from class org.apache.sling.testing.clients.SlingClient

`CLIENT_CONNECTION_TIMEOUT_PROP, DEFAULT_NODE_TYPE, SUDO_COOKIE_NAME`

## Constructor Summary

Constructors
Constructor
Description
`OsgiConsoleClient(URI serverUrl, String userName, String password)`
Default constructor.
`OsgiConsoleClient(org.apache.http.impl.client.CloseableHttpClient http, SlingClientConfig config)`
Constructor used by adaptTo() and InternalBuilder classes.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`checkBundleInstalled(String symbolicName, int waitTime, int retries)`
Deprecated.
does not respect polling practices; use `waitBundleInstalled(String, long, long)` instead
`SlingHttpResponse`
`deleteConfiguration(String pid, int... expectedStatus)`
Delete the config referenced by the PID
`String`
`editConfiguration(String PID, String factoryPID, Map<String,Object> configProperties, int... expectedStatus)`
Sets properties of a config referenced by its PID.
`String`
`editConfigurationWithWait(int waitCount, String PID, String factoryPID, Map<String,Object> configProperties, int... expectedStatus)`
Deprecated.
use `waitEditConfiguration(long, String, String, Map, int...)`
`long`
`getBundleId(String symbolicName)`
Get the id of the bundle
`BundleInfo`
`getBundleInfo(String id, int... expectedStatus)`
Returns the wrapper for the bundle info json
`BundlesInfo`
`getBundlesInfo(int... expectedStatus)`
Returns the wrapper for the bundles info json
`String`
`getBundleState(String symbolicName)`
Get the state of the bundle
`static String`
`getBundleSymbolicName(File bundleFile)`
Get the symbolic name from a bundle file by looking at the manifest
`String`
`getBundleVersion(String symbolicName)`
Get the version of the bundle
`static String`
`getBundleVersionFromFile(File bundleFile)`
Get the version form a bundle file by looking at the manifest
`ComponentInfo`
`getComponentInfo(String id, int expectedStatus)`
Returns the wrapper for the component info json
`ComponentsInfo`
`getComponentsInfo(int... expectedStatus)`
Returns the wrapper for the components info json
`String`
`getConfigPIDFromServices(String serviceType, String propertyName, String propertyValue, long timeout, long delay)`
Performs a search of a config PID through the service information.
`Map<String,Object>`
`getConfiguration(String pid, int... expectedStatus)`
Returns a map of all properties set for the config referenced by the PID, where the map keys are the property names.
`Map<String,Object>`
`getConfigurationWithWait(long waitCount, String pid, int... expectedStatus)`
Deprecated.
use `waitGetConfiguration(long, String, int...)`
`Map<String,Object>`
`getOSGiConfiguration(String pid, int... expectedStatus)`
Returns a map of all properties set for the config referenced by the PID, where the map keys are the property names.
`SlingHttpResponse`
`installBundle(File f, boolean startBundle)`
Install a bundle using the Felix webconsole HTTP interface
`SlingHttpResponse`
`installBundle(File f, boolean startBundle, int startLevel)`
Install a bundle using the Felix webconsole HTTP interface, with a specific start level
`boolean`
`installBundleWithRetry(File f, boolean startBundle, int startLevel, int waitTime, int retries)`
Deprecated.
`waitInstallBundle(File, boolean, int, long, long)`
`void`
`refreshPackages()`
Calls PackageAdmin.refreshPackages to force re-wiring of all the bundles.
`void`
`startBundle(String symbolicName)`
Starts a bundle
`void`
`startBundlewithWait(String symbolicName, int waitTime, int retries)`
Deprecated.
use `waitStartBundle(String, long, long)`
`void`
`stopBundle(String symbolicName)`
Stop a bundle
`SlingHttpResponse`
`uninstallBundle(String symbolicName)`
Uninstall a bundle
`void`
`waitBundleInstalled(String symbolicName, long timeout, long delay)`
Wait until the bundle is installed.
`void`
`waitBundleStarted(String symbolicName, long timeout, long delay)`
Wait until the bundle is started
`void`
`waitComponentRegistered(String componentName, long timeout, long delay)`
Wait until the component with the given name is registered.
`String`
`waitEditConfiguration(long timeout, String PID, String factoryPID, Map<String,Object> configProperties, int... expectedStatus)`
Sets properties of a config referenced by its PID.
`Map<String,Object>`
`waitGetConfiguration(long timeout, String pid, int... expectedStatus)`
Returns a map of all properties set for the config referenced by the PID, where the map keys are the property names.
`void`
`waitInstallBundle(File f, boolean startBundle, int startLevel, long timeout, long delay)`
Install a bundle using the Felix webconsole HTTP interface and wait for it to be installed.
`void`
`waitServiceRegistered(String type, String bundleSymbolicName, long timeout, long delay)`
Wait until the service with the given name is registered.
`void`
`waitStartBundle(String symbolicName, long timeout, long delay)`
Starts a bundle and waits for it to be started

### Methods inherited from class org.apache.sling.testing.clients.SlingClient

`createFolder, createNode, createNodeRecursive, deletePath, doGetJson, endImpersonation, exists, getJsonNode, getJsonNode, getNodeNameFromPath, getParentPath, getUser, getUUId, getUUID, impersonate, importContent, importContent, importJson, move, setPropertiesString, setPropertyString, setPropertyStringArray, upload, waitExists, waitUntilExists`

### Methods inherited from class org.apache.sling.testing.clients.AbstractSlingClient

`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, getValue, getValues, hasValue`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### JSON_KEY_ID

public static final String JSON_KEY_ID

See Also:
  - Constant Field Values

### JSON_KEY_VERSION

public static final String JSON_KEY_VERSION

See Also:
  - Constant Field Values

### JSON_KEY_DATA

public static final String JSON_KEY_DATA

See Also:
  - Constant Field Values

### JSON_KEY_STATE

public static final String JSON_KEY_STATE

See Also:
  - Constant Field Values

## Constructor Details

### OsgiConsoleClient

public OsgiConsoleClient(URI serverUrl, String userName, String password) throws ClientException
Default constructor. Simply calls `SlingClient(URI, String, String)`

Parameters:
  `serverUrl` - the URL to the server under test
  `userName` - the username used for authentication
  `password` - the password for this user
Throws:
  `ClientException` - if the client cannot be instantiated

### OsgiConsoleClient

public OsgiConsoleClient(org.apache.http.impl.client.CloseableHttpClient http, SlingClientConfig config) throws ClientException
Constructor used by adaptTo() and InternalBuilder classes. Should not be called directly in the code

Parameters:
  `http` - http client to be used for requests
  `config` - sling specific configs
Throws:
  `ClientException` - if the client cannot be instantiated

## Method Details

### getBundlesInfo

public BundlesInfo getBundlesInfo(int... expectedStatus) throws ClientException
Returns the wrapper for the bundles info json

Parameters:
  `expectedStatus` - list of accepted statuses of the response
Returns:
  all the bundles info
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus

### getBundleInfo

public BundleInfo getBundleInfo(String id, int... expectedStatus) throws ClientException
Returns the wrapper for the bundle info json

Parameters:
  `id` - the id of the bundle
  `expectedStatus` - list of accepted statuses of the response
Returns:
  the bundle info
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus

### getComponentsInfo

public ComponentsInfo getComponentsInfo(int... expectedStatus) throws ClientException
Returns the wrapper for the components info json

Parameters:
  `expectedStatus` - list of accepted statuses of the response
Returns:
  the components info
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus

### getComponentInfo

public ComponentInfo getComponentInfo(String id, int expectedStatus) throws ClientException
Returns the wrapper for the component info json

Parameters:
  `id` - the id of the component
  `expectedStatus` - list of accepted statuses of the response
Returns:
  the component info
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus

### waitComponentRegistered

public void waitComponentRegistered(String componentName, long timeout, long delay) throws TimeoutException, InterruptedException
Wait until the component with the given name is registered. This means the component must be either in state "Registered" or "Active". The state registered is called "satisfied" in the Felix DS Web Console

Parameters:
  `componentName` - the component's name
  `timeout` - how long to wait for the component to become registered before throwing a `TimeoutException` in milliseconds
  `delay` - time to wait between checks of the state in milliseconds
Throws:
  `TimeoutException` - if the component did not become registered before timeout was reached
  `InterruptedException` - if interrupted
See Also:
  - "OSGi Comp. R6, §112.5 Component Life Cycle"

### waitServiceRegistered

public void waitServiceRegistered(String type, String bundleSymbolicName, long timeout, long delay) throws TimeoutException, InterruptedException
Wait until the service with the given name is registered. This means the component must be either in state "Registered" or "Active".

Parameters:
  `type` - the type of the service (usually the name of a Java interface)
  `bundleSymbolicName` - the symbolic name of the bundle supposed to register that service. May be `null` in which case this method just waits for any service with the requested type being registered (independent of the registering bundle).
  `timeout` - how long to wait for the component to become registered before throwing a `TimeoutException` in milliseconds
  `delay` - time to wait between checks of the state in milliseconds
Throws:
  `TimeoutException` - if the component did not become registered before timeout was reached
  `InterruptedException` - if interrupted

### getConfiguration

public Map<String,Object> getConfiguration(String pid, int... expectedStatus) throws ClientException
Returns a map of all properties set for the config referenced by the PID, where the map keys are the property names. If properties are not set in the corresponding OSGi configuration but metatype information is available, the defaults from the metatype will be included.

Parameters:
  `pid` - the pid of the configuration
  `expectedStatus` - list of accepted statuses of the response
Returns:
  the properties as a map
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus

### getOSGiConfiguration

public Map<String,Object> getOSGiConfiguration(String pid, int... expectedStatus) throws ClientException
Returns a map of all properties set for the config referenced by the PID, where the map keys are the property names.

Parameters:
  `pid` - the pid of the configuration
  `expectedStatus` - list of accepted statuses of the response
Returns:
  the properties as a map or `null` if the configuration does not exist
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus
Since:
  2.1.0

### getConfigurationWithWait

@Deprecated public Map<String,Object> getConfigurationWithWait(long waitCount, String pid, int... expectedStatus) throws TestingValidationException, InterruptedException
Deprecated.
use `waitGetConfiguration(long, String, int...)`
Returns a map of all properties set for the config referenced by the PID, where the map keys are the property names. The method waits until the configuration has been set.

Parameters:
  `waitCount` - The number of maximum wait intervals of 500ms. Between each wait interval, the method polls the backend to see if the configuration ahs been set.
  `pid` - pid
  `expectedStatus` - expected response status
Returns:
  the config properties
Throws:
  `TestingValidationException` - if the response status does not match any of the expectedStatus
  `InterruptedException` - to mark this operation as "waiting"

### waitGetConfiguration

public Map<String,Object> waitGetConfiguration(long timeout, String pid, int... expectedStatus) throws ClientException, InterruptedException, TimeoutException
Returns a map of all properties set for the config referenced by the PID, where the map keys are the property names. The method waits until the configuration has been set.

Parameters:
  `timeout` - Maximum time to wait for the configuration to be available, in ms.
  `pid` - service pid
  `expectedStatus` - expected response status
Returns:
  the config properties
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus
  `InterruptedException` - to mark this operation as "waiting"
  `TimeoutException` - if the timeout was reached

### editConfiguration

public String editConfiguration(String PID, String factoryPID, Map<String,Object> configProperties, int... expectedStatus) throws ClientException
Sets properties of a config referenced by its PID. the properties to be edited are passed as a map of property name,value pairs.

Parameters:
  `PID` - Persistent identity string
  `factoryPID` - Factory persistent identity string or `null`
  `configProperties` - map of properties
  `expectedStatus` - expected response status
Returns:
  the location of the config
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus

### editConfigurationWithWait

@Deprecated public String editConfigurationWithWait(int waitCount, String PID, String factoryPID, Map<String,Object> configProperties, int... expectedStatus) throws ClientException, InterruptedException
Deprecated.
use `waitEditConfiguration(long, String, String, Map, int...)`
Sets properties of a config referenced by its PID. the properties to be edited are passed as a map of property (name,value) pairs. The method waits until the configuration has been set.

Parameters:
  `waitCount` - The number of maximum wait intervals of 500ms. Between each wait interval, the method polls the backend to see if the configuration ahs been set.
  `PID` - Persistent identity string
  `factoryPID` - Factory persistent identity string or `null`
  `configProperties` - map of properties
  `expectedStatus` - expected response status
Returns:
  the pid
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus
  `InterruptedException` - to mark this operation as "waiting"

### waitEditConfiguration

public String waitEditConfiguration(long timeout, String PID, String factoryPID, Map<String,Object> configProperties, int... expectedStatus) throws ClientException, InterruptedException, TimeoutException
Sets properties of a config referenced by its PID. the properties to be edited are passed as a map of property (name,value) pairs. The method waits until the configuration has been set.

Parameters:
  `timeout` - Max time to wait for the configuration to be set, in ms
  `PID` - Persistent identity string
  `factoryPID` - Factory persistent identity string or `null`
  `configProperties` - map of properties
  `expectedStatus` - expected response status
Returns:
  the pid
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus
  `InterruptedException` - to mark this operation as "waiting"
  `TimeoutException` - if the timeout was reached

### deleteConfiguration

public SlingHttpResponse deleteConfiguration(String pid, int... expectedStatus) throws ClientException
Delete the config referenced by the PID

Parameters:
  `pid` - pid
  `expectedStatus` - expected response status
Returns:
  the sling response
Throws:
  `ClientException` - if the response status does not match any of the expectedStatus

### uninstallBundle

public SlingHttpResponse uninstallBundle(String symbolicName) throws ClientException
Uninstall a bundle

Parameters:
  `symbolicName` - bundle symbolic name
Returns:
  the sling response
Throws:
  `ClientException` - if something went wrong with the request

### installBundle

public SlingHttpResponse installBundle(File f, boolean startBundle) throws ClientException
Install a bundle using the Felix webconsole HTTP interface

Parameters:
  `f` - the bundle file
  `startBundle` - whether to start the bundle or not
Returns:
  the sling response
Throws:
  `ClientException` - if the request failed

### installBundle

public SlingHttpResponse installBundle(File f, boolean startBundle, int startLevel) throws ClientException
Install a bundle using the Felix webconsole HTTP interface, with a specific start level

Parameters:
  `f` - bundle file
  `startBundle` - whether to start or just install the bundle
  `startLevel` - start level
Returns:
  the sling response
Throws:
  `ClientException` - if the request failed

### checkBundleInstalled

@Deprecated public boolean checkBundleInstalled(String symbolicName, int waitTime, int retries) throws InterruptedException
Deprecated.
does not respect polling practices; use `waitBundleInstalled(String, long, long)` instead
Check that specified bundle is installed and retries every {{waitTime}} milliseconds, until the bundle is installed or the number of retries was reached

Parameters:
  `symbolicName` - the name of the bundle
  `waitTime` - How many milliseconds to wait between retries
  `retries` - the number of retries
Returns:
  true if the bundle was installed until the retries stop, false otherwise
Throws:
  `InterruptedException` - if interrupted

### installBundleWithRetry

@Deprecated public boolean installBundleWithRetry(File f, boolean startBundle, int startLevel, int waitTime, int retries) throws ClientException, InterruptedException
Deprecated.
`waitInstallBundle(File, boolean, int, long, long)`
Install a bundle using the Felix webconsole HTTP interface and wait for it to be installed

Parameters:
  `f` - the bundle file
  `startBundle` - whether to start the bundle or not
  `startLevel` - the start level of the bundle. negative values mean default start level
  `waitTime` - how long to wait between retries of checking the bundle
  `retries` - how many times to check for the bundle to be installed, until giving up
Returns:
  true if the bundle was successfully installed, false otherwise
Throws:
  `ClientException` - if the request failed
  `InterruptedException` - if interrupted

### waitInstallBundle

public void waitInstallBundle(File f, boolean startBundle, int startLevel, long timeout, long delay) throws ClientException, InterruptedException, TimeoutException
Install a bundle using the Felix webconsole HTTP interface and wait for it to be installed.

Parameters:
  `f` - the bundle file
  `startBundle` - whether to start the bundle or not
  `startLevel` - the start level of the bundle. negative values mean default start level
  `timeout` - how long to wait for the bundle to be installed before throwing a `TimeoutException` in milliseconds
  `delay` - time to wait between checks of the state in milliseconds
Throws:
  `ClientException` - if the request failed
  `TimeoutException` - if the bundle did not install before timeout was reached
  `InterruptedException` - if interrupted

### waitBundleInstalled

public void waitBundleInstalled(String symbolicName, long timeout, long delay) throws TimeoutException, InterruptedException
Wait until the bundle is installed.

Parameters:
  `symbolicName` - symbolic name of bundle
  `timeout` - how long to wait for the bundle to be installed before throwing a `TimeoutException` in milliseconds
  `delay` - time to wait between checks of the state in milliseconds
Throws:
  `TimeoutException` - if the bundle did not install before timeout was reached
  `InterruptedException` - if interrupted
See Also:
  - "OSGi Core R6, §4.4.2 Bundle State"

### waitBundleStarted

public void waitBundleStarted(String symbolicName, long timeout, long delay) throws TimeoutException, InterruptedException
Wait until the bundle is started

Parameters:
  `symbolicName` - symbolic name of bundle
  `timeout` - how long to wait for the bundle to be installed before throwing a `TimeoutException` in milliseconds.
  `delay` - time to wait between checks of the state in milliseconds.
Throws:
  `TimeoutException` - if the bundle did not install before timeout was reached
  `InterruptedException` - if interrupted
See Also:
  - "OSGi Core R6, §4.4.2 Bundle State"

### getBundleId

public long getBundleId(String symbolicName) throws ClientException
Get the id of the bundle

Parameters:
  `symbolicName` - bundle symbolic name
Returns:
  the id
Throws:
  `ClientException` - if the id cannot be retrieved

### getBundleVersion

public String getBundleVersion(String symbolicName) throws ClientException
Get the version of the bundle

Parameters:
  `symbolicName` - bundle symbolic name
Returns:
  bundle version
Throws:
  `ClientException` - if the version is not retrieved

### getBundleState

public String getBundleState(String symbolicName) throws ClientException
Get the state of the bundle

Parameters:
  `symbolicName` - bundle symbolic name
Returns:
  the state of the bundle
Throws:
  `ClientException` - if the state cannot be retrieved

### startBundle

public void startBundle(String symbolicName) throws ClientException
Starts a bundle

Parameters:
  `symbolicName` - the name of the bundle
Throws:
  `ClientException` - if the request failed

### stopBundle

public void stopBundle(String symbolicName) throws ClientException
Stop a bundle

Parameters:
  `symbolicName` - the name of the bundle
Throws:
  `ClientException` - if the request failed

### startBundlewithWait

@Deprecated public void startBundlewithWait(String symbolicName, int waitTime, int retries) throws ClientException, InterruptedException
Deprecated.
use `waitStartBundle(String, long, long)`
Starts a bundle and waits for it to be started

Parameters:
  `symbolicName` - the name of the bundle
  `waitTime` - How many milliseconds to wait between retries
  `retries` - the number of retries
Throws:
  `ClientException` - if the request failed
  `InterruptedException` - if interrupted

### waitStartBundle

public void waitStartBundle(String symbolicName, long timeout, long delay) throws ClientException, InterruptedException, TimeoutException
Starts a bundle and waits for it to be started

Parameters:
  `symbolicName` - the name of the bundle
  `timeout` - max time to wait for the bundle to start, in ms
  `delay` - time to wait between status checks, in ms
Throws:
  `ClientException` - if the request failed
  `InterruptedException` - if interrupted
  `TimeoutException` - if starting timed out

### refreshPackages

public void refreshPackages() throws ClientException
Calls PackageAdmin.refreshPackages to force re-wiring of all the bundles.

Throws:
  `ClientException` - if the request failed

### getConfigPIDFromServices

public String getConfigPIDFromServices(String serviceType, String propertyName, String propertyValue, long timeout, long delay) throws ClientException, InterruptedException, TimeoutException
Performs a search of a config PID through the service information.

This is useful for the case where:

- We don't have the exact config PID (common if it is a factory config we didn't create).
- We have to search the config with a property having a defined value

Parameters:
  `serviceType` - The type of service.
  `propertyName` - The name of the property with the unique value to search.
  `propertyValue` - The unique value to be searched.
Returns:
  The final config PID. Null if it is not found.*
Throws:
  `ClientException`
  `InterruptedException`
  `TimeoutException`

### getBundleSymbolicName

public static String getBundleSymbolicName(File bundleFile) throws IOException
Get the symbolic name from a bundle file by looking at the manifest

Parameters:
  `bundleFile` - bundle file
Returns:
  the name extracted from the manifest
Throws:
  `IOException` - if reading the jar failed

### getBundleVersionFromFile

public static String getBundleVersionFromFile(File bundleFile) throws IOException
Get the version form a bundle file by looking at the manifest

Parameters:
  `bundleFile` - bundle file
Returns:
  the version
Throws:
  `IOException` - if reading the bundle jar failed