public class ApacheCloudStackClient
extends java.lang.Object
implements java.io.Serializable
apacheCloudStackUser).| Modifier and Type | Field and Description |
|---|---|
protected boolean |
acceptAllKindsOfCertificates
This flag is highly dangerous and should never be used with production environments.
|
protected ApacheCloudStackUser |
apacheCloudStackUser
User credentials that can be used to access ApacheCloudStack.
|
protected boolean |
validateServerHttpsCertificate
This flag indicates if we are going to validate the server certificate in case of HTTPS connections.
|
| Constructor and Description |
|---|
ApacheCloudStackClient(java.lang.String url,
ApacheCloudStackUser apacheCloudStackUser) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
configureDomainForCookie(org.apache.http.impl.cookie.BasicClientCookie cookie)
It configures the cookie domain with the domain of the Apache CloudStack that is being accessed.
|
protected void |
configureRequestExpiration(java.util.List<ApacheCloudStackApiCommandParameter> apacheCloudStackRequestList)
This method configures the request expiration if needed.
|
protected void |
createAndAddCookiesOnStoreForHeader(org.apache.http.client.CookieStore cookieStore,
org.apache.http.Header header)
This method creates a cookie for every
HeaderElement of the Header given as parameter. |
protected void |
createAndAddCookiesOnStoreForHeaders(org.apache.http.client.CookieStore cookieStore,
org.apache.http.Header[] allHeaders)
For every header that contains the command 'Set-Cookie' it will call the method
createAndAddCookiesOnStoreForHeader(CookieStore, Header) |
protected java.lang.String |
createApacheCloudStackApiUrlRequest(ApacheCloudStackRequest request,
boolean shouldSignAppendSignature)
This method creates transforms the given
ApacheCloudStackRequest into a URL requeest for the Apache CloudStack API. |
protected java.lang.String |
createCommandString(ApacheCloudStackRequest request)
It creates the command query string, placing the parameters in alphabetical order.
|
protected org.apache.http.impl.cookie.BasicClientCookie |
createCookieForHeaderElement(org.apache.http.HeaderElement element)
This method will create a
BasicClientCookie with the given HeaderElement. |
protected java.lang.String |
createExpirationDate()
This method creates the expiration date as a string according to the ISO 8601.
|
protected org.apache.http.impl.client.CloseableHttpClient |
createHttpClient()
It creates an
CloseableHttpClient object. |
protected org.apache.http.protocol.HttpContext |
createHttpContextWithAuthenticatedSessionUsingUserCredentials(org.apache.http.impl.client.CloseableHttpClient httpClient)
This method creates an
HttpContext with an authenticated JSESSIONID. |
protected org.apache.http.protocol.HttpContext |
createHttpContextWithAuthenticatedSessionUsingUserCredentialsIfNeeded(org.apache.http.impl.client.CloseableHttpClient httpClient,
boolean isSecretKeyApiKeyAuthenticationMechanism)
According to the 'isSecretKeyApiKey AuthenticationMechanism' parameter this method creates an HttpContext that is used when executing requests.
|
protected org.apache.http.protocol.HttpContext |
createHttpContextWithCookies(org.apache.http.client.methods.CloseableHttpResponse loginResponse)
It creates an
HttpContext object with a cookie store that will contain the cookies returned by the user in the CloseableHttpResponse that is received as parameter. |
protected org.apache.http.client.methods.HttpPost |
createHttpPost()
Creates an
HttpPost object to be sent to Apache CloudStack API. |
protected javax.net.ssl.HostnameVerifier |
createInsecureHostNameVerifier() |
protected org.apache.http.conn.ssl.SSLConnectionSocketFactory |
createInsecureSslFactory()
This method creates an insecure SSL factory that will trust on self signed certificates.
|
protected org.apache.http.client.config.RequestConfig |
createRequestConfig()
It creates a
RequestConfig object with configured connectionTimeout value. |
protected java.lang.String |
createSignature(java.lang.String queryString)
Creates a signature (HMAC-sha1) with the
#ApacheCloudStackUser#getSecretKey() and the given queryString
The returner signature is encoded in Base64. |
protected java.util.List<ApacheCloudStackApiCommandParameter> |
createSortedCommandQueryList(ApacheCloudStackRequest request)
This methods adds the final data needed to the command query.
|
java.lang.String |
executeRequest(ApacheCloudStackRequest request)
This method executes the given
ApacheCloudStackRequest. |
<T> T |
executeRequest(ApacheCloudStackRequest request,
java.lang.Class<T> clazz)
It executes the given request and converts the result into an object of the given type.
|
protected java.lang.String |
executeRequestGetResponseAsString(java.lang.String urlRequest,
org.apache.http.impl.client.CloseableHttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext)
Executes the request with the given
HttpContext. |
protected void |
executeUserLogout(org.apache.http.impl.client.CloseableHttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext)
This method executes the user logout when using username/password/domain authentication.
|
protected java.util.Date |
getExpirationDate()
Creates the expiration date, by adding the
requestValidity to the current time. |
protected java.util.List<org.apache.http.NameValuePair> |
getParametersForLogin()
This method creates a list of
NameValuePair and returns the data for login using username and password. |
protected java.lang.String |
getResponseAsString(org.apache.http.client.methods.CloseableHttpResponse response)
It retrieves the response status as a
String |
java.lang.String |
getUrl() |
protected java.lang.String |
getUrlEncodedValue(java.lang.Object paramValue)
This method encodes the parameter value as specified by Apache CloudStack
|
void |
setAcceptAllKindsOfCertificates(boolean acceptAllKindOfCertificates) |
void |
setConnectionTimeout(int connectionTimeout) |
void |
setRequestValidity(int requestValidity) |
void |
setShouldRequestsExpire(boolean shouldRequestsExpire) |
void |
setValidateServerHttpsCertificate(boolean validateServerHttpsCertificate)
This configuration enables or disables the HTTPS certification validation.
|
java.lang.String |
toString() |
protected boolean validateServerHttpsCertificate
protected boolean acceptAllKindsOfCertificates
protected ApacheCloudStackUser apacheCloudStackUser
public ApacheCloudStackClient(java.lang.String url,
ApacheCloudStackUser apacheCloudStackUser)
public java.lang.String executeRequest(ApacheCloudStackRequest request)
ApacheCloudStackRequest.
It will return the response as a plain String.
You should have in mind that if the parameter 'response' is not set, the default is 'XML'.protected java.lang.String executeRequestGetResponseAsString(java.lang.String urlRequest,
org.apache.http.impl.client.CloseableHttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext)
HttpContext.protected void executeUserLogout(org.apache.http.impl.client.CloseableHttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext)
protected org.apache.http.protocol.HttpContext createHttpContextWithAuthenticatedSessionUsingUserCredentialsIfNeeded(org.apache.http.impl.client.CloseableHttpClient httpClient,
boolean isSecretKeyApiKeyAuthenticationMechanism)
BasicHttpContext object. Otherwise, we authenticate the user with his/her username/password/domain and return an HttpContext object that contains the authenticated session Id configured as a cookie.protected org.apache.http.client.config.RequestConfig createRequestConfig()
RequestConfig object with configured connectionTimeout value.
This can be used to set on a HttpClient its connect timeout, connection request timeout and socket timeout.protected org.apache.http.impl.client.CloseableHttpClient createHttpClient()
CloseableHttpClient object.
If validateServerHttpsCertificate indicates that we should not validate HTTPS server certificate, we use an insecure SSL factory; the insecure factory is created using createInsecureSslFactory().protected org.apache.http.protocol.HttpContext createHttpContextWithAuthenticatedSessionUsingUserCredentials(org.apache.http.impl.client.CloseableHttpClient httpClient)
HttpContext with an authenticated JSESSIONID.
The authentication is performed using username, password and domain that are provided by the user.protected org.apache.http.protocol.HttpContext createHttpContextWithCookies(org.apache.http.client.methods.CloseableHttpResponse loginResponse)
HttpContext object with a cookie store that will contain the cookies returned by the user in the CloseableHttpResponse that is received as parameter.protected void createAndAddCookiesOnStoreForHeaders(org.apache.http.client.CookieStore cookieStore,
org.apache.http.Header[] allHeaders)
createAndAddCookiesOnStoreForHeader(CookieStore, Header)protected void createAndAddCookiesOnStoreForHeader(org.apache.http.client.CookieStore cookieStore,
org.apache.http.Header header)
HeaderElement of the Header given as parameter.
Then, it adds this newly created cookie into the CookieStore provided as parameter.protected org.apache.http.impl.cookie.BasicClientCookie createCookieForHeaderElement(org.apache.http.HeaderElement element)
BasicClientCookie with the given HeaderElement.
It sill set the cookie's name and value according to the HeaderElement.getName() and HeaderElement.getValue() methods.
Moreover, it will transport every HeaderElement parameter to the cookie using the BasicClientCookie.setAttribute(String, String).
Additionally, it configures the cookie path (BasicClientCookie.setPath(String)) to value '/client/api' and the cookie domain using configureDomainForCookie(BasicClientCookie) method.protected void configureDomainForCookie(org.apache.http.impl.cookie.BasicClientCookie cookie)
url variable.protected org.apache.http.client.methods.HttpPost createHttpPost()
HttpPost object to be sent to Apache CloudStack API.
The content type configured for this request is 'application/x-www-form-urlencoded'.protected java.util.List<org.apache.http.NameValuePair> getParametersForLogin()
NameValuePair and returns the data for login using username and password.protected org.apache.http.conn.ssl.SSLConnectionSocketFactory createInsecureSslFactory()
TrustSelfSignedStrategy.protected javax.net.ssl.HostnameVerifier createInsecureHostNameVerifier()
protected java.lang.String getResponseAsString(org.apache.http.client.methods.CloseableHttpResponse response)
throws java.io.IOException
Stringjava.io.IOExceptionprotected java.lang.String createApacheCloudStackApiUrlRequest(ApacheCloudStackRequest request, boolean shouldSignAppendSignature)
ApacheCloudStackRequest into a URL requeest for the Apache CloudStack API.
Therefore, it will create a command query string following the CloudStack specifications using method createCommandString(ApacheCloudStackRequest);
and then, if it needs, it creates the signature using the method createSignature(String) and append it to the URL.protected java.lang.String createSignature(java.lang.String queryString)
#ApacheCloudStackUser#getSecretKey() and the given queryString
The returner signature is encoded in Base64.protected java.lang.String createCommandString(ApacheCloudStackRequest request)
createSortedCommandQueryList(ApacheCloudStackRequest) method.protected java.lang.String getUrlEncodedValue(java.lang.Object paramValue)
protected java.util.List<ApacheCloudStackApiCommandParameter> createSortedCommandQueryList(ApacheCloudStackRequest request)
ApacheCloudStackRequest.getCommand() as value.
It also adds a parameter called 'apiKey', with the value of #ApacheCloudStackUser#getApiKey() as value.
Then, it will sort the parameters that are in a list in alphabetical order.protected void configureRequestExpiration(java.util.List<ApacheCloudStackApiCommandParameter> apacheCloudStackRequestList)
requestValidity to determine until when the request is valid.
It also uses the parameter shouldRequestsExpire to decide if it has to configure or not the validity of the request.
Moreover, if the 'apacheCloudStackRequestList' contains the 'expires' it will only add a parameter called 'signatureVersion=3', in order to enable that override.protected java.lang.String createExpirationDate()
protected java.util.Date getExpirationDate()
requestValidity to the current time.public <T> T executeRequest(ApacheCloudStackRequest request, java.lang.Class<T> clazz)
executeRequest(ApacheCloudStackRequest).
To convert the result into an object, it will use Gson.fromJson(String, Class)public void setValidateServerHttpsCertificate(boolean validateServerHttpsCertificate)
public void setRequestValidity(int requestValidity)
public void setShouldRequestsExpire(boolean shouldRequestsExpire)
public void setConnectionTimeout(int connectionTimeout)
public void setAcceptAllKindsOfCertificates(boolean acceptAllKindOfCertificates)
public java.lang.String getUrl()
public java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2017. All Rights Reserved.