ProfileHelper.java
16.4 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.foundation.forms.FormsHelper
* com.day.cq.wcm.foundation.forms.ValidationInfo
* javax.servlet.http.HttpServletRequest
* org.apache.sling.api.SlingHttpServletRequest
* 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.apache.sling.api.scripting.SlingBindings
* org.apache.sling.api.scripting.SlingScriptHelper
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.campaign.profile;
import com.day.cq.mcm.campaign.ACConnectorException;
import com.day.cq.mcm.campaign.Defs;
import com.day.cq.mcm.campaign.profile.CampaignMetaData;
import com.day.cq.mcm.campaign.profile.MetaDataInstance;
import com.day.cq.mcm.campaign.profile.MetaDataRetriever;
import com.day.cq.mcm.campaign.profile.OptionValue;
import com.day.cq.mcm.campaign.profile.Options;
import com.day.cq.mcm.campaign.profile.Profile;
import com.day.cq.mcm.campaign.profile.ProfileRetriever;
import com.day.cq.mcm.campaign.profile.Subscriptions;
import com.day.cq.mcm.campaign.profile.SubscriptionsManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.foundation.forms.FormsHelper;
import com.day.cq.wcm.foundation.forms.ValidationInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.sling.api.SlingHttpServletRequest;
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.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Deprecated
public class ProfileHelper {
private static final String ATTRIB_METADATA = CampaignMetaData.class.toString();
private static final String ATTRIB_PROFILE = Profile.class.toString();
private static final String ATTRIB_SUBSCRIPTIONS = Subscriptions.class.toString();
private static final Logger log = LoggerFactory.getLogger(ProfileHelper.class);
private ProfileHelper() {
}
private static Resource searchFormStart(Resource resource) {
Resource current;
if (resource.getName().equals("jcr:content")) {
return null;
}
if (resource.getPath().lastIndexOf("/") == 0) {
return null;
}
if (ResourceUtil.isA((Resource)resource, (String)"foundation/components/form/start")) {
return resource;
}
Resource parent = resource.getParent();
ArrayList<Resource> predecessor = new ArrayList<Resource>();
Iterator children = parent.listChildren();
while (children.hasNext() && !(current = (Resource)children.next()).getPath().equals(resource.getPath())) {
predecessor.add(current);
}
Collections.reverse(predecessor);
for (Resource current2 : predecessor) {
if (ResourceUtil.isA((Resource)current2, (String)"foundation/components/form/start")) {
return current2;
}
if (!ResourceUtil.isA((Resource)current2, (String)"foundation/components/form/end")) continue;
return null;
}
return ProfileHelper.searchFormStart(parent);
}
public static CampaignMetaData getMetaData(SlingHttpServletRequest request) throws ACConnectorException {
CampaignMetaData metaData = (CampaignMetaData)request.getAttribute(ATTRIB_METADATA);
if (metaData == null) {
SlingBindings bindings = (SlingBindings)request.getAttribute(SlingBindings.class.getName());
if (bindings == null) {
throw new ACConnectorException("No sling bindings available - utility class is meant to be usedfrom Sling scripts only.");
}
MetaDataRetriever retriever = (MetaDataRetriever)bindings.getSling().getService(MetaDataRetriever.class);
if (retriever == null) {
throw new ACConnectorException("Could not determine 'MetaDataRetriever' service.");
}
Resource resource = request.getResource();
PageManager pageManager = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
Page page = pageManager.getContainingPage(resource);
if (page == null) {
throw new ACConnectorException("Resource '" + resource.getPath() + "' is not part of a page.");
}
metaData = retriever.retrieve((Resource)page.adaptTo(Resource.class));
request.setAttribute(ATTRIB_METADATA, (Object)metaData);
}
return metaData;
}
public static Options getOptions(SlingHttpServletRequest request) throws ACConnectorException {
MetaDataInstance data;
int sepPos;
CampaignMetaData metaData;
ValueMap values = (ValueMap)request.getResource().adaptTo(ValueMap.class);
String mapping = (String)values.get("acMapping", String.class);
String blockId = null;
String dataId = null;
if (mapping != null && (sepPos = mapping.indexOf(".")) >= 0) {
blockId = mapping.substring(0, sepPos);
dataId = mapping.substring(sepPos + 1);
}
if (dataId != null && (data = (metaData = ProfileHelper.getMetaData(request)).getData(blockId, dataId)) != null) {
return data.hasOptions() ? data.getOptions() : null;
}
return null;
}
public static Map<String, String> getOptionsAsMap(SlingHttpServletRequest request) {
LinkedHashMap<String, String> optionsMap = null;
try {
Options options = ProfileHelper.getOptions(request);
if (options != null) {
optionsMap = new LinkedHashMap<String, String>(4);
Iterator<OptionValue> values = options.getValues();
while (values.hasNext()) {
OptionValue value = values.next();
optionsMap.put(value.getValue(), value.getLabel());
}
}
}
catch (ACConnectorException e) {
log.warn("Could not determine options", (Throwable)e);
}
return optionsMap;
}
public static Profile getProfile(SlingHttpServletRequest request, String encryptedPK) throws ACConnectorException {
Profile profile = (Profile)request.getAttribute(ATTRIB_PROFILE);
if (profile == null) {
CampaignMetaData metaData = ProfileHelper.getMetaData(request);
SlingBindings bindings = (SlingBindings)request.getAttribute(SlingBindings.class.getName());
if (bindings == null) {
throw new ACConnectorException("No sling bindings available - utility class is meant to be usedfrom Sling scripts only.");
}
ProfileRetriever retriever = (ProfileRetriever)bindings.getSling().getService(ProfileRetriever.class);
if (retriever == null) {
throw new ACConnectorException("Could not determine 'ProfileRetriever' service.");
}
Resource resource = request.getResource();
PageManager pageManager = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
Page page = pageManager.getContainingPage(resource);
if (page == null) {
throw new ACConnectorException("Resource '" + resource.getPath() + "' is not part of a page.");
}
profile = retriever.load((Resource)page.adaptTo(Resource.class), encryptedPK, metaData);
request.setAttribute(ATTRIB_PROFILE, (Object)profile);
}
return profile;
}
public static String determineEncryptedPK(SlingHttpServletRequest request) {
log.debug("Determining encrypted PK");
Resource resource = request.getResource();
Resource form = ProfileHelper.searchFormStart(resource);
if (form == null) {
log.debug("Form resource not found");
return null;
}
log.debug("Form resource: {}", (Object)form.getPath());
Iterator fields = FormsHelper.getFormElements((Resource)form);
while (fields.hasNext()) {
Resource field = (Resource)fields.next();
log.debug("Processing component '{}' ({})", (Object[])new String[]{field.getName(), field.getResourceType()});
if (!ResourceUtil.isA((Resource)field, (String)"mcm/campaign/components/form/profile/encryptedPK")) continue;
log.debug("Encrypted PK field found: {}" + field.getName());
ValueMap values = ResourceUtil.getValueMap((Resource)field);
String urlParameter = (String)values.get("urlParameter", String.class);
if (urlParameter == null) {
log.debug("Missing property 'urlParameter'");
return null;
}
log.debug("URL parameter for encrypted PK is: {}", (Object)urlParameter);
return request.getParameter(urlParameter);
}
return null;
}
public static Profile getProfile(SlingHttpServletRequest request) throws ACConnectorException {
Profile profile = (Profile)request.getAttribute(ATTRIB_PROFILE);
if (profile == null) {
log.debug("Loading profile data from Adobe Campaign instance.");
String encryptedPK = ProfileHelper.determineEncryptedPK(request);
log.debug("Encrypted primary key: {}", (Object)encryptedPK);
if (encryptedPK != null) {
profile = ProfileHelper.getProfile(request, encryptedPK);
}
}
return profile;
}
public static MetaDataInstance getMetaDataInstance(SlingHttpServletRequest request) {
Resource resource = request.getResource();
try {
log.debug("Profile received successfully,");
CampaignMetaData metaData = ProfileHelper.getMetaData(request);
if (metaData == null) {
return null;
}
log.debug("Meta data received successfully,");
ValueMap values = ResourceUtil.getValueMap((Resource)resource);
String mapping = (String)values.get("acMapping", String.class);
if (mapping == null) {
return null;
}
log.debug("Mapping determined successfully: {}", (Object)mapping);
int sepPos = mapping.indexOf(".");
if (sepPos < 0) {
return null;
}
String blockId = mapping.substring(0, sepPos);
String dataId = mapping.substring(sepPos + 1);
return metaData.getData(blockId, dataId);
}
catch (ACConnectorException e) {
log.warn("Could not determine meta data instance.", (Throwable)e);
return null;
}
}
public static Object getValue(SlingHttpServletRequest request) {
Resource resource = request.getResource();
String name = FormsHelper.getParameterName((Resource)resource);
ValidationInfo info = ValidationInfo.getValidationInfo((HttpServletRequest)request);
if (info != null) {
log.debug("Validation info found for field '{}'", (Object)name);
String[] valuesArray = info.getValues(name);
if (valuesArray.length == 1) {
log.debug("Taking value from validation info: {}", (Object)valuesArray[0]);
return valuesArray[0];
}
}
Object value = null;
try {
MetaDataInstance instance;
log.debug("Determining profile value.");
Profile profile = ProfileHelper.getProfile(request);
if (profile != null && (instance = ProfileHelper.getMetaDataInstance(request)) != null) {
log.debug("MetaDataInstance: {}", (Object)instance.getId());
value = profile.getValue(instance);
log.debug("Profile value: {}", value);
}
}
catch (ACConnectorException e) {
log.warn("Could not determine profile");
}
return value;
}
public static List<String> getValuesAsList(SlingHttpServletRequest request) {
ArrayList<String> values = new ArrayList<String>(1);
Object value = ProfileHelper.getValue(request);
if (value != null && value instanceof String) {
values.add((String)value);
}
return values;
}
public static boolean isReconciliationKey(SlingHttpServletRequest request) {
Resource resource = request.getResource();
ValueMap values = ResourceUtil.getValueMap((Resource)resource);
return values != null && "true".equals(values.get("acReconciliationKey", String.class));
}
public static boolean getValueAsBoolean(SlingHttpServletRequest request) {
Object valueObj = ProfileHelper.getValue(request);
if (valueObj != null) {
if (valueObj instanceof Boolean) {
return (Boolean)valueObj;
}
if (valueObj instanceof String) {
String valueStr = (String)valueObj;
for (String trueValue : Defs.BOOLEAN_TRUE_VALUES) {
if (!valueStr.equals(trueValue)) continue;
return true;
}
}
}
return false;
}
public static String getBooleanTrue(SlingHttpServletRequest request) {
Object valueObj = ProfileHelper.getValue(request);
if (valueObj == null || !(valueObj instanceof String)) {
return "true";
}
String valueStr = (String)valueObj;
for (String trueValue : Defs.BOOLEAN_TRUE_VALUES) {
if (!valueStr.equals(trueValue)) continue;
return trueValue;
}
int i = 0;
for (String falseValue : Defs.BOOLEAN_FALSE_VALUES) {
if (valueStr.equals(falseValue)) {
return Defs.BOOLEAN_TRUE_VALUES[i];
}
++i;
}
return "true";
}
public static String getBooleanFalse(SlingHttpServletRequest request) {
Object valueObj = ProfileHelper.getValue(request);
if (valueObj == null || !(valueObj instanceof String)) {
return "false";
}
String valueStr = (String)valueObj;
for (String falseValue : Defs.BOOLEAN_FALSE_VALUES) {
if (!valueStr.equals(falseValue)) continue;
return falseValue;
}
int i = 0;
for (String trueValue : Defs.BOOLEAN_TRUE_VALUES) {
if (valueStr.equals(trueValue)) {
return Defs.BOOLEAN_FALSE_VALUES[i];
}
++i;
}
return "false";
}
public static Subscriptions getSubscriptions(SlingHttpServletRequest req) {
Subscriptions subscriptions = (Subscriptions)req.getAttribute(ATTRIB_SUBSCRIPTIONS);
if (subscriptions == null) {
try {
SlingBindings bindings = (SlingBindings)req.getAttribute(SlingBindings.class.getName());
if (bindings == null) {
throw new ACConnectorException("No sling bindings available - utility class is meant to be used from Sling scripts only.");
}
SubscriptionsManager sm = (SubscriptionsManager)bindings.getSling().getService(SubscriptionsManager.class);
if (sm == null) {
throw new ACConnectorException("Could not determine 'SubscriptionsManager' service.");
}
Resource resource = req.getResource();
PageManager pageManager = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
Page page = pageManager.getContainingPage(resource);
if (page == null) {
throw new ACConnectorException("Resource '" + resource.getPath() + "' is not part of a page.");
}
subscriptions = sm.retrieve((Resource)page.adaptTo(Resource.class));
req.setAttribute(ATTRIB_SUBSCRIPTIONS, (Object)subscriptions);
}
catch (ACConnectorException ace) {
log.warn("Could not determine subscriptions: {}", (Object)ace.getMessage());
subscriptions = null;
}
}
return subscriptions;
}
}