GuideEmail.java
7.82 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Session
* javax.mail.internet.InternetAddress
* org.apache.commons.io.IOUtils
* org.apache.commons.lang3.text.StrSubstitutor
* org.apache.commons.mail.ByteArrayDataSource
* org.apache.commons.mail.Email
* org.apache.commons.mail.MultiPartEmail
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.activation.DataSource;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;
import javax.mail.internet.InternetAddress;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.apache.commons.mail.ByteArrayDataSource;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class GuideEmail {
private String[] to;
private String[] cc;
private String[] bcc;
private String from;
private String subject;
private String template;
private String charSet;
private List<String> attachments;
private List<DataSource> fileAttachments;
private static final String DEFAULT_CHARSET = "utf-8";
private Logger logger = LoggerFactory.getLogger(GuideEmail.class);
public GuideEmail(SlingHttpServletRequest request) throws IOException {
Resource resource = request.getResource();
Session userSession = (Session)request.getResourceResolver().adaptTo(Session.class);
ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
String templatePath = null;
this.charSet = "utf-8";
String string = templatePath = properties.containsKey((Object)"templatePath") ? (String)properties.get("templatePath", (Object)"") : null;
if (templatePath != null) {
this.charSet = this.getCharSet(userSession, templatePath);
this.template = this.getTemplate(templatePath, this.charSet, userSession);
}
if (this.template == null) {
String string2 = this.template = properties.containsKey((Object)"template") ? (String)properties.get("template", (Object)"") : null;
}
if (properties.containsKey((Object)"mailto")) {
this.to = (String[])properties.get("mailto", (Object)new String[0]);
}
if (properties.containsKey((Object)"cc")) {
this.cc = (String[])properties.get("cc", (Object)new String[0]);
}
if (properties.containsKey((Object)"bcc")) {
this.bcc = (String[])properties.get("bcc", (Object)new String[0]);
}
if (properties.containsKey((Object)"from")) {
this.from = (String)properties.get("from", (Object)"");
}
if (properties.containsKey((Object)"subject")) {
this.subject = (String)properties.get("subject", (Object)"");
}
if (properties.containsKey((Object)"includeAttachments") && "true".equals(properties.get("includeAttachments", (Object)""))) {
this.attachments = new ArrayList<String>();
this.fileAttachments = new ArrayList<DataSource>();
String attachmentNames = request.getParameter("_guideAttachments");
if (attachmentNames != null) {
StringTokenizer stringTokenizer = new StringTokenizer(attachmentNames, ",");
while (stringTokenizer.hasMoreElements()) {
String filename = (String)stringTokenizer.nextElement();
InputStream att = request.getRequestParameter(filename).getInputStream();
this.fileAttachments.add((DataSource)new ByteArrayDataSource(att, request.getRequestParameter(filename).getContentType()));
this.attachments.add(filename.substring(filename.indexOf("/") + 1));
}
}
}
}
public Email createEmail(StrSubstitutor substitutor) throws Exception {
String substitutedSubject;
MultiPartEmail email = new MultiPartEmail();
email.setCharset(this.charSet);
InternetAddress af = null;
if (this.to != null) {
for (String toAddr : this.to) {
toAddr = substitutor.replace(toAddr);
af = new InternetAddress(toAddr);
email.addTo(af.getAddress(), af.getPersonal());
}
} else {
return null;
}
if (this.cc != null) {
for (String ccAddr : this.cc) {
ccAddr = substitutor.replace(ccAddr);
af = new InternetAddress(ccAddr);
email.addCc(af.getAddress(), af.getPersonal());
}
}
if (this.bcc != null) {
for (String bccAddr : this.bcc) {
bccAddr = substitutor.replace(bccAddr);
af = new InternetAddress(bccAddr);
email.addBcc(af.getAddress(), af.getPersonal());
}
}
if (this.from == null) {
return null;
}
String fromAddr = substitutor.replace(this.from);
af = new InternetAddress(fromAddr);
email.setFrom(af.getAddress(), af.getPersonal());
if (this.subject != null && (substitutedSubject = substitutor.replace(this.subject)).length() > 0) {
email.setSubject(substitutedSubject);
}
if (this.template != null) {
String substitutedTemplate = substitutor.replace(this.template);
if (substitutedTemplate.length() == 0) {
substitutedTemplate = " ";
}
email.setMsg(substitutedTemplate);
}
if (this.fileAttachments != null) {
int i = 0;
for (DataSource source : this.fileAttachments) {
email.attach(source, this.attachments.get(i), this.attachments.get(i));
++i;
}
}
return email;
}
private String getCharSet(Session session, String path) {
String encoding = "utf-8";
try {
Node content = session.getNode(path + "/" + "jcr:content");
encoding = content.hasProperty("jcr:encoding") ? content.getProperty("jcr:encoding").getString() : "utf-8";
}
catch (Exception e) {
this.logger.error(e.getMessage(), (Throwable)e);
}
return encoding;
}
public String getTemplate(String templatePath, String charSet, Session userSession) {
StringWriter w = new StringWriter();
try {
Node content = userSession.getNode(templatePath + "/" + "jcr:content");
InputStream is = content.getProperty("jcr:data").getBinary().getStream();
InputStreamReader r = new InputStreamReader(is, charSet);
IOUtils.copy((Reader)r, (Writer)w);
return w.toString();
}
catch (Exception e) {
this.logger.error(e.toString(), (Throwable)e);
return null;
}
}
public String getTemplate() {
return this.template;
}
}