<commit_before>/**
 * Copyright (C) 2000 - 2013 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.silverpeas.authentication.password;

import com.stratelia.webactiv.beans.admin.AdministrationServiceProvider;
import org.silverpeas.util.MimeTypes;
import com.stratelia.silverpeas.silvertrace.SilverTrace;
import org.silverpeas.util.ResourceLocator;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.event.TransportEvent;
import javax.mail.event.TransportListener;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

public class ForgottenPasswordMailManager {

  private static final String TRANSPORT_SMTP = "smtp";
  private static final String TRANSPORT_SMTPS = "smtps";
  private static final String SUBJECT_ENCODING = "UTF-8";

  private static final String PREFIX_RESET_PASSWORD_REQUEST = "resetPasswordRequest";
  private static final String PREFIX_NEW_PASSWORD = "<PASSWORD>";
  private static final String PREFIX_ERROR = "error";
  private static final String PREFIX_ADMIN = "admin";
  private static final String CONTENT = "content";
  private static final String SUBJECT = "subject";

  private ResourceLocator resource = new ResourceLocator(
      "com.silverpeas.authentication.multilang.forgottenPasswordMail", "");

  // SMTP parameters
  private String smtpHost;
  private boolean smtpAuthentication;
  private boolean smtpSecure;
  private boolean smtpDebug;
  private int smtpPort;
  private String smtpUser;
  private String smtpPwd;
  private Session session;
  private String fromAddress;
  private String fromName;
  private String adminEmail;

  public ForgottenPasswordMailManager() {
    initSmtpParameters();
    initFromAddress();
  }

  private void initSmtpParameters() {
    ResourceLocator smtpSettings = new ResourceLocator(
        "com.stratelia.silverpeas.notificationserver.channel.smtp.smtpSettings", "");
    smtpHost = smtpSettings.getString("SMTPServer");
    smtpAuthentication = smtpSettings.getBoolean("SMTPAuthentication", false);
    smtpDebug = smtpSettings.getBoolean("SMTPDebug", false);
    smtpPort = Integer.parseInt(smtpSettings.getString("SMTPPort"));
    smtpUser = smtpSettings.getString("SMTPUser");
    smtpPwd = smtpSettings.getString("SMTPPwd");
    smtpSecure = smtpSettings.getBoolean("SMTPSecure", false);

    Properties props = System.getProperties();
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.auth", String.valueOf(smtpAuthentication));
    session = Session.getInstance(props, null);
    session.setDebug(smtpDebug); // print on the console all SMTP messages.
  }

  private void initFromAddress() {
    ResourceLocator mailSettings = new ResourceLocator(
        "com.silverpeas.authentication.settings.forgottenPasswordMail", "");
    adminEmail =
        mailSettings.getString("admin.mail", AdministrationServiceProvider.getAdminService()
        .getAdministratorEmail());
    fromAddress = mailSettings.getString("fromAddress", adminEmail);
    fromName = mailSettings.getString("fromName", "Silverpeas");
  }

  public void sendResetPasswordRequestMail(ForgottenPasswordMailParameters parameters)
      throws MessagingException {
    sendMail(parameters, PREFIX_RESET_PASSWORD_REQUEST);
  }

  public void sendNewPasswordMail(ForgottenPasswordMailParameters parameters)
      throws MessagingException {
    sendMail(parameters, PREFIX_NEW_PASSWORD);
  }

  public void sendErrorMail(ForgottenPasswordMailParameters parameters)
      throws MessagingException {
    parameters.setToAddress(adminEmail);
    sendMail(parameters, PREFIX_ERROR);
  }

  public void sendAdminMail(ForgottenPasswordMailParameters parameters)
      throws MessagingException {
    parameters.setToAddress(adminEmail);
    sendMail(parameters, PREFIX_ADMIN);
  }

  private void sendMail(ForgottenPasswordMailParameters parameters, String resourcePrefix)
      throws MessagingException {
    parameters.setSubject(resource.getString(resourcePrefix + "." + SUBJECT));
    parameters.setContent(resource.getString(resourcePrefix + "." + CONTENT));
    sendMail(parameters);
  }

  private void sendMail(ForgottenPasswordMailParameters parameters) throws MessagingException {
    Transport transport = null;
    try {
      MimeMessage msg = new MimeMessage(session);
      try {
        msg.setFrom(new InternetAddress(fromAddress, fromName, "UTF-8"));
      } catch (UnsupportedEncodingException e1) {
        msg.setFrom(new InternetAddress(fromAddress));
      }
      msg.setSubject(parameters.getSubject(), SUBJECT_ENCODING);
      msg.setContent(parameters.getFilledContent(), MimeTypes.HTML_MIME_TYPE);
      msg.setSentDate(new Date());

      // create a Transport connection
      if (smtpSecure) {
        transport = session.getTransport(TRANSPORT_SMTPS);
      } else {
        transport = session.getTransport(TRANSPORT_SMTP);
      }

      // redefine the TransportListener interface.
      TransportListener transportListener = new TransportListener() {
        public void messageDelivered(TransportEvent e) {
        }

        public void messageNotDelivered(TransportEvent e) {
        }

        public void messagePartiallyDelivered(TransportEvent e) {
        }
      };

      transport.addTransportListener(transportListener);

      InternetAddress[] addresses = { new InternetAddress(parameters.getToAddress()) };
      msg.setRecipients(Message.RecipientType.TO, addresses);
      // add Transport Listener to the transport connection.
      if (smtpAuthentication) {
        SilverTrace.info("Bus - peasCore", "ForgottenPasswordMailManager.sendMail()",
            "root.MSG_GEN_PARAM_VALUE", "host = " + smtpHost + " m_Port=" + smtpPort
            + " m_User=" + smtpUser);
        transport.connect(smtpHost, smtpPort, smtpUser, smtpPwd);
        msg.saveChanges();
      } else {
        transport.connect();
      }
      transport.sendMessage(msg, addresses);
    } finally {
      if (transport != null) {
        try {
          transport.close();
        } catch (Exception e) {
          SilverTrace.error("Bus - peasCore", "ForgottenPasswordMailManager.sendMail()",
              "root.EX_IGNORED", "ClosingTransport", e);
        }
      }
    }
  }

}
<commit_msg>Wiring forgotten password treatment with the new Mail Sending API.<commit_after>/**
 * Copyright (C) 2000 - 2013 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.silverpeas.authentication.password;

import org.silverpeas.util.ResourceLocator;
import org.silverpeas.mail.MailSending;

import javax.mail.MessagingException;

import static org.silverpeas.mail.MailAddress.eMail;

public class ForgottenPasswordMailManager {

  private static final String PREFIX_RESET_PASSWORD_REQUEST = "resetPasswordRequest";
  private static final String PREFIX_NEW_PASSWORD = "<PASSWORD>";
  private static final String PREFIX_ERROR = "error";
  private static final String PREFIX_ADMIN = "admin";
  private static final String CONTENT = "content";
  private static final String SUBJECT = "subject";

  private ResourceLocator resource = new ResourceLocator(
      "com.silverpeas.authentication.multilang.forgottenPasswordMail", "");

  // SMTP parameters
  private String fromAddress;
  private String fromName;
  private String adminEmail;

  public ForgottenPasswordMailManager() {
    initFromAddress();
  }

  private void initFromAddress() {
    ResourceLocator mailSettings = new ResourceLocator(
        "com.silverpeas.authentication.settings.forgottenPasswordMail", "");
    adminEmail =
        mailSettings.getString("admin.mail", AdministrationServiceProvider.getAdminService()
        .getAdministratorEmail());
    fromAddress = mailSettings.getString("fromAddress", adminEmail);
    fromName = mailSettings.getString("fromName", "Silverpeas");
  }

  public void sendResetPasswordRequestMail(ForgottenPasswordMailParameters parameters)
      throws MessagingException {
    sendMail(parameters, PREFIX_RESET_PASSWORD_REQUEST);
  }

  public void sendNewPasswordMail(ForgottenPasswordMailParameters parameters)
      throws MessagingException {
    sendMail(parameters, PREFIX_NEW_PASSWORD);
  }

  public void sendErrorMail(ForgottenPasswordMailParameters parameters)
      throws MessagingException {
    parameters.setToAddress(adminEmail);
    sendMail(parameters, PREFIX_ERROR);
  }

  public void sendAdminMail(ForgottenPasswordMailParameters parameters)
      throws MessagingException {
    parameters.setToAddress(adminEmail);
    sendMail(parameters, PREFIX_ADMIN);
  }

  private void sendMail(ForgottenPasswordMailParameters parameters, String resourcePrefix)
      throws MessagingException {
    parameters.setSubject(resource.getString(resourcePrefix + "." + SUBJECT));
    parameters.setContent(resource.getString(resourcePrefix + "." + CONTENT));
    MailSending.from(eMail(fromAddress).withName(fromName)).to(eMail(parameters.getToAddress()))
        .withSubject(parameters.getSubject()).withContent(parameters.getFilledContent()).send();
  }
}

{'fraction_non_alphanumeric': 0.08592644978783592, 'fraction_numerical': 0.00221004243281471, 'mean_word_length': 4.313762329732269, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6652680', 'n_tokens_mistral': 3125, 'n_tokens_neox': 2999, 'n_words': 824}