EmailChannel.java 5.43 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.mailer.MailService
 *  com.day.cq.mailer.MailingException
 *  org.apache.commons.mail.Email
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.sling.commons.osgi.OsgiUtil
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.InvalidSyntaxException
 *  org.osgi.framework.ServiceReference
 *  org.osgi.service.component.ComponentContext
 *  org.osgi.service.event.Event
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.notification.email.impl;

import com.day.cq.mailer.MailService;
import com.day.cq.mailer.MailingException;
import com.day.cq.wcm.notification.Channel;
import com.day.cq.wcm.notification.NotificationContext;
import com.day.cq.wcm.notification.email.EmailBuilder;
import java.util.Dictionary;
import org.apache.commons.mail.Email;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1)
@Service
@Properties(value={@Property(name="type", value={"email"}, propertyPrivate=1), @Property(name="email.from", value={"cq5@acme.com"})})
public class EmailChannel
implements Channel {
    private static final Logger log = LoggerFactory.getLogger(EmailChannel.class);
    protected static final String EMAIL_TYPE = "email";
    protected static final String PROPERTY_EMAILFROM_NAME = "email.from";
    protected static final String PROPERTY_EMAILFROM_DEFAULT = "cq5@acme.com";
    @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
    private volatile MailService mailer;
    private BundleContext bundleContext;
    private String addressFrom;

    @Override
    public void publish(NotificationContext context, Event event) throws Exception {
        String userId = context.getAuthorizable().getID();
        if (null == this.mailer) {
            log.error("could not send email notification to [{}], mail service unavailable.", (Object)userId);
            return;
        }
        EmailBuilder builder = this.lookupBuilder(context, event);
        if (null != builder) {
            if (builder.shouldBuild(context, event)) {
                Email email = builder.build(context, event, this.addressFrom);
                if (null == email) {
                    log.error("build of email failed, not sending email notification for user [{}].", (Object)userId);
                    return;
                }
                try {
                    this.mailer.send((Object)email);
                    log.info("sent notification email to [{}].", (Object)userId);
                }
                catch (MailingException e) {
                    log.error("could not send email notification to [{}], a sending error occurred: {}", (Object)userId, (Object)e);
                }
            } else {
                log.info("not sending email notification for user [{}], shouldBuild returned false", (Object)userId);
            }
        } else {
            log.error("could not send email notification to [{}], no suitable email builder found for type [{}]", (Object)userId, (Object)event.getTopic());
        }
    }

    private EmailBuilder lookupBuilder(NotificationContext context, Event event) {
        try {
            ServiceReference[] refs = this.bundleContext.getServiceReferences(EmailBuilder.class.getName(), "(topic=*)");
            if (null != refs) {
                for (ServiceReference ref : refs) {
                    EmailBuilder builder = (EmailBuilder)this.bundleContext.getService(ref);
                    if (!builder.accepts(context, event)) continue;
                    return builder;
                }
            }
        }
        catch (InvalidSyntaxException e) {
            log.error("could not look up email builder services: {}", (Throwable)e);
        }
        return null;
    }

    protected void activate(ComponentContext context) {
        this.bundleContext = context.getBundleContext();
        Dictionary properties = context.getProperties();
        this.addressFrom = OsgiUtil.toString(properties.get("email.from"), (String)"cq5@acme.com");
    }

    protected void deactivate(ComponentContext context) {
        this.mailer = null;
        this.bundleContext = null;
    }

    protected void bindMailer(MailService mailService) {
        this.mailer = mailService;
    }

    protected void unbindMailer(MailService mailService) {
        if (this.mailer == mailService) {
            this.mailer = null;
        }
    }
}