CampaignSeedDataServlet.java
7.06 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.webservicesupport.Configuration
* javax.servlet.ServletOutputStream
* javax.servlet.http.HttpServletResponse
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.campaign.servlets;
import com.adobe.cq.mcm.campaign.CampaignProxy;
import com.adobe.cq.mcm.campaign.NewsletterException;
import com.adobe.cq.mcm.campaign.NewsletterManager;
import com.day.cq.mcm.campaign.CallResults;
import com.day.cq.mcm.campaign.CampaignConnector;
import com.day.cq.mcm.campaign.CampaignCredentials;
import com.day.cq.mcm.campaign.CampaignException;
import com.day.cq.mcm.campaign.ConfigurationException;
import com.day.cq.mcm.campaign.ConnectionException;
import com.day.cq.mcm.campaign.servlets.AbstractProxyServlet;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(selectors={"campaign.seeddata"}, resourceTypes={"mcm/campaign/components/newsletter", "mcm/campaign/components/campaign_newsletterpage", "mcm/campaign/components/profile"}, extensions={"json"}, methods={"GET"})
public class CampaignSeedDataServlet
extends AbstractProxyServlet {
private static final String PRM_TEMPLATE = "template";
private static final String PRM_DELIVERY = "delivery";
private static final String PRM_ID = "id";
private final Logger log;
@Reference
private CampaignProxy proxy;
@Reference
private CampaignConnector connector;
@Reference
private NewsletterManager manager;
public CampaignSeedDataServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Override
protected void performGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws CampaignException {
Resource resource = request.getResource();
Page page = (Page)resource.getParent().adaptTo(Page.class);
Configuration config = this.connector.getWebserviceConfig(resource);
CampaignCredentials credentials = this.connector.retrieveCredentials(config);
ValueMap values = ResourceUtil.getValueMap((Resource)resource);
String templateId = (String)values.get("acTemplateId", String.class);
if (templateId == null) {
throw new ConfigurationException("Missing template ID on newsletter");
}
String id = request.getRequestPathInfo().getSuffix();
if (id == null || id.length() < 2) {
throw new CampaignException("Invalid or no ID suffix");
}
id = id.substring(1);
HashMap<String, String> params = new HashMap<String, String>();
params.put("template", templateId);
params.put("id", id);
String[] links = null;
if (this.manager.isNewsletter(page)) {
try {
links = this.manager.getLinkedDeliveries(page);
}
catch (NewsletterException e) {
throw new CampaignException("Error getting the linked deliveries");
}
}
if (links != null) {
for (String deliveryId : links) {
this.log.debug("Trying delivery '{}'.", (Object)deliveryId);
params.put("delivery", deliveryId);
if (!this.checkAndProcess(params, credentials, (HttpServletResponse)response, this.connector)) continue;
this.log.debug("Success; using meta data of delivery '{}'.", (Object)deliveryId);
return;
}
}
this.proxy(this.proxy, "amcGetSeedData.jssp", new String[]{"template", templateId, "delivery", templateId, "id", id}, resource, (HttpServletResponse)response);
}
private boolean checkAndProcess(Map<String, String> params, CampaignCredentials credentials, HttpServletResponse response, CampaignConnector connector) throws CampaignException {
InputStream is = null;
CallResults callResults = null;
try {
callResults = connector.callFunction("amcGetSeedData.jssp", params, credentials);
Map<String, String> headers = callResults.getResponseHeaders();
for (String name2 : headers.keySet()) {
response.addHeader(name2, headers.get(name2));
}
is = callResults.bodyAsStream();
ServletOutputStream os = response.getOutputStream();
IOUtils.copy((InputStream)is, (OutputStream)os);
boolean name2 = true;
return name2;
}
catch (ConnectionException ce) {
if (ce.getStatusCode() != 404) {
if (ce.getStatusCode() == 500) {
throw new CampaignException("Remote error", ce);
}
throw new CampaignException("Caught exception while writing response", ce);
}
}
catch (IOException ioe) {
throw new CampaignException("Caught exception while writing response", ioe);
}
finally {
IOUtils.closeQuietly((InputStream)is);
if (callResults != null) {
callResults.destroy();
}
}
return false;
}
protected void bindProxy(CampaignProxy campaignProxy) {
this.proxy = campaignProxy;
}
protected void unbindProxy(CampaignProxy campaignProxy) {
if (this.proxy == campaignProxy) {
this.proxy = null;
}
}
protected void bindConnector(CampaignConnector campaignConnector) {
this.connector = campaignConnector;
}
protected void unbindConnector(CampaignConnector campaignConnector) {
if (this.connector == campaignConnector) {
this.connector = null;
}
}
protected void bindManager(NewsletterManager newsletterManager) {
this.manager = newsletterManager;
}
protected void unbindManager(NewsletterManager newsletterManager) {
if (this.manager == newsletterManager) {
this.manager = null;
}
}
}