/*
* Copyright 2010 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.QueryStringSigner;
import com.amazonaws.handlers.HandlerChainFactory;
import com.amazonaws.handlers.RequestHandler;
import com.amazonaws.http.DefaultResponseHandler;
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.VoidUnmarshaller;
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.
*
*/
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>();
/**
* 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");
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, "//ListMetricsResult", new ListMetricsResultUnmarshaller());
}
/**
*
* @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, "//GetMetricStatisticsResult", new GetMetricStatisticsResultUnmarshaller());
}
/**
*
* @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());
}
private <X, Y extends AmazonWebServiceRequest> X invoke(Request<Y> request, String responseElement, Unmarshaller<X, Node> 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);
}
}
QueryStringSigner<Y> signer = new QueryStringSigner<Y>(awsCredentials);
try {
signer.sign(request);
} catch (SignatureException e) {
throw new AmazonServiceException("Unable to sign request", e);
}
HttpRequest httpRequest = new HttpRequest(HttpMethodName.POST);
for (Entry<String, String> parameter : request.getParameters().entrySet()) {
httpRequest.addParameter(parameter.getKey(), parameter.getValue());
}
httpRequest.setServiceName(request.getServiceName());
httpRequest.setEndpoint(request.getEndpoint());
httpRequest.setResourcePath(request.getResourcePath());
DefaultResponseHandler<X> responseHandler = new DefaultResponseHandler<X>(unmarshaller, responseElement);
DefaultErrorResponseHandler errorResponseHandler = new DefaultErrorResponseHandler(exceptionUnmarshallers);
return (X)client.execute(httpRequest, responseHandler, errorResponseHandler);
}
}