WCMScriptHelper.java
10.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* aQute.bnd.annotation.ProviderType
* com.day.cq.wcm.api.WCMMode
* com.day.cq.wcm.api.components.ComponentContext
* com.day.cq.wcm.api.components.IncludeOptions
* com.day.cq.wcm.commons.WCMUtils
* javax.servlet.RequestDispatcher
* javax.servlet.Servlet
* javax.servlet.ServletRequest
* javax.servlet.ServletResponse
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestDispatcherOptions
* org.apache.sling.api.resource.NonExistingResource
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.SyntheticResource
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.scripting.SlingScriptHelper
* org.apache.sling.api.servlets.ServletResolver
* org.apache.sling.api.wrappers.ValueMapDecorator
* org.apache.sling.scripting.sightly.SightlyException
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.sightly;
import aQute.bnd.annotation.ProviderType;
import com.adobe.cq.sightly.SightlyWCMMode;
import com.adobe.cq.sightly.WCMResourceOptions;
import com.adobe.cq.sightly.internal.PrintWriterResponseWrapper;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.api.components.IncludeOptions;
import com.day.cq.wcm.commons.WCMUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestDispatcherOptions;
import org.apache.sling.api.resource.*;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.api.servlets.ServletResolver;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.scripting.sightly.SightlyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ProviderType
public class WCMScriptHelper {
private final Logger log = LoggerFactory.getLogger(WCMScriptHelper.class);
private SlingScriptHelper slingHelper;
private SlingHttpServletRequest request;
private SlingHttpServletResponse response;
private SightlyWCMMode mode;
public WCMScriptHelper(SlingScriptHelper sling) {
this.slingHelper = sling;
this.request = sling.getRequest();
this.response = sling.getResponse();
this.mode = new SightlyWCMMode(this.request);
}
public void includeResource(PrintWriter out, String script, String dispatcherOptions, String resourceType, WCMResourceOptions wcmResourceOptions) {
PrintWriterResponseWrapper customResponse = new PrintWriterResponseWrapper(out, this.response);
this.includeResource((SlingHttpServletResponse)customResponse, script, dispatcherOptions, resourceType, wcmResourceOptions);
}
public void includeResource(SlingHttpServletResponse customResponse, String script, String dispatcherOptions, String resourceType, WCMResourceOptions wcmResourceOptions) {
this.includeResource(customResponse, script, dispatcherOptions, resourceType, wcmResourceOptions, Collections.EMPTY_MAP);
}
public void includeResource(SlingHttpServletResponse response, String script, String dispatcherOptions, String resourceType, WCMResourceOptions wcmResourceOptions, Map<String, Object> resourceProperties) {
if (StringUtils.isEmpty((CharSequence)script)) {
this.log.error("Script path cannot be empty.");
} else {
script = this.normalizePath(script);
WCMMode currentMode = (WCMMode)this.request.getAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME);
try {
RequestDispatcher dispatcher;
WCMMode mode = wcmResourceOptions.getWCMMode();
if (mode != null) {
this.request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, (Object)mode);
}
IncludeOptions includeOptions = IncludeOptions.getOptions((ServletRequest)this.request, (boolean)true);
String decorationTag = wcmResourceOptions.getDecorationTagName();
if (decorationTag == null) {
ComponentContext componentContext = WCMUtils.getComponentContext((ServletRequest)this.request);
decorationTag = componentContext.getDecorationTagName();
}
includeOptions.setDecorationTagName(decorationTag);
String cssClassName = wcmResourceOptions.getCssClassName();
if (StringUtils.isNotEmpty((CharSequence)cssClassName)) {
includeOptions.getCssClassNames().addAll(this.expandClassName(cssClassName));
}
RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions(dispatcherOptions);
if (StringUtils.isNotEmpty((CharSequence)resourceType)) {
requestDispatcherOptions.setForceResourceType(resourceType);
}
if (resourceProperties != null && resourceProperties.size() > 0) {
SyntheticResourceWithProperties includedResource = new SyntheticResourceWithProperties(this.request.getResourceResolver(), script, resourceType, resourceProperties);
dispatcher = this.request.getRequestDispatcher((Resource)includedResource, requestDispatcherOptions);
} else {
Resource includedResource = this.request.getResourceResolver().resolve(script);
if ((includedResource instanceof NonExistingResource || includedResource.isResourceType("sling:nonexisting")) && resourceType != null) {
includedResource = new SyntheticResource(this.request.getResourceResolver(), script, resourceType);
dispatcher = this.request.getRequestDispatcher(includedResource, requestDispatcherOptions);
requestDispatcherOptions.remove((Object)"forceResourceType");
} else {
dispatcher = this.request.getRequestDispatcher(script, requestDispatcherOptions);
}
}
dispatcher.include((ServletRequest)this.request, (ServletResponse)response);
}
catch (Exception e) {
if (e instanceof SightlyException) {
throw (SightlyException)e;
}
throw new SightlyException((Throwable)e);
}
finally {
if (currentMode == null) {
this.request.removeAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME);
} else {
this.request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, (Object)currentMode);
}
}
}
}
public void includeScript(String script, String wcmMode, PrintWriter out) {
if (StringUtils.isEmpty((CharSequence)script)) {
this.log.error("Script path cannot be empty.");
} else {
ServletResolver servletResolver;
WCMMode mode;
WCMMode currentMode = (WCMMode)this.request.getAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME);
if (StringUtils.isNotEmpty((CharSequence)wcmMode) && (mode = WCMMode.valueOf((String)wcmMode.toUpperCase())) != null) {
this.request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, (Object)mode);
}
if ((servletResolver = (ServletResolver)this.slingHelper.getService(ServletResolver.class)) != null) {
Servlet servlet = servletResolver.resolveServlet(this.request.getResource(), script);
if (servlet != null) {
try {
PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, this.response);
servlet.service((ServletRequest)this.request, (ServletResponse)resWrapper);
}
catch (Exception e) {
if (e instanceof SightlyException) {
throw (SightlyException)e;
}
throw new SightlyException((Throwable)e);
}
} else {
this.log.error("Failed to locate script {}.", (Object)script);
}
} else {
this.log.error("Sling ServletResolver service is unavailable, failed to include {}.", (Object)script);
}
if (currentMode == null) {
this.request.removeAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME);
} else {
this.request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, (Object)currentMode);
}
}
}
public SightlyWCMMode getMode() {
return this.mode;
}
private String normalizePath(String path) {
if (!path.startsWith("/")) {
path = this.request.getResource().getPath() + "/" + path;
}
return ResourceUtil.normalize((String)path);
}
private List<String> expandClassName(String classesString) {
String[] classesArray = classesString.split("\\s");
ArrayList<String> classes = new ArrayList<String>();
for (int i = 0; i < classesArray.length; ++i) {
String c = classesArray[i].trim();
if (!StringUtils.isNotEmpty((CharSequence)c)) continue;
classes.add(c);
}
return classes;
}
private class SyntheticResourceWithProperties
extends SyntheticResource {
private Map<String, Object> properties;
SyntheticResourceWithProperties(ResourceResolver resourceResolver, String path, String resourceType, Map<String, Object> properties) {
super(resourceResolver, path, resourceType);
this.properties = properties;
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type == ValueMap.class) {
return (AdapterType)new ValueMapDecorator(this.properties);
}
return (AdapterType)super.adaptTo(type);
}
}
}