AbstractEmailBuilder.java
8.66 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.security.user.UserProperties
* com.adobe.granite.security.user.UserPropertiesManager
* javax.jcr.Binary
* javax.jcr.Credentials
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Repository
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.SimpleCredentials
* org.apache.commons.lang.StringUtils
* org.apache.commons.mail.Email
* org.apache.commons.mail.EmailException
* org.apache.commons.mail.SimpleEmail
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.service.event.Event
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.notification.email;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.day.cq.wcm.notification.NotificationContext;
import com.day.cq.wcm.notification.email.EmailBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.jcr.Binary;
import javax.jcr.Credentials;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.event.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractEmailBuilder
implements EmailBuilder {
protected static final String TEMPLATE_ROOT_PATH = "/etc/notification/email/default";
protected static final String DEFAULT_CONTENT_TYPE = "text/plain; charset=utf-8";
protected static final String DEFAULT_CHARSET = "UTF-8";
protected static final String NOTIFICATION_SERVICE = "notification-service";
private final Logger log;
public AbstractEmailBuilder() {
this.log = LoggerFactory.getLogger((String)this.getClass().getName());
}
protected abstract ResourceResolverFactory getResolverFactory();
protected abstract Map<String, String> getHeaders(NotificationContext var1, Event var2);
protected abstract String getSubject(NotificationContext var1, Event var2, Properties var3);
protected abstract String getMessage(NotificationContext var1, Event var2, Properties var3);
protected String getContentType() {
return "text/plain; charset=utf-8";
}
protected String getCharset() {
return "UTF-8";
}
protected String getTemplateRootPath() {
return "/etc/notification/email/default";
}
@Override
public boolean shouldBuild(NotificationContext context, Event event) {
return true;
}
public abstract Repository getRepository();
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public Email build(NotificationContext context, Event event, String addressFrom) {
SimpleEmail email = null;
String addressTo = null;
Session userSession = null;
ResourceResolver resolver = null;
try {
SimpleCredentials credentials = new SimpleCredentials(context.getAuthorizable().getID(), new char[0]);
userSession = ((SlingRepository)this.getRepository()).impersonateFromService("notification-service", (Credentials)credentials, null);
resolver = this.getResolverFactory().getResourceResolver(Collections.singletonMap("user.jcr.session", userSession));
UserPropertiesManager userPropertiesManager = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
UserProperties userProps = userPropertiesManager.getUserProperties(context.getAuthorizable().getID(), "profile");
addressTo = userProps.getProperty("email");
}
catch (LoginException e) {
this.log.error("error obtaining resource resolver: ", (Throwable)e);
}
catch (RepositoryException e) {
this.log.error("error obtaining user properties: ", (Throwable)e);
}
finally {
if (resolver != null) {
resolver.close();
}
if (userSession != null) {
userSession.logout();
}
}
if (StringUtils.isNotBlank((String)addressTo)) {
try {
Properties properties = this.loadEmailProperties(context, event);
email = new SimpleEmail();
HashMap<String, String> headerMap = new HashMap<String, String>();
headerMap.put("Content-Type", this.getContentType());
headerMap.putAll(this.getHeaders(context, event));
email.setHeaders(headerMap);
email.setCharset(this.getCharset());
email.addTo(addressTo);
email.setFrom(StringUtils.defaultIfEmpty((String)addressFrom, (String)"cq5@acme.com"));
email.setSubject(this.getSubject(context, event, properties));
email.setMsg(this.getMessage(context, event, properties));
return email;
}
catch (RepositoryException e) {
this.log.error("failed building notification email template for user with destination [" + addressTo + "]: {}", (Throwable)e);
}
catch (IOException e) {
this.log.error("failed loading email text for user with destination [" + addressTo + "]: {}", (Throwable)e);
}
catch (EmailException e) {
this.log.error("failed building email for user with destination [" + addressTo + "]: {}", (Throwable)e);
}
} else {
this.log.error("failed building notification email, user has no email address set.");
}
return email;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected Properties loadEmailProperties(NotificationContext context, Event event) throws RepositoryException, IOException {
Properties properties = new Properties();
Session userSession = null;
Session serviceSession = null;
ResourceResolver resolver = null;
try {
SimpleCredentials credentials = new SimpleCredentials(context.getAuthorizable().getID(), new char[0]);
userSession = ((SlingRepository)this.getRepository()).impersonateFromService("notification-service", (Credentials)credentials, null);
resolver = this.getResolverFactory().getResourceResolver(Collections.singletonMap("user.jcr.session", userSession));
UserPropertiesManager userPropertiesManager = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
UserProperties preferences = userPropertiesManager.getUserProperties(context.getAuthorizable().getID(), "preferences");
String language = null != preferences ? StringUtils.defaultIfEmpty((String)preferences.getProperty("language"), (String)"en") : "en";
String templateRoot = this.getTemplateRootPath();
String path = templateRoot + "/" + event.getTopic().replace('/', '.') + "/" + language + ".txt";
serviceSession = ((SlingRepository)this.getRepository()).loginService("notification-service", null);
Node node = serviceSession.getNode(path);
InputStream is = node.getNode("jcr:content").getProperty("jcr:data").getBinary().getStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, this.getCharset()));
properties.load(bufferedReader);
}
catch (LoginException e) {
this.log.error("error obtaining resource resolver: ", (Throwable)e);
}
finally {
if (resolver != null) {
resolver.close();
}
if (userSession != null) {
userSession.logout();
}
if (serviceSession != null) {
serviceSession.logout();
}
}
return properties;
}
}