/*
* Copyright (c) 2014 Jacob D. Parr
*
* This software may not be used without permission.
*/
package com.cosmicpush.app.resources.api;
import com.amazonaws.auth.*;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
import com.amazonaws.services.simpleemail.model.*;
import com.cosmicpush.app.domain.accounts.Account;
import com.cosmicpush.app.domain.clients.ApiClient;
import com.cosmicpush.app.domain.clients.config.AwsEmailConfig;
import com.cosmicpush.app.domain.requests.*;
import com.cosmicpush.app.system.CpServerObjectMapper;
import com.cosmicpush.pub.common.RequestStatus;
import com.cosmicpush.pub.push.EmailToSmsPush;
import org.crazyyak.dev.common.StringUtils;
import org.crazyyak.dev.common.exceptions.ExceptionUtils;
public class AwsSmsDelegate extends AbstractDelegate {
private final Account account;
private final ApiClient apiClient;
private final EmailToSmsPush action;
public AwsSmsDelegate(CpServerObjectMapper objectMapper, ApiRequestStore apiRequestStore, Account account, ApiClient apiClient, ApiRequest apiRequest, EmailToSmsPush action) {
super(objectMapper, apiRequest, apiRequestStore);
this.action = ExceptionUtils.assertNotNull(action, "action");
this.account = ExceptionUtils.assertNotNull(account, "account");
this.apiClient = ExceptionUtils.assertNotNull(apiClient, "apiClient");
}
@Override
public synchronized RequestStatus processRequest() throws Exception {
String reasonNotPermitted = account.getReasonNotPermitted(action);
if (StringUtils.isNotBlank(reasonNotPermitted)) {
return apiRequest.denyRequest(reasonNotPermitted);
}
String apiMessage = null;
AwsEmailConfig config = apiClient.getAwsEmailConfig();
SendEmailRequest sendEmailRequest = new SendEmailRequest();
sendEmailRequest.withSource(action.getFromAddress());
sendEmailRequest.withReturnPath(action.getFromAddress());
sendEmailRequest.withReplyToAddresses(action.getFromAddress());
if (StringUtils.isNotBlank(config.getRecipientOverride())) {
// This is NOT a "production" request.
sendEmailRequest.setDestination(new Destination().withToAddresses(config.getRecipientOverride()));
apiMessage = String.format("Request sent to recipient override, %s.", config.getRecipientOverride());
} else {
// This IS a "production" request.
sendEmailRequest.setDestination(new Destination().withToAddresses(action.getToAddress()));
}
Content subject = new Content().withCharset("UTF-8").withData("");
Body body = new Body();
body.withText(new Content().withCharset("UTF-8").withData(action.getMessage()));
sendEmailRequest.setMessage(new Message(subject, body));
AWSCredentials awsCredentials = new BasicAWSCredentials(config.getAccessKeyId(), config.getSecretKey());
new AmazonSimpleEmailServiceClient(awsCredentials).sendEmail(sendEmailRequest);
return apiRequest.processed(apiMessage);
}
}