GuideModuleContentProviderServlet.java
13.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.service.GuideModuleImporter
* com.adobe.aemds.guide.utils.GuideContainerThreadLocal
* com.adobe.aemds.guide.utils.GuideUtils
* com.adobe.aemds.guide.utils.XMLUtils
* com.adobe.forms.common.service.ContentType
* com.adobe.forms.common.service.DataOptions
* com.adobe.forms.common.service.FormDataProviderRegistry
* com.adobe.forms.common.service.PrefillData
* com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
* com.adobe.granite.xss.XSSAPI
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* 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.apache.sling.api.servlets.SlingAllMethodsServlet
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.adaddon.servlet;
import com.adobe.aemds.guide.service.GuideModuleImporter;
import com.adobe.aemds.guide.utils.GuideContainerThreadLocal;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.XMLUtils;
import com.adobe.forms.common.service.ContentType;
import com.adobe.forms.common.service.DataOptions;
import com.adobe.forms.common.service.FormDataProviderRegistry;
import com.adobe.forms.common.service.PrefillData;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import com.adobe.granite.xss.XSSAPI;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
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.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"fd/adaddon/components/guideAdModule", "fd/adaddon/components/guideAdModuleGroup"}), @Property(name="sling.servlet.methods", value={"GET"}), @Property(name="service.description", value={"Adaptive Form Document Fragment / Group content provider"}), @Property(name="sling.servlet.selectors", value={"ad.modulecontentprovider"})})
public class GuideModuleContentProviderServlet
extends SlingAllMethodsServlet {
private Logger logger = LoggerFactory.getLogger(GuideModuleContentProviderServlet.class);
@Reference
ResourceResolverHelper resolverHelper;
@Reference
FormDataProviderRegistry formDataProviderRegistry;
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
private GuideModuleImporter guideModuleImporter;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String originalGuideContainerPath = GuideContainerThreadLocal.getGuideContainerPath();
try {
String guideContainerPath = request.getParameter("guideContainerPath");
GuideContainerThreadLocal.setGuideContainerPath((String)guideContainerPath);
this.handleGet(request, response);
}
finally {
GuideContainerThreadLocal.setGuideContainerPath((String)originalGuideContainerPath);
}
}
protected void handleGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
Resource moduleResource = request.getResource();
String normalizedResourceType = GuideUtils.getNormalizedNodeType((String)moduleResource.getResourceType(), (String)moduleResource.getResourceSuperType());
String resultantHtml = "";
String resolvedHtml = "";
ValueMap moduleProperties = moduleResource.getValueMap();
if (moduleProperties != null && this.guideModuleImporter != null) {
Resource itemsResource;
String assetRef = (String)moduleProperties.get((Object)"assetRef");
Object layoutConfigProperty = moduleProperties.get((Object)"layoutConfig");
Object[] layoutConfigArray = null;
String layoutConfig = "";
if (layoutConfigProperty != null) {
if (layoutConfigProperty instanceof String[]) {
layoutConfigArray = (String[])layoutConfigProperty;
}
layoutConfig = layoutConfigArray != null ? Arrays.toString(layoutConfigArray) : "[" + (String)layoutConfigProperty + "]";
}
if (StringUtils.isNotEmpty((CharSequence)assetRef)) {
HashMap<String, Object> moduleParameter;
if ("fd/adaddon/components/guideAdModuleGroup".equals(normalizedResourceType)) {
moduleParameter = new HashMap<String, Object>();
String guideContainerPath = request.getParameter("guideContainerPath");
if (guideContainerPath != null) {
Resource guideContainerResource;
String letterRef = "";
if (StringUtils.isNotEmpty((CharSequence)guideContainerPath) && (guideContainerResource = moduleResource.getResourceResolver().getResource(guideContainerPath)) != null) {
ValueMap containerValueMap = guideContainerResource.getValueMap();
letterRef = (String)containerValueMap.get("letterRef", (Object)"");
}
moduleParameter.put("letterRef", letterRef);
}
ValueMap guideProperties = this.getGuideProperties(request);
moduleParameter.put("ddRef", this.getDDRef(guideProperties));
moduleParameter.put("letterRef", this.getLetterRef(guideProperties));
moduleParameter.put("AD_DATA_DOCUMENT", this.getDataDocument(request));
resolvedHtml = this.guideModuleImporter.getModuleGroupResolution(assetRef, moduleParameter);
} else if ("fd/adaddon/components/guideAdModule".equals(normalizedResourceType)) {
moduleParameter = new HashMap();
ValueMap guideProperties = this.getGuideProperties(request);
moduleParameter.put("ddRef", this.getDDRef(guideProperties));
moduleParameter.put("letterRef", this.getLetterRef(guideProperties));
moduleParameter.put("AD_DATA_DOCUMENT", this.getDataDocument(request));
resolvedHtml = this.guideModuleImporter.getModuleResolution(assetRef, moduleParameter);
}
} else if ("fd/adaddon/components/guideAdModuleGroup".equals(normalizedResourceType) && (itemsResource = moduleResource.getChild("items")) != null) {
ArrayList<String> moduleAssetRefs = new ArrayList<String>();
for (Resource itemResource : itemsResource.getChildren()) {
ValueMap itemValueMap = ResourceUtil.getValueMap((Resource)itemResource);
String itemAssetRef = (String)itemValueMap.get("assetRef", (Object)"");
if (StringUtils.isEmpty((CharSequence)itemAssetRef)) continue;
moduleAssetRefs.add(itemAssetRef);
}
HashMap<String, Object> moduleParameter = new HashMap<String, Object>();
moduleParameter.put("moduleList", moduleAssetRefs);
ValueMap guideProperties = this.getGuideProperties(request);
moduleParameter.put("ddRef", this.getDDRef(guideProperties));
moduleParameter.put("letterRef", this.getLetterRef(guideProperties));
moduleParameter.put("AD_DATA_DOCUMENT", this.getDataDocument(request));
resolvedHtml = this.guideModuleImporter.getModuleGroupResolution(null, moduleParameter);
}
XSSAPI xssapi = (XSSAPI)request.adaptTo(XSSAPI.class);
resultantHtml = "<div data-module-layout-config='" + GuideUtils.encodeForHtmlAttr((String)layoutConfig, (XSSAPI)xssapi) + "'>" + GuideUtils.filterHtml((String)resolvedHtml, (XSSAPI)xssapi) + "</div>";
}
response.getWriter().write(resultantHtml);
}
private Document getDataDocument(SlingHttpServletRequest httpServletRequest) throws ServletException {
String dataRef = httpServletRequest.getParameter("dataRef");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
XMLUtils.disableExternalEntities((DocumentBuilderFactory)dbf);
DocumentBuilder db = dbf.newDocumentBuilder();
if (dataRef != null) {
ResourceResolver rr = this.resolverHelper.getResourceResolver();
if (rr == null) {
throw new ServletException("Resource resolver is null");
}
String guideContainerPath = httpServletRequest.getParameter("guideContainerPath");
if (StringUtils.isBlank((CharSequence)guideContainerPath)) {
throw new ServletException("Could not find guideContainerNode Path");
}
Resource aemFormContainer = rr.getResource(guideContainerPath);
DataOptions options = new DataOptions();
options.setFormResource(aemFormContainer);
options.setDataRef(dataRef);
options.setContentType(ContentType.XML);
Map prefillServiceParamMap = (Map)httpServletRequest.getAttribute("paramMap");
if (prefillServiceParamMap != null) {
options.setExtras(prefillServiceParamMap);
}
PrefillData prefillData = this.formDataProviderRegistry.getDataFromService(options);
return db.parse(prefillData.getInputStream());
}
return db.parse(new ByteArrayInputStream("<?xml version=\"1.0\" ?><root/>".getBytes("UTF-8")));
}
catch (Exception e) {
this.logger.error("Error creating data dom", (Throwable)e);
throw new ServletException((Throwable)e);
}
}
private ValueMap getGuideProperties(SlingHttpServletRequest request) {
Resource guideContainerResource;
String guideContainerPath = request.getParameter("guideContainerPath");
if (guideContainerPath != null && !StringUtils.isEmpty((CharSequence)guideContainerPath) && (guideContainerResource = request.getResourceResolver().getResource(guideContainerPath)) != null) {
return guideContainerResource.getValueMap();
}
return null;
}
private String getDDRef(ValueMap valueMap) {
if (valueMap != null) {
return (String)valueMap.get((Object)"ddRef");
}
return null;
}
private String getLetterRef(ValueMap valueMap) {
if (valueMap != null) {
return (String)valueMap.get((Object)"letterRef");
}
return null;
}
protected void bindResolverHelper(ResourceResolverHelper resourceResolverHelper) {
this.resolverHelper = resourceResolverHelper;
}
protected void unbindResolverHelper(ResourceResolverHelper resourceResolverHelper) {
if (this.resolverHelper == resourceResolverHelper) {
this.resolverHelper = null;
}
}
protected void bindFormDataProviderRegistry(FormDataProviderRegistry formDataProviderRegistry) {
this.formDataProviderRegistry = formDataProviderRegistry;
}
protected void unbindFormDataProviderRegistry(FormDataProviderRegistry formDataProviderRegistry) {
if (this.formDataProviderRegistry == formDataProviderRegistry) {
this.formDataProviderRegistry = null;
}
}
protected void bindGuideModuleImporter(GuideModuleImporter guideModuleImporter) {
this.guideModuleImporter = guideModuleImporter;
}
protected void unbindGuideModuleImporter(GuideModuleImporter guideModuleImporter) {
if (this.guideModuleImporter == guideModuleImporter) {
this.guideModuleImporter = null;
}
}
}