/*
* Copyright 2010-2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.cloudwatch;
import org.w3c.dom.Node;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.security.SignatureException;
import com.amazonaws.*;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWS3Signer;
import com.amazonaws.auth.QueryStringSigner;
import com.amazonaws.handlers.HandlerChainFactory;
import com.amazonaws.handlers.RequestHandler;
import com.amazonaws.http.StaxResponseHandler;
import com.amazonaws.http.DefaultErrorResponseHandler;
import com.amazonaws.http.HttpClient;
import com.amazonaws.http.HttpMethodName;
import com.amazonaws.http.HttpRequest;
import com.amazonaws.transform.Unmarshaller;
import com.amazonaws.transform.StaxUnmarshallerContext;
import com.amazonaws.transform.StandardErrorUnmarshaller;
import com.amazonaws.services.cloudwatch.model.*;
import com.amazonaws.services.cloudwatch.model.transform.*;
/**
* Client for accessing AmazonCloudWatch. All service calls made
* using this client are blocking, and will not return until the service call
* completes.
* <p>
*
*/
public class AmazonCloudWatchClient extends AmazonWebServiceClient implements AmazonCloudWatch {
/**
* The AWS credentials (access key ID and secret key) to use when
* authenticating with AWS services.
*/
private AWSCredentials awsCredentials;
/**
* List of exception unmarshallers for all AmazonCloudWatch exceptions.
*/
protected final List<Unmarshaller<AmazonServiceException, Node>> exceptionUnmarshallers;
/** Low level client for sending requests to AWS services. */
protected final HttpClient client;
/** Optional request handlers for additional request processing. */
private List<RequestHandler> requestHandlers = new ArrayList<RequestHandler>();
/** AWS signer for authenticating requests. */
private QueryStringSigner signer;
/**
* Constructs a new client to invoke service methods on
* AmazonCloudWatch using the specified AWS account credentials.
*
* <p>
* All service calls made using this new client object are blocking, and will not
* return until the service call completes.
*
* @param awsCredentials The AWS credentials (access key ID and secret key) to use
* when authenticating with AWS services.
*/
public AmazonCloudWatchClient(AWSCredentials awsCredentials) {
this(awsCredentials, new ClientConfiguration());
}
/**
* Constructs a new client to invoke service methods on
* AmazonCloudWatch using the specified AWS account credentials
* and client configuration options.
*
* <p>
* All service calls made using this new client object are blocking, and will not
* return until the service call completes.
*
* @param awsCredentials The AWS credentials (access key ID and secret key) to use
* when authenticating with AWS services.
* @param clientConfiguration The client configuration options controlling how this
* client connects to AmazonCloudWatch
* (ex: proxy settings, retry counts, etc.).
*/
public AmazonCloudWatchClient(AWSCredentials awsCredentials, ClientConfiguration clientConfiguration) {
super(clientConfiguration);
this.awsCredentials = awsCredentials;
exceptionUnmarshallers = new ArrayList<Unmarshaller<AmazonServiceException, Node>>();
exceptionUnmarshallers.add(new InternalServiceExceptionUnmarshaller());
exceptionUnmarshallers.add(new InvalidParameterCombinationExceptionUnmarshaller());
exceptionUnmarshallers.add(new MissingRequiredParameterExceptionUnmarshaller());
exceptionUnmarshallers.add(new InvalidParameterValueExceptionUnmarshaller());
exceptionUnmarshallers.add(new StandardErrorUnmarshaller());
setEndpoint("monitoring.amazonaws.com");
signer = new QueryStringSigner(awsCredentials);
requestHandlers = new HandlerChainFactory().newRequestHandlerChain(
"/com/amazonaws/services/cloudwatch/request.handlers");
client = new HttpClient(clientConfiguration);
}
/**
*
* @param listMetricsRequest Container for the necessary parameters to
* execute the ListMetrics service method on AmazonCloudWatch.
*
* @return The response from the ListMetrics service method, as returned
* by AmazonCloudWatch.
*
* @throws InvalidParameterValueException
* @throws InternalServiceException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonCloudWatch indicating
* either a problem with the data in the request, or a server side issue.
*/
public ListMetricsResult listMetrics(ListMetricsRequest listMetricsRequest)
throws AmazonServiceException, AmazonClientException {
Request<ListMetricsRequest> request = new ListMetricsRequestMarshaller().marshall(listMetricsRequest);
return invoke(request, new ListMetricsResultStaxUnmarshaller());
}
/**
*
* @param getMetricStatisticsRequest Container for the necessary
* parameters to execute the GetMetricStatistics service method on
* AmazonCloudWatch.
*
* @return The response from the GetMetricStatistics service method, as
* returned by AmazonCloudWatch.
*
* @throws InvalidParameterValueException
* @throws InternalServiceException
* @throws InvalidParameterCombinationException
* @throws MissingRequiredParameterException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonCloudWatch indicating
* either a problem with the data in the request, or a server side issue.
*/
public GetMetricStatisticsResult getMetricStatistics(GetMetricStatisticsRequest getMetricStatisticsRequest)
throws AmazonServiceException, AmazonClientException {
Request<GetMetricStatisticsRequest> request = new GetMetricStatisticsRequestMarshaller().marshall(getMetricStatisticsRequest);
return invoke(request, new GetMetricStatisticsResultStaxUnmarshaller());
}
/**
*
* @return The response from the ListMetrics service method, as returned
* by AmazonCloudWatch.
*
* @throws InvalidParameterValueException
* @throws InternalServiceException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonCloudWatch indicating
* either a problem with the data in the request, or a server side issue.
*/
public ListMetricsResult listMetrics() throws AmazonServiceException, AmazonClientException {
return listMetrics(new ListMetricsRequest());
}
/**
* Returns additional metadata for a previously executed successful, request, typically used for
* debugging issues where a service isn't acting as expected. This data isn't considered part
* of the result data returned by an operation, so it's available through this separate,
* diagnostic interface.
* <p>
* Response metadata is only cached for a limited period of time, so if you need to access
* this extra diagnostic information for an executed request, you should use this method
* to retrieve it as soon as possible after executing the request.
*
* @param request
* The originally executed request
*
* @return The response metadata for the specified request, or null if none
* is available.
*/
public ResponseMetadata getCachedResponseMetadata(AmazonWebServiceRequest request) {
return client.getResponseMetadataForRequest(request);
}
private <X, Y extends AmazonWebServiceRequest> X invoke(Request<Y> request, Unmarshaller<X, StaxUnmarshallerContext> unmarshaller) {
request.setEndpoint(endpoint);
for (Entry<String, String> entry : request.getOriginalRequest().copyPrivateRequestParameters().entrySet()) {
request.addParameter(entry.getKey(), entry.getValue());
}
// Apply any additional service specific request handlers that need to be run
if (requestHandlers != null) {
for (RequestHandler requestHandler : requestHandlers) {
request = requestHandler.handleRequest(request);
}
}
try {
signer.sign(request);
} catch (SignatureException e) {
throw new AmazonServiceException("Unable to sign request", e);
}
HttpRequest httpRequest = convertToHttpRequest(request, HttpMethodName.POST);
StaxResponseHandler<X> responseHandler = new StaxResponseHandler<X>(unmarshaller);
DefaultErrorResponseHandler errorResponseHandler = new DefaultErrorResponseHandler(exceptionUnmarshallers);
return (X)client.execute(httpRequest, responseHandler, errorResponseHandler);
}
}