Package com.cosmicpush.app.resources.manage.client

Source Code of com.cosmicpush.app.resources.manage.client.ManageAwsEmailApi

/*
* Copyright (c) 2014 Jacob D. Parr
*
* This software may not be used without permission.
*/

package com.cosmicpush.app.resources.manage.client;

import com.cosmicpush.app.domain.accounts.Account;
import com.cosmicpush.app.domain.clients.ApiClient;
import com.cosmicpush.app.domain.clients.actions.CreateAwsEmailConfigAction;
import com.cosmicpush.app.domain.requests.ApiRequest;
import com.cosmicpush.app.resources.api.AwsEmailDelegate;
import com.cosmicpush.app.resources.manage.UserRequestConfig;
import com.cosmicpush.pub.push.EmailPush;
import java.net.*;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import org.crazyyak.dev.common.*;
import org.crazyyak.dev.common.exceptions.ExceptionUtils;

public class ManageAwsEmailApi {

  private final Account account;
  private final ApiClient apiClient;
  private final UserRequestConfig config;

  public ManageAwsEmailApi(UserRequestConfig config, Account account, ApiClient apiClient) {
    this.config = ExceptionUtils.assertNotNull(config, "config");
    this.account = ExceptionUtils.assertNotNull(account, "account");
    this.apiClient = ExceptionUtils.assertNotNull(apiClient, "apiClient");
  }

  public Response redirect() throws Exception {
    String path = String.format("manage/api-client/%s", apiClient.getClientName());
    return Response.seeOther(new URI(path)).build();
  }

  @POST
  public Response updateAwsEmail(@FormParam("accessKeyId") String accessKeyId,
                                 @FormParam("secretKey") String secretKey,
                                 @FormParam("testAddress") String testAddress,
                                 @FormParam("recipientOverride") String recipientOverride) throws Exception {

    CreateAwsEmailConfigAction action = new CreateAwsEmailConfigAction(accessKeyId, secretKey, testAddress, recipientOverride);

    apiClient.apply(action);
    apiClient.deleteSmtpEmailConfig();
    config.getAccountStore().update(account);

    return redirect();
  }

  @POST
  @Path("/delete")
  public Response deleteAwsEmail() throws Exception {

    apiClient.deleteAwsEmailConfig();
    config.getAccountStore().update(account);

    return redirect();
  }

  @POST
  @Path("/test")
  public Response testAwsEmail() throws Exception {
    String recipient = apiClient.getAwsEmailConfig().getTestAddress();

    if (StringUtils.isBlank((recipient))) {
      apiClient.setLastMessage("Test message cannot be set with out the Account's \"Email Test\" setting.");
      config.getAccountStore().update(account);
      return redirect();
    }

    String override = apiClient.getAwsEmailConfig().getRecipientOverride();
    if (StringUtils.isNotBlank(override)) {
      recipient = override;
    }

    String when = Formats.defaultStamp(new java.util.Date());
    String msg = String.format("<html><head><title>Some Email</title></head><body style='background-color:red'><div style='background-color:#c0c0ff'><h1>Testing 123</h1>This is a test message from Cosmic Push sent at %s.</div></body>", when);
    String subject = "AWS-SMS test message from Cosmic Push";
    EmailPush action = new EmailPush(recipient, recipient, subject, msg,
                                     null, BeanUtils.toMap("aws-test:true"));

    String remoteAddress = config.getRequest().getRemoteAddr();
    InetAddress inetAddress = InetAddress.getByName(remoteAddress);

    ApiRequest apiRequest = new ApiRequest(apiClient, action, inetAddress);
    config.getApiRequestStore().create(apiRequest);

    new AwsEmailDelegate(config.getObjectMapper(), config.getApiRequestStore(), account, apiClient, apiRequest, action).run();

    msg = String.format("Test message sent to %s:\n%s", recipient, msg);
    apiClient.setLastMessage(msg);
    config.getAccountStore().update(account);

    return redirect();
  }
}
TOP

Related Classes of com.cosmicpush.app.resources.manage.client.ManageAwsEmailApi

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.