/*
* 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.simpleemail;
import org.w3c.dom.Node;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
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.ExecutionContext;
import com.amazonaws.transform.Unmarshaller;
import com.amazonaws.transform.StaxUnmarshallerContext;
import com.amazonaws.transform.StandardErrorUnmarshaller;
import com.amazonaws.services.simpleemail.model.*;
import com.amazonaws.services.simpleemail.model.transform.*;
/**
* Client for accessing AmazonSimpleEmailService. All service calls made
* using this client are blocking, and will not return until the service call
* completes.
* <p>
* Amazon Simple Email Service <p>
* This is the API Reference for Amazon Simple Email Service (Amazon
* SES). This documentation is intended to be used in conjunction with
* the Amazon SES Getting Started Guide and the Amazon SES Developer
* Guide.
* </p>
* <p>
* For specific details on how to construct a service request, please
* consult the <a
* href="http://docs.amazonwebservices.com/ses/latest/DeveloperGuide">
* Amazon SES Developer Guide </a> .
* </p>
*/
public class AmazonSimpleEmailServiceClient extends AmazonWebServiceClient implements AmazonSimpleEmailService {
/**
* 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 AmazonSimpleEmailService exceptions.
*/
protected final List<Unmarshaller<AmazonServiceException, Node>> exceptionUnmarshallers;
/** Optional request handlers for additional request processing. */
private final List<RequestHandler> requestHandlers;
/** AWS signer for authenticating requests. */
private AWS3Signer signer;
/**
* Constructs a new client to invoke service methods on
* AmazonSimpleEmailService 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 AmazonSimpleEmailServiceClient(AWSCredentials awsCredentials) {
this(awsCredentials, new ClientConfiguration());
}
/**
* Constructs a new client to invoke service methods on
* AmazonSimpleEmailService 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 AmazonSimpleEmailService
* (ex: proxy settings, retry counts, etc.).
*/
public AmazonSimpleEmailServiceClient(AWSCredentials awsCredentials, ClientConfiguration clientConfiguration) {
super(clientConfiguration);
this.awsCredentials = awsCredentials;
exceptionUnmarshallers = new ArrayList<Unmarshaller<AmazonServiceException, Node>>();
exceptionUnmarshallers.add(new MessageRejectedExceptionUnmarshaller());
exceptionUnmarshallers.add(new StandardErrorUnmarshaller());
setEndpoint("email.us-east-1.amazonaws.com");
signer = new AWS3Signer();
HandlerChainFactory chainFactory = new HandlerChainFactory();
requestHandlers = Collections.synchronizedList(chainFactory.newRequestHandlerChain(
"/com/amazonaws/services/simpleemail/request.handlers"));
}
/**
* Appends a request handler to the list of registered handlers that are run
* as part of a request's lifecycle.
*
* @param requestHandler
* The new handler to add to the current list of request
* handlers.
*/
public void addRequestHandler(RequestHandler requestHandler) {
requestHandlers.add(requestHandler);
}
/**
* <p>
* Returns the user's current activity limits.
* </p>
*
* @param getSendQuotaRequest Container for the necessary parameters to
* execute the GetSendQuota service method on AmazonSimpleEmailService.
*
* @return The response from the GetSendQuota service method, as returned
* by AmazonSimpleEmailService.
*
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public GetSendQuotaResult getSendQuota(GetSendQuotaRequest getSendQuotaRequest)
throws AmazonServiceException, AmazonClientException {
Request<GetSendQuotaRequest> request = new GetSendQuotaRequestMarshaller().marshall(getSendQuotaRequest);
return invoke(request, new GetSendQuotaResultStaxUnmarshaller());
}
/**
* <p>
* Returns a list containing all of the email addresses that have been
* verified.
* </p>
*
* @param listVerifiedEmailAddressesRequest Container for the necessary
* parameters to execute the ListVerifiedEmailAddresses service method on
* AmazonSimpleEmailService.
*
* @return The response from the ListVerifiedEmailAddresses service
* method, as returned by AmazonSimpleEmailService.
*
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public ListVerifiedEmailAddressesResult listVerifiedEmailAddresses(ListVerifiedEmailAddressesRequest listVerifiedEmailAddressesRequest)
throws AmazonServiceException, AmazonClientException {
Request<ListVerifiedEmailAddressesRequest> request = new ListVerifiedEmailAddressesRequestMarshaller().marshall(listVerifiedEmailAddressesRequest);
return invoke(request, new ListVerifiedEmailAddressesResultStaxUnmarshaller());
}
/**
* <p>
* Returns the user's sending statistics. The result is a list of data
* points, representing the last two weeks of sending activity.
* </p>
* <p>
* Each data point in the list contains statistics for a 15-minute
* interval.
* </p>
*
* @param getSendStatisticsRequest Container for the necessary parameters
* to execute the GetSendStatistics service method on
* AmazonSimpleEmailService.
*
* @return The response from the GetSendStatistics service method, as
* returned by AmazonSimpleEmailService.
*
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public GetSendStatisticsResult getSendStatistics(GetSendStatisticsRequest getSendStatisticsRequest)
throws AmazonServiceException, AmazonClientException {
Request<GetSendStatisticsRequest> request = new GetSendStatisticsRequestMarshaller().marshall(getSendStatisticsRequest);
return invoke(request, new GetSendStatisticsResultStaxUnmarshaller());
}
/**
* <p>
* Composes an email message based on input data, and then immediately
* queues the message for sending.
* </p>
* <p>
* <b>IMPORTANT:</b>If you have not yet requested production access to
* Amazon SES, then you will only be able to send email to and from
* verified email addresses. For more information, go to the Amazon SES
* Developer Guide.
* </p>
*
* @param sendEmailRequest Container for the necessary parameters to
* execute the SendEmail service method on AmazonSimpleEmailService.
*
* @return The response from the SendEmail service method, as returned by
* AmazonSimpleEmailService.
*
* @throws MessageRejectedException
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public SendEmailResult sendEmail(SendEmailRequest sendEmailRequest)
throws AmazonServiceException, AmazonClientException {
Request<SendEmailRequest> request = new SendEmailRequestMarshaller().marshall(sendEmailRequest);
return invoke(request, new SendEmailResultStaxUnmarshaller());
}
/**
* <p>
* Deletes the specified email address from the list of verified
* addresses.
* </p>
*
* @param deleteVerifiedEmailAddressRequest Container for the necessary
* parameters to execute the DeleteVerifiedEmailAddress service method on
* AmazonSimpleEmailService.
*
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public void deleteVerifiedEmailAddress(DeleteVerifiedEmailAddressRequest deleteVerifiedEmailAddressRequest)
throws AmazonServiceException, AmazonClientException {
Request<DeleteVerifiedEmailAddressRequest> request = new DeleteVerifiedEmailAddressRequestMarshaller().marshall(deleteVerifiedEmailAddressRequest);
invoke(request, null);
}
/**
* <p>
* Verifies an email address. This action causes a confirmation email
* message to be sent to the specified address.
* </p>
*
* @param verifyEmailAddressRequest Container for the necessary
* parameters to execute the VerifyEmailAddress service method on
* AmazonSimpleEmailService.
*
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public void verifyEmailAddress(VerifyEmailAddressRequest verifyEmailAddressRequest)
throws AmazonServiceException, AmazonClientException {
Request<VerifyEmailAddressRequest> request = new VerifyEmailAddressRequestMarshaller().marshall(verifyEmailAddressRequest);
invoke(request, null);
}
/**
* <p>
* Sends an email message, with header and content specified by the
* client. The <code>SendRawEmail</code> action is useful for sending
* multipart MIME emails, with attachments or inline content. The raw
* text of the message must comply with Internet email standards;
* otherwise, the message cannot be sent.
* </p>
* <p>
* <b>IMPORTANT:</b>If you have not yet requested production access to
* Amazon SES, then you will only be able to send email to and from
* verified email addresses. For more information, go to the Amazon SES
* Developer Guide.
* </p>
*
* @param sendRawEmailRequest Container for the necessary parameters to
* execute the SendRawEmail service method on AmazonSimpleEmailService.
*
* @return The response from the SendRawEmail service method, as returned
* by AmazonSimpleEmailService.
*
* @throws MessageRejectedException
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public SendRawEmailResult sendRawEmail(SendRawEmailRequest sendRawEmailRequest)
throws AmazonServiceException, AmazonClientException {
Request<SendRawEmailRequest> request = new SendRawEmailRequestMarshaller().marshall(sendRawEmailRequest);
return invoke(request, new SendRawEmailResultStaxUnmarshaller());
}
/**
* <p>
* Returns the user's current activity limits.
* </p>
*
* @return The response from the GetSendQuota service method, as returned
* by AmazonSimpleEmailService.
*
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public GetSendQuotaResult getSendQuota() throws AmazonServiceException, AmazonClientException {
return getSendQuota(new GetSendQuotaRequest());
}
/**
* <p>
* Returns a list containing all of the email addresses that have been
* verified.
* </p>
*
* @return The response from the ListVerifiedEmailAddresses service
* method, as returned by AmazonSimpleEmailService.
*
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public ListVerifiedEmailAddressesResult listVerifiedEmailAddresses() throws AmazonServiceException, AmazonClientException {
return listVerifiedEmailAddresses(new ListVerifiedEmailAddressesRequest());
}
/**
* <p>
* Returns the user's sending statistics. The result is a list of data
* points, representing the last two weeks of sending activity.
* </p>
* <p>
* Each data point in the list contains statistics for a 15-minute
* interval.
* </p>
*
* @return The response from the GetSendStatistics service method, as
* returned by AmazonSimpleEmailService.
*
*
* @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 AmazonSimpleEmailService indicating
* either a problem with the data in the request, or a server side issue.
*/
public GetSendStatisticsResult getSendStatistics() throws AmazonServiceException, AmazonClientException {
return getSendStatistics(new GetSendStatisticsRequest());
}
/**
* 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) {
requestHandler.beforeRequest(request);
}
}
if (request.getOriginalRequest().getRequestCredentials() != null) {
signer.sign(request, request.getOriginalRequest().getRequestCredentials());
} else {
signer.sign(request, awsCredentials);
}
StaxResponseHandler<X> responseHandler = new StaxResponseHandler<X>(unmarshaller);
DefaultErrorResponseHandler errorResponseHandler = new DefaultErrorResponseHandler(exceptionUnmarshallers);
ExecutionContext executionContext = new ExecutionContext(requestHandlers);
return (X)client.execute(request, responseHandler, errorResponseHandler, executionContext);
}
}