MailHelper.java 3.93 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.mailer.MailService
 *  com.day.cq.mailer.MailingException
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.text.StrSubstitutor
 *  org.apache.commons.mail.Email
 *  org.apache.commons.mail.EmailException
 *  org.apache.commons.mail.SimpleEmail
 *  org.apache.sling.api.resource.LoginException
 */
package com.day.cq.dam.core.impl;

import com.day.cq.mailer.MailService;
import com.day.cq.mailer.MailingException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.sling.api.resource.LoginException;

public class MailHelper {
    private static final String EMAIL_FOOTER = "footer";
    private static final String EMAIL_MESSAGE = "message";
    private static final String EMAIL_HEADER = "header";
    private static final String EMAIL_SUBJECT = "subject";

    public static void sendMail(Session adminSession, MailService mailer, Map<String, String> sendTo, Map<String, String> parameters) throws RepositoryException, IOException, LoginException, EmailException, MailingException {
        String templateRootPath = parameters.get("templateRootPath");
        Node node = adminSession.getNode(templateRootPath);
        InputStream is = node.getNode("jcr:content").getProperty("jcr:data").getBinary().getStream();
        Properties properties = new Properties();
        properties.load(is);
        SimpleEmail email = null;
        email = new SimpleEmail();
        email.setCharset("UTF-8");
        HashMap<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("Content-Type", "text/plain; charset=utf-8");
        email.setHeaders(headerMap);
        email.setSubject(MailHelper.getSubject(properties, parameters, ""));
        for (Map.Entry<String, String> entry : sendTo.entrySet()) {
            email.addTo(entry.getValue());
            email.setMsg(MailHelper.getMessage(properties, parameters, entry.getKey()));
            if (mailer != null) {
                mailer.send((Object)email);
            }
            email.getToAddresses().clear();
        }
    }

    private static String getSubject(Properties properties, Map<String, String> parameters, String invitee) throws RepositoryException {
        Properties values = MailHelper.getValues(parameters, invitee);
        String subject = StrSubstitutor.replace(properties.get("subject"), (Map)values);
        return subject;
    }

    private static String getMessage(Properties properties, Map<String, String> parameters, String invitee) throws RepositoryException {
        Properties values = MailHelper.getValues(parameters, invitee);
        StringBuilder msg = new StringBuilder();
        msg.append(StrSubstitutor.replace(properties.get("header"), (Map)values));
        msg.append(StrSubstitutor.replace(properties.get("message"), (Map)values));
        msg.append(StrSubstitutor.replace(properties.get("footer"), (Map)values));
        return msg.toString();
    }

    private static Properties getValues(Map<String, String> parameters, String invitee) throws RepositoryException {
        Properties values = new Properties();
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            values.setProperty(entry.getKey(), entry.getValue());
        }
        values.setProperty("inviteeFirstName", invitee);
        values.setProperty("time", new Date().toString());
        return values;
    }
}