SaveProfileServlet.java
6.74 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.webservicesupport.Configuration
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* 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.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.day.cq.mcm.campaign.servlets;
import com.day.cq.i18n.I18n;
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.servlets.AbstractProfileServlet;
import com.day.cq.mcm.campaign.servlets.util.EncryptedPKParameter;
import com.day.cq.mcm.campaign.servlets.util.ParameterMapper;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
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.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;
@SlingServlet(resourceTypes={"foundation/components/form/start", "mcm/campaign/components/profile"}, selectors={"campaign.profile.save"}, extensions={"html", "json"}, methods={"POST"})
public class SaveProfileServlet
extends AbstractProfileServlet {
private static final String PRM_INSERT = "insert";
private final Logger log;
@Reference
private CampaignConnector connector;
public SaveProfileServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void saveProfile(Page page, Map<String, String> parameters) throws CampaignException {
Resource resource = page.getContentResource();
ValueMap props = ResourceUtil.getValueMap((Resource)resource);
String mappingId = (String)props.get("acMapping", String.class);
if (mappingId == null) {
throw new ConfigurationException("Missing mapping information.");
}
parameters.put("mapping", mappingId);
parameters.put("resource", mappingId);
Configuration config = this.connector.getWebserviceConfig(resource);
CampaignCredentials credentials = this.connector.retrieveCredentials(config);
CallResults callResults = null;
try {
callResults = this.connector.postFunction("amcStoreProfile.jssp", parameters, credentials);
}
finally {
if (callResults != null) {
callResults.destroy();
}
}
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
EncryptedPKParameter epkp;
I18n i18n = new I18n((HttpServletRequest)request);
String formPath = request.getParameter(":formstart");
if (formPath == null) {
this.handleError(request, response, null, i18n.get("Missing parameter '{0}'.", null, new Object[]{":formstart"}));
return;
}
ResourceResolver resolver = request.getResourceResolver();
Resource form = resolver.getResource(formPath);
if (form == null) {
this.handleError(request, response, null, i18n.get("Could not determine form '{0}'.", null, new Object[]{formPath}));
return;
}
PageManager pageManager = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
Page page = pageManager.getContainingPage(formPath);
try {
boolean allowNewProfiles;
ParameterMapper mapper = new ParameterMapper();
mapper.parseParameters(request, form);
epkp = mapper.getEncryptedPKParameter();
LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
mapper.addEncryptedPK(parameters);
mapper.addReconciliationKey(parameters, true);
mapper.addData(parameters);
ValueMap formValues = ResourceUtil.getValueMap((Resource)form);
if (formValues.containsKey((Object)"acAllowNewProfiles") && (allowNewProfiles = ((Boolean)formValues.get("acAllowNewProfiles", Boolean.class)).booleanValue())) {
parameters.put("insert", "true");
}
this.saveProfile(page, parameters);
}
catch (ConfigurationException e) {
this.log.info("Invalid webservice configuration", (Throwable)e);
this.handleError(request, response, form, i18n.get("Missing or invalid webservice configuration."));
return;
}
catch (CampaignException e) {
this.log.error("Could not save profile data", (Throwable)e);
this.handleError(request, response, form, i18n.get("Could not save profile data."));
return;
}
String redirect = request.getParameter(":redirect");
if (redirect == null) {
String epkpValue;
redirect = page.getPath() + ".html";
if (epkp != null && (epkpValue = epkp.getValue()) != null && epkpValue.length() > 0) {
try {
redirect = redirect + "?" + epkp.getName() + "=" + URLEncoder.encode(epkp.getValue(), "UTF-8") + "&cq_ck=" + new Date().getTime();
}
catch (UnsupportedEncodingException uee) {
// empty catch block
}
}
}
response.sendRedirect(redirect);
}
protected void bindConnector(CampaignConnector campaignConnector) {
this.connector = campaignConnector;
}
protected void unbindConnector(CampaignConnector campaignConnector) {
if (this.connector == campaignConnector) {
this.connector = null;
}
}
}