LCFormsOptionServiceImpl.java
9.04 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.xss.XSSAPI
* javax.jcr.Node
* javax.jcr.Property
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.Resource
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.forms.service.impl;
import com.adobe.forms.admin.LCFormsAdminService;
import com.adobe.forms.option.LCFormsOptions;
import com.adobe.forms.service.LCFormsOptionService;
import com.adobe.granite.xss.XSSAPI;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="LC Forms Option Service", description="LC Forms Option Service")
@Service(value={LCFormsOptionService.class})
public class LCFormsOptionServiceImpl
implements LCFormsOptionService {
private static Logger logger = LoggerFactory.getLogger(LCFormsOptionServiceImpl.class);
private static Set<String> SECURE_PROPERTIES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList("skipTrustedCheck", "template", "templateBytes", "contentRoot")));
@Reference
private LCFormsAdminService lcFormsAdminService;
public static LCFormsOptions create(SlingHttpServletRequest slingRequest) {
LCFormsOptions options = new LCFormsOptions();
for (String opts : LCFormsOptions.getSupported()) {
try {
String val = LCFormsOptionServiceImpl.getAttributeOrRequestParam(slingRequest, opts);
LCFormsOptionServiceImpl.processOption(val, opts, options, slingRequest);
}
catch (Exception e) {
logger.debug("Error setting " + opts + " in LCFormsOptions: ", (Throwable)e);
}
}
return options;
}
@Override
public LCFormsOptions createFromRequest(SlingHttpServletRequest slingRequest) {
LCFormsOptions options = new LCFormsOptions();
for (String opts : LCFormsOptions.getSupported()) {
try {
String val = LCFormsOptionServiceImpl.getAttributeOrRequestParam(slingRequest, opts, this.lcFormsAdminService);
LCFormsOptionServiceImpl.processOption(val, opts, options, slingRequest);
}
catch (Exception e) {
logger.debug("Error setting " + opts + " in LCFormsOptions: ", (Throwable)e);
}
}
return options;
}
private static void processOption(String val, String opts, LCFormsOptions options, SlingHttpServletRequest slingRequest) {
if (val != null) {
String setter = LCFormsOptionServiceImpl.formSetterName(opts);
LCFormsOptionServiceImpl.invokeSetter(options, setter, val);
}
}
private static void invokeSetter(LCFormsOptions options, String setter, String val) {
try {
options.getClass().getMethod(setter, String.class).invoke(options, val);
}
catch (Exception e) {
logger.error("Cannot invoke setter for setter" + setter, (Throwable)e);
}
}
private static String formSetterName(String option) {
return "set" + Character.toUpperCase(option.charAt(0)) + option.substring(1);
}
@Override
public LCFormsOptions create(Map<String, String> formOptions) {
LCFormsOptions options = new LCFormsOptions();
for (String opts : LCFormsOptions.getSupported()) {
try {
String val = formOptions.get(opts);
LCFormsOptionServiceImpl.processOption(val, opts, options, null);
}
catch (Exception e) {
logger.debug("Error setting " + opts + " in LCFormsOptions: ", (Throwable)e);
}
}
return options;
}
@Override
public JSONObject getBehaviourConfig(SlingHttpServletRequest slingRequest, XSSAPI xssAPI) {
String[] behaviourConstants = new String[]{"mfDataDependentFloatingField", "mfDisableHeadRequest", "mfStripInitialFormDom", "mfDisableLeanSubmit", "mfDisableCalendarIcon", "mfRangeTabIndex"};
List<String> boolVal = Arrays.asList("true", "false");
HashMap<String, Object> mpBehaviour = new HashMap<String, Object>();
for (String behaviourConstant : behaviourConstants) {
String behaviorConfigParam = this.get(slingRequest, behaviourConstant);
if (xssAPI != null) {
behaviorConfigParam = xssAPI.encodeForJSString(behaviorConfigParam);
}
if (behaviorConfigParam == null) continue;
if (boolVal.contains(behaviorConfigParam)) {
mpBehaviour.put(behaviourConstant, Boolean.valueOf(behaviorConfigParam));
continue;
}
mpBehaviour.put(behaviourConstant, behaviorConfigParam);
}
return new JSONObject(mpBehaviour);
}
@Override
public JSONObject getPagingConfig(SlingHttpServletRequest slingRequest, XSSAPI xssAPI) {
String[] pagingConstants = new String[]{"mfPagingDisabled", "mfShrinkPageDisabled"};
HashMap<String, Boolean> mpPaging = new HashMap<String, Boolean>();
mpPaging.put("mfPagingDisabled", false);
mpPaging.put("mfShrinkPageDisabled", false);
for (int i = 0; i < pagingConstants.length; ++i) {
String pagingConfig = xssAPI != null ? xssAPI.encodeForJSString(this.get(slingRequest, pagingConstants[i])) : this.get(slingRequest, pagingConstants[i]);
if (pagingConfig == null) continue;
mpPaging.put(pagingConstants[i], Boolean.valueOf(pagingConfig));
}
return new JSONObject(mpPaging);
}
public static Map<String, Object> getAttributeOrRequestParamAsMap(SlingHttpServletRequest slingRequest, String param) {
Map value = null;
Object valObj = slingRequest.getAttribute(param);
if (valObj != null && valObj instanceof Map) {
value = (Map)valObj;
}
return value;
}
@Deprecated
public static String getAttributeOrRequestParam(SlingHttpServletRequest slingRequest, String param) {
return LCFormsOptionServiceImpl.getAttributeOrRequestParam(slingRequest, param, null);
}
public static String getAttributeOrRequestParam(SlingHttpServletRequest slingRequest, String param, LCFormsAdminService lcFormsAdminService) {
boolean isSkipTrustedCheck;
RequestParameter reqParam;
Node node;
Resource resource;
if ("debugDir".equals(param) && (lcFormsAdminService == null || !lcFormsAdminService.isAllowDebugParameters())) {
return null;
}
String value = null;
Object valObj = slingRequest.getAttribute(param);
if (valObj != null && valObj instanceof String) {
value = (String)valObj;
}
if ((isSkipTrustedCheck = "true".equals(slingRequest.getAttribute("skipTrustedCheck"))) && SECURE_PROPERTIES.contains(param)) {
return value;
}
if (value == null && (resource = slingRequest.getResource()) != null && (node = (Node)resource.adaptTo(Node.class)) != null) {
try {
Property property;
if (node.hasProperty(param) && (property = node.getProperty(param)).getType() == 1) {
value = property.getString();
}
}
catch (Exception e) {
logger.debug("Exception getting value of " + param, (Throwable)e);
}
}
if (value == null && (reqParam = slingRequest.getRequestParameter(param)) != null) {
value = reqParam.getString();
}
return value;
}
@Override
public String get(SlingHttpServletRequest slingRequest, String option) {
return LCFormsOptionServiceImpl.getAttributeOrRequestParam(slingRequest, option, this.lcFormsAdminService);
}
@Override
public void set(SlingHttpServletRequest slingRequest, String option, String value) {
slingRequest.setAttribute(option, (Object)value);
}
protected void bindLcFormsAdminService(LCFormsAdminService lCFormsAdminService) {
this.lcFormsAdminService = lCFormsAdminService;
}
protected void unbindLcFormsAdminService(LCFormsAdminService lCFormsAdminService) {
if (this.lcFormsAdminService == lCFormsAdminService) {
this.lcFormsAdminService = null;
}
}
}