GuideMailServlet.java
4.6 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.mailer.MailService
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.commons.lang3.text.StrSubstitutor
* 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.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.servlets.OptingServlet
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.servlet;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.servlet.GuideEmail;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.day.cq.mailer.MailService;
import java.io.IOException;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.commons.lang3.text.StrSubstitutor;
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.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.servlets.OptingServlet;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"fd/af/components/guideContainer"}), @Property(name="sling.servlet.methods", value={"POST"}), @Property(name="service.description", value={"Guide Mail Service"}), @Property(name="sling.servlet.selectors", value={"af.mail"})})
public class GuideMailServlet
extends SlingAllMethodsServlet
implements OptingServlet {
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
protected MailService mailService;
private Logger logger = LoggerFactory.getLogger(GuideMailServlet.class);
protected static final String EXTENSION = "jsp";
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block5 : {
if (this.mailService == null) {
this.logger.error("Could not find email service");
throw new GuideException("Could not find email service");
}
GuideEmail guideEmail = new GuideEmail(request);
try {
String guideValues = request.getParameter("_guideValuesMap");
JSONObject guideValueMap = new JSONObject(guideValues);
StrSubstitutor substitutor = GuideUtils.getStringSubstitutor(guideValueMap);
Email email = guideEmail.createEmail(substitutor);
if (email != null) {
List toList = email.getToAddresses();
if (!toList.isEmpty()) {
this.mailService.send((Object)email);
}
break block5;
}
throw new GuideException("Email Headers (from and to addresses) not specified. Cannot send email");
}
catch (Exception e) {
this.logger.error("Could not create email" + e.getMessage(), (Throwable)e);
throw new GuideException(e);
}
}
response.sendRedirect(request.getContextPath() + request.getParameter(":redirect"));
}
public boolean accepts(SlingHttpServletRequest request) {
return "jsp".equals(request.getRequestPathInfo().getExtension());
}
protected void bindMailService(MailService mailService) {
this.mailService = mailService;
}
protected void unbindMailService(MailService mailService) {
if (this.mailService == mailService) {
this.mailService = null;
}
}
}