Package com.cosmicpush.app.resources.api

Source Code of com.cosmicpush.app.resources.api.AwsEmailDelegate

/*
* 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.EmailPush;
import org.crazyyak.dev.common.StringUtils;
import org.crazyyak.dev.common.exceptions.ExceptionUtils;

public class AwsEmailDelegate extends AbstractDelegate {

  private final Account account;
  private final ApiClient apiClient;

  private final EmailPush action;

  public AwsEmailDelegate(CpServerObjectMapper objectMapper, ApiRequestStore apiRequestStore, Account account, ApiClient apiClient, ApiRequest apiRequest, EmailPush 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();

    Body body = new Body();
    if (StringUtils.isBlank(action.getHtmlContent())) {
      body.withText(new Content().withCharset("UTF-8").withData("-no message-"));
    } else {
      body.withHtml(new Content().withCharset("UTF-8").withData(action.getHtmlContent()));
    }

    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(action.getEmailSubject());
    sendEmailRequest.setMessage(new Message(subject, body));

    AWSCredentials awsCredentials = new BasicAWSCredentials(config.getAccessKeyId(), config.getSecretKey());
    new AmazonSimpleEmailServiceClient(awsCredentials).sendEmail(sendEmailRequest);

    return apiRequest.processed(apiMessage);
  }
}
TOP

Related Classes of com.cosmicpush.app.resources.api.AwsEmailDelegate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.