MboxHelper.java
10.9 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.analytics.sitecatalyst.Framework
* com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
* com.day.cq.commons.inherit.InheritanceValueMap
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.WCMMode
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.util;
import com.day.cq.analytics.sitecatalyst.Framework;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MboxHelper {
private static final Logger log = LoggerFactory.getLogger(MboxHelper.class);
private MboxHelper() {
}
public static String getMboxName(Resource rsrc) {
String mboxName = "";
ValueMap resourceConfig = (ValueMap)(rsrc = MboxHelper.getStartResource(rsrc)).adaptTo(ValueMap.class);
if (resourceConfig != null) {
mboxName = (String)resourceConfig.get("jcr:title", (Object)"");
if ("".equals(mboxName)) {
mboxName = MboxHelper.getMboxId(rsrc);
}
mboxName = mboxName.replaceAll("\\s", "");
}
return mboxName;
}
public static /* varargs */ String generateLocationName(Resource currentResource, WCMMode wcmMode, String ... ambitMappings) {
String mboxName;
String mboxBaseName = MboxHelper.getMboxName(currentResource);
if (ambitMappings != null && ambitMappings.length > 0) {
TreeSet<String> mappings = new TreeSet<String>();
for (String mapping : ambitMappings) {
if (mapping.endsWith("master")) continue;
mappings.add(mapping);
}
String hashedInfo = mappings.size() > 0 ? "-" + MboxHelper.hashData(StringUtils.join(mappings, (String)"#")) : "";
mboxName = MboxHelper.qualifyMboxNameOrId(mboxBaseName + hashedInfo, wcmMode);
} else {
mboxName = MboxHelper.qualifyMboxNameOrId(mboxBaseName, wcmMode);
}
return mboxName;
}
public static String getMboxId(Resource rsrc) {
String mboxId = "";
if ((rsrc = MboxHelper.getStartResource(rsrc)) != null) {
ValueMap vm = (ValueMap)rsrc.adaptTo(ValueMap.class);
String loc = (String)vm.get("location", (Object)rsrc.getPath());
mboxId = MboxHelper.getMboxId(loc);
}
return mboxId;
}
public static String qualifyMboxNameOrId(String mboxNameOrId, WCMMode wcmMode) {
if (wcmMode == WCMMode.DISABLED) {
return mboxNameOrId;
}
return mboxNameOrId + "--author";
}
public static String qualifyMboxNameOrId(String mboxNameOrId, WCMMode wcmMode, String ambitName) {
if (StringUtils.isEmpty((String)ambitName) || "master".equals(ambitName)) {
return MboxHelper.qualifyMboxNameOrId(mboxNameOrId, wcmMode);
}
return MboxHelper.qualifyMboxNameOrId(StringUtils.join((Object[])new String[]{mboxNameOrId, ambitName}, (String)"-"), wcmMode);
}
public static String getMboxId(String location) {
if (location.startsWith("/")) {
location = location.substring(1);
}
return location.replaceAll("/", "-").replaceAll("-jcr:content", "");
}
public static Resource searchStartElement(Resource resource) {
Resource current;
if (ResourceUtil.getName((Resource)resource).equals("jcr:content")) {
return null;
}
if (resource.getPath().lastIndexOf("/") == 0) {
return null;
}
if (ResourceUtil.isA((Resource)resource, (String)"cq/personalization/components/mbox/start")) {
return resource;
}
Resource parent = ResourceUtil.getParent((Resource)resource);
ArrayList<Resource> predecessor = new ArrayList<Resource>();
Iterator i = ResourceUtil.listChildren((Resource)parent);
while (i.hasNext() && !(current = (Resource)i.next()).getPath().equals(resource.getPath())) {
predecessor.add(current);
}
Collections.reverse(predecessor);
for (Resource current2 : predecessor) {
if (ResourceUtil.isA((Resource)current2, (String)"cq/personalization/components/mbox/start")) {
return current2;
}
if (!ResourceUtil.isA((Resource)current2, (String)"cq/personalization/components/mbox/end")) continue;
return null;
}
return MboxHelper.searchStartElement(parent);
}
private static Resource getStartResource(Resource rsrc) {
Resource startElement;
if (ResourceUtil.isA((Resource)rsrc, (String)"cq/personalization/components/mbox/end") && (startElement = MboxHelper.searchStartElement(rsrc)) != null) {
return startElement;
}
return rsrc;
}
public static String getCustomMboxJsPath(Resource resource, Page currentPage, ConfigurationManager cfgMgr) throws RepositoryException {
boolean isValidConfig;
Configuration configuration = null;
HierarchyNodeInheritanceValueMap mboxProperties = new HierarchyNodeInheritanceValueMap(resource);
String[] services = (String[])mboxProperties.getInherited("cq:cloudserviceconfigs", (Object)new String[0]);
if (services.length == 0) {
mboxProperties = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
services = (String[])mboxProperties.getInherited("cq:cloudserviceconfigs", (Object)new String[0]);
}
if (cfgMgr != null) {
configuration = cfgMgr.getConfiguration("testandtarget", services);
}
boolean bl = isValidConfig = configuration != null && configuration.getInherited("clientcode", (Object)null) != null;
if (isValidConfig) {
Node configNode = null;
if (configuration != null) {
configNode = (Node)configuration.getResource().adaptTo(Node.class);
}
if (configNode.hasNode("./jcr:content/public/mbox.js")) {
Node scriptNode = configNode.getNode("./jcr:content/public/mbox.js");
return scriptNode.getPath();
}
}
return null;
}
public static boolean isAccurateRendering(Resource resource) throws RepositoryException {
return (Boolean)((ValueMap)resource.adaptTo(ValueMap.class)).get("accurateTargeting", (Object)false);
}
public static List<String> getClientContextParameterNames(Resource resource, InheritanceValueMap pageProperties, ConfigurationManager configurationManager) throws RepositoryException {
return new ArrayList<String>(MboxHelper.getMappedClientContextParameterNames(resource, pageProperties, configurationManager).values());
}
public static Map<String, String> getMappedClientContextParameterNames(Resource resource, InheritanceValueMap pageProperties, ConfigurationManager configurationManager) throws RepositoryException {
LinkedHashMap<String, String> mappedProperties = new LinkedHashMap<String, String>();
List<Object> directMappings = Arrays.asList((Object[])((ValueMap)resource.adaptTo(ValueMap.class)).get("cq:mappings", (Object)new String[0]));
for (String directMapping : directMappings) {
mappedProperties.put(directMapping, directMapping.replace('/', '.'));
}
Framework framework = MboxHelper.getFramework(pageProperties, configurationManager);
if (framework == null) {
return mappedProperties;
}
for (String scVar : framework.scVars()) {
mappedProperties.put(framework.getMapping(scVar), scVar);
}
return mappedProperties;
}
public static Map<String, String> getStaticParameters(Resource resource) {
String[] staticParameters = (String[])((ValueMap)resource.adaptTo(ValueMap.class)).get("staticParams", (Object)new String[0]);
HashMap<String, String> toReturn = new HashMap<String, String>();
try {
for (String keyAndValue : staticParameters) {
JSONArray array = new JSONArray(keyAndValue);
toReturn.put(array.getString(0), array.getString(1));
}
}
catch (JSONException e) {
log.error(e.getMessage(), (Throwable)e);
}
return toReturn;
}
private static Framework getFramework(InheritanceValueMap pageProperties, ConfigurationManager configurationManager) {
String[] services = (String[])pageProperties.getInherited("cq:cloudserviceconfigs", (Object)new String[0]);
log.debug("Found has {} service configs", (Object)services.length);
if (services.length == 0) {
return null;
}
Configuration configuration = configurationManager.getConfiguration("testandtarget", services);
if (configuration == null) {
return null;
}
log.debug("Resource configured to use configuration at {}", (Object)configuration.getPath());
return (Framework)configuration.getContentResource().adaptTo(Framework.class);
}
private static String hashData(String data) {
MessageDigest md = null;
String mappings = "";
try {
md = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e) {
return data;
}
md.update(data.getBytes());
byte[] hashedBytes = md.digest();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashedBytes.length; ++i) {
sb.append(Integer.toString((hashedBytes[i] & 255) + 256, 16).substring(1));
}
return sb.toString();
}
}