Package com.cosmicpush.app.resources.api

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

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

package com.cosmicpush.app.resources.api;

import com.cosmicpush.app.domain.accounts.Account;
import com.cosmicpush.app.domain.clients.ApiClient;
import com.cosmicpush.app.domain.clients.config.SmtpEmailConfig;
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;
import org.crazyyak.dev.domain.comm.AuthenticationMethod;
import org.crazyyak.dev.net.email.EmailMessage;

public class SmtpSmsDelegate extends AbstractDelegate {

  private final Account account;
  private final ApiClient apiClient;

  private final EmailToSmsPush push;

  public SmtpSmsDelegate(CpServerObjectMapper objectMapper, ApiRequestStore apiRequestStore, Account account, ApiClient apiClient, ApiRequest apiRequest, EmailToSmsPush push) {
    super(objectMapper, apiRequest, apiRequestStore);
    this.push = ExceptionUtils.assertNotNull(push, "push");
    this.account = ExceptionUtils.assertNotNull(account, "account");
    this.apiClient = ExceptionUtils.assertNotNull(apiClient, "apiClient");
  }

  @Override
  public synchronized RequestStatus processRequest() throws Exception {
    String reasonNotPermitted = account.getReasonNotPermitted(push);
    if (StringUtils.isNotBlank(reasonNotPermitted)) {
      return apiRequest.denyRequest(reasonNotPermitted);
    }

    String apiMessage = null;
    SmtpEmailConfig config = apiClient.getSmtpEmailConfig();

    EmailMessage message;

    if (StringUtils.isNotBlank(config.getRecipientOverride())) {
      // This is NOT a "production" request.
      message = new EmailMessage(config.getServerName(), config.getPortNumber(), config.getRecipientOverride());
      apiMessage = String.format("Request sent to recipient override, %s.", config.getRecipientOverride());
    } else {
      // This IS a "production" request.
      message = new EmailMessage(config.getServerName(), config.getPortNumber(), push.getToAddress());
    }

    if (config.getAuthType().isTls()) {
      message.setAuthentication(AuthenticationMethod.TLS, config.getUserName(), config.getPassword());
    } else if (config.getAuthType().isSsl()) {
      message.setAuthentication(AuthenticationMethod.SSL, config.getUserName(), config.getPassword());
    } else {
      message.setAuthentication(AuthenticationMethod.NONE, config.getUserName(), config.getPassword());
    }

    message.setFrom(push.getFromAddress());
    message.send("", push.getMessage(), null);

    return apiRequest.processed(apiMessage);
  }
}
TOP

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

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.