ParameterMapper.java
8.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
179
180
181
182
183
184
185
186
187
188
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.foundation.forms.FormsHelper
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.campaign.servlets.util;
import com.day.cq.mcm.campaign.CampaignException;
import com.day.cq.mcm.campaign.servlets.util.EncryptedPKParameter;
import com.day.cq.mcm.campaign.servlets.util.Mapping;
import com.day.cq.wcm.foundation.forms.FormsHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
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.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ParameterMapper {
private static final String PRM_RECON_KEY = "key";
private static final String PRM_RECON_KEY_VALUE = "keyValue";
private static final String PRM_DATA = "data";
private final Logger log;
private final HashMap<Mapping, String> values;
public ParameterMapper() {
this.log = LoggerFactory.getLogger(this.getClass());
this.values = new HashMap();
}
public void parseParameters(SlingHttpServletRequest request, Resource form) {
String value;
Iterator formElements = FormsHelper.getFormElements((Resource)form);
ArrayList<Mapping> mappings = new ArrayList<Mapping>();
while (formElements.hasNext()) {
String name;
Resource fieldResource = (Resource)formElements.next();
ValueMap props = ResourceUtil.getValueMap((Resource)fieldResource);
boolean isEncryptedPK = ResourceUtil.isA((Resource)fieldResource, (String)"mcm/campaign/components/form/profile/encryptedPK");
if (isEncryptedPK) {
if (!props.containsKey((Object)"name")) continue;
name = (String)props.get("name", String.class);
mappings.add(new Mapping(name, (String)props.get("urlParameter", String.class)));
continue;
}
if (!props.containsKey((Object)"name") || !props.containsKey((Object)"acMapping")) continue;
name = (String)props.get("name", String.class);
String mapping = (String)props.get("acMapping", String.class);
boolean isReconciliationKey = "true".equals(props.get("acReconciliationKey", String.class));
Object[] arrobject = new String[3];
arrobject[0] = name;
arrobject[1] = mapping;
arrobject[2] = isReconciliationKey ? "reconcKey" : "";
this.log.debug("Mapping: {} {} {}", arrobject);
Mapping def = new Mapping(name, mapping, isReconciliationKey);
if (isReconciliationKey) {
def.setOriginalValue(request.getParameter(":original:" + name));
}
mappings.add(def);
}
RequestParameterMap prms = request.getRequestParameterMap();
Set keys = prms.keySet();
this.values.clear();
block1 : for (String key : keys) {
for (Mapping mapping : mappings) {
if (!mapping.getName().equals(key)) continue;
value = prms.getValue(key).getString();
this.log.debug("Value for mapping '{}': {}", (Object[])new String[]{mapping.getMapping(), value});
this.values.put(mapping, value);
continue block1;
}
}
for (Mapping mapping : mappings) {
if (this.values.containsKey(mapping)) continue;
String name = mapping.getName();
RequestParameter falsePrm = request.getRequestParameter(":false:" + name);
if (falsePrm == null) continue;
value = falsePrm.getString();
this.values.put(mapping, value);
this.log.debug("Enhancing mapping '{}' with value '{}'", (Object[])new String[]{mapping.getMapping(), value});
}
}
private String getRecipientData(Mapping mapping) {
String mappingId = mapping.getMapping();
int sepPos = mappingId.indexOf(46);
if (sepPos >= 0) {
mappingId = mappingId.substring(sepPos + 1);
}
return mappingId;
}
public EncryptedPKParameter getEncryptedPKParameter() {
for (Map.Entry<Mapping, String> entry : this.values.entrySet()) {
Mapping mapping = entry.getKey();
if (!mapping.isEncryptedPK()) continue;
return new EncryptedPKParameter(mapping.getMapping(), entry.getValue());
}
return null;
}
public void addEncryptedPK(Map<String, String> parameters) {
for (Map.Entry<Mapping, String> toProcess : this.values.entrySet()) {
Mapping mapping = toProcess.getKey();
if (!mapping.isEncryptedPK()) continue;
String value = toProcess.getValue();
this.log.debug("Encrypted PK: {}", (Object)value);
parameters.put("encryptedPK", value);
return;
}
}
public void addReconciliationKey(Map<String, String> parameters, boolean useOriginalValues) {
StringBuilder reconKey = new StringBuilder();
StringBuilder reconKeyValue = new StringBuilder();
for (Map.Entry<Mapping, String> toProcess : this.values.entrySet()) {
String keyValue;
Mapping mapping = toProcess.getKey();
if (!mapping.isReconciliationKey() || (keyValue = useOriginalValues ? mapping.getOriginalValue() : toProcess.getValue()) == null) continue;
if (reconKey.length() > 0) {
reconKey.append(",");
}
String dataId = this.getRecipientData(mapping);
reconKey.append(dataId);
if (reconKeyValue.length() > 0) {
reconKeyValue.append(":");
}
reconKeyValue.append(keyValue);
}
String key = reconKey.toString();
String keyValue = reconKeyValue.toString();
this.log.debug("Reconciliation key: {} = {}", (Object[])new String[]{key, keyValue});
parameters.put("key", key);
parameters.put("keyValue", keyValue);
}
public void addData(Map<String, String> parameters) throws CampaignException {
JSONObject json = new JSONObject();
try {
for (Map.Entry<Mapping, String> toProcess : this.values.entrySet()) {
Mapping mapping = toProcess.getKey();
if (mapping.isEncryptedPK()) continue;
String dataId = mapping.getMapping();
if (!dataId.startsWith("context.")) {
dataId = this.getRecipientData(mapping);
}
String[] hierarchy = dataId.split("\\.");
int levels = hierarchy.length;
JSONObject insertObj = json;
for (int h = 0; h < levels - 1; ++h) {
String key = hierarchy[h];
if (!insertObj.has(key)) {
insertObj.put(key, (Object)new JSONObject());
}
insertObj = insertObj.getJSONObject(key);
}
insertObj.put(hierarchy[levels - 1], (Object)toProcess.getValue());
}
}
catch (JSONException je) {
throw new CampaignException("Could not create JSON representation", (Throwable)je);
}
String jsonStr = json.toString();
this.log.debug("Data JSON: {}", (Object)jsonStr);
parameters.put("data", jsonStr);
}
}