EmailChannel.java
5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* 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;
}
}
}