Author: radu
Date: Thu Jun 30 10:14:29 2016
New Revision: 1750765
URL: http://svn.apache.org/viewvc?rev=1750765&view=rev
Log:
SLING-5811 - Properly handle actual Resources in Sightly data-sly-resource
* Added handling for actual Resource objects in data-sly-resource
* Added tests
(applied slightly modified patch from Vlad Băilescu; closes #150)
Added:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.html
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.js
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.html
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.js
Modified:
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/ResourceRuntimeExtension.java
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json
sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
Modified:
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/ResourceRuntimeExtension.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/ResourceRuntimeExtension.java?rev=1750765&r1=1750764&r2=1750765&view=diff
==============================================================================
---
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/ResourceRuntimeExtension.java
(original)
+++
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/ResourceRuntimeExtension.java
Thu Jun 30 10:14:29 2016
@@ -18,7 +18,6 @@
******************************************************************************/
package org.apache.sling.scripting.sightly.impl.engine.extension;
-import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
@@ -28,7 +27,6 @@ import java.util.Map;
import java.util.Set;
import javax.script.Bindings;
import javax.servlet.RequestDispatcher;
-import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
@@ -38,7 +36,6 @@ import org.apache.felix.scr.annotations.
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.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.SyntheticResource;
@@ -47,8 +44,6 @@ import org.apache.sling.scripting.sightl
import org.apache.sling.scripting.sightly.extension.RuntimeExtension;
import org.apache.sling.scripting.sightly.impl.utils.BindingsUtils;
import org.apache.sling.scripting.sightly.render.RenderContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* Runtime support for including resources in a Sightly script through {@code
data-sly-resource}.
@@ -60,7 +55,6 @@ import org.slf4j.LoggerFactory;
)
public class ResourceRuntimeExtension implements RuntimeExtension {
- private static final Logger LOG =
LoggerFactory.getLogger(ResourceRuntimeExtension.class);
private static final String OPTION_RESOURCE_TYPE = "resourceType";
private static final String OPTION_PATH = "path";
private static final String OPTION_PREPEND_PATH = "prependPath";
@@ -78,16 +72,24 @@ public class ResourceRuntimeExtension im
private String provideResource(final RenderContext renderContext, Object
pathObj, Map<String, Object> options) {
Map<String, Object> opts = new HashMap<>(options);
- PathInfo pathInfo = new PathInfo(coerceString(pathObj));
- String path = pathInfo.path;
final Bindings bindings = renderContext.getBindings();
- String finalPath = buildPath(path, opts,
BindingsUtils.getResource(bindings));
String resourceType = coerceString(getAndRemoveOption(opts,
OPTION_RESOURCE_TYPE));
- Map<String, String> dispatcherOptionsMap = handleSelectors(bindings,
pathInfo.selectors, opts);
- String dispatcherOptions =
createDispatcherOptions(dispatcherOptionsMap);
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
- includeResource(bindings, printWriter, finalPath, dispatcherOptions,
resourceType);
+
+ if (pathObj instanceof Resource) {
+ Resource includeRes = (Resource) pathObj;
+ Map<String, String> dispatcherOptionsMap =
handleSelectors(bindings, new LinkedHashSet<String>(), opts);
+ String dispatcherOptions =
createDispatcherOptions(dispatcherOptionsMap);
+ includeResource(bindings, printWriter, includeRes,
dispatcherOptions, resourceType);
+ } else {
+ PathInfo pathInfo = new PathInfo(coerceString(pathObj));
+ String finalPath = buildPath(pathInfo.path, opts,
BindingsUtils.getResource(bindings));
+ Map<String, String> dispatcherOptionsMap =
handleSelectors(bindings, pathInfo.selectors, opts);
+ String dispatcherOptions =
createDispatcherOptions(dispatcherOptionsMap);
+ includeResource(bindings, printWriter, finalPath,
dispatcherOptions, resourceType);
+ }
+
return writer.toString();
}
@@ -229,16 +231,24 @@ public class ResourceRuntimeExtension im
private void includeResource(final Bindings bindings, PrintWriter out,
String script, String dispatcherOptions, String resourceType) {
if (StringUtils.isEmpty(script)) {
- LOG.error("Script path cannot be empty");
+ throw new SightlyException("Resource path cannot be empty");
} else {
- SlingHttpServletResponse customResponse = new
PrintWriterResponseWrapper(out, BindingsUtils.getResponse(bindings));
SlingHttpServletRequest request =
BindingsUtils.getRequest(bindings);
script = normalizePath(request, script);
-
Resource includeRes =
request.getResourceResolver().resolve(script);
- if (includeRes instanceof NonExistingResource ||
includeRes.isResourceType(Resource.RESOURCE_TYPE_NON_EXISTING)) {
+ if (ResourceUtil.isNonExistingResource(includeRes)) {
includeRes = new
SyntheticResource(request.getResourceResolver(), script, resourceType);
}
+ includeResource(bindings, out, includeRes, dispatcherOptions,
resourceType);
+ }
+ }
+
+ private void includeResource(final Bindings bindings, PrintWriter out,
Resource includeRes, String dispatcherOptions, String resourceType) {
+ if (includeRes == null) {
+ throw new SightlyException("Resource cannot be null");
+ } else {
+ SlingHttpServletResponse customResponse = new
PrintWriterResponseWrapper(out, BindingsUtils.getResponse(bindings));
+ SlingHttpServletRequest request =
BindingsUtils.getRequest(bindings);
RequestDispatcherOptions opts = new
RequestDispatcherOptions(dispatcherOptions);
if (StringUtils.isNotEmpty(resourceType)) {
opts.setForceResourceType(resourceType);
@@ -246,10 +256,8 @@ public class ResourceRuntimeExtension im
RequestDispatcher dispatcher =
request.getRequestDispatcher(includeRes, opts);
try {
dispatcher.include(request, customResponse);
- } catch (ServletException e) {
- throw new SightlyException("Failed to include resource " +
script, e);
- } catch (IOException e) {
- throw new SightlyException("Failed to include resource " +
script, e);
+ } catch (Exception e) {
+ throw new SightlyException("Failed to include resource " +
includeRes.getPath(), e);
}
}
}
Added:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.html
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.html?rev=1750765&view=auto
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.html
(added)
+++
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.html
Thu Jun 30 10:14:29 2016
@@ -0,0 +1,23 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<sly data-sly-use.actualresource="actualresource.js">
+ <div id="hash">${actualresource.hash}</div>
+ <div id="actual" data-sly-resource="${actualresource.actual @
selectors='included'}"></div>
+ <div id="path" data-sly-resource="${actualresource.path @
selectors='included'}"></div>
+</sly>
\ No newline at end of file
Added:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.js
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.js?rev=1750765&view=auto
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.js
(added)
+++
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/actualresource.js
Thu Jun 30 10:14:29 2016
@@ -0,0 +1,25 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+
******************************************************************************/
+use(function () {
+ return {
+ hash: Packages.java.lang.System.identityHashCode(resource),
+ actual: resource,
+ path: resource.getPath()
+ };
+});
\ No newline at end of file
Added:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.html
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.html?rev=1750765&view=auto
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.html
(added)
+++
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.html
Thu Jun 30 10:14:29 2016
@@ -0,0 +1,19 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<sly data-sly-use.included="${'included.js'}">${included.hash}</sly>
\ No newline at end of file
Added:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.js
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.js?rev=1750765&view=auto
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.js
(added)
+++
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/actualresource/included.js
Thu Jun 30 10:14:29 2016
@@ -0,0 +1,23 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+
******************************************************************************/
+use(function () {
+ return {
+ hash: Packages.java.lang.System.identityHashCode(resource)
+ };
+});
\ No newline at end of file
Modified:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json?rev=1750765&r1=1750764&r2=1750765&view=diff
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json
(original)
+++
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json
Thu Jun 30 10:14:29 2016
@@ -9,6 +9,10 @@
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "/apps/sightly/scripts/resource"
},
+ "actualresource": {
+ "jcr:primaryType": "nt:unstructured",
+ "sling:resourceType": "/apps/sightly/scripts/actualresource"
+ },
"includedresource": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "/apps/sightly/scripts/includedresource"
Modified:
sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java?rev=1750765&r1=1750764&r2=1750765&view=diff
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
(original)
+++
sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
Thu Jun 30 10:14:29 2016
@@ -33,6 +33,7 @@ import io.sightly.tck.html.HTMLExtractor
import io.sightly.tck.http.Client;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
public class SlingSpecificsSightlyIT {
@@ -42,6 +43,7 @@ public class SlingSpecificsSightlyIT {
private static final String SLING_USE = "/sightly/use.html";
private static final String SLING_JAVA_USE_NPE =
"/sightly/use.javaerror.html";
private static final String SLING_RESOURCE = "/sightly/resource.html";
+ private static final String SLING_RESOURCE_ACTUAL =
"/sightly/actualresource.html";
private static final String SLING_TEMPLATE = "/sightly/template.html";
private static final String SLING_TEMPLATE_BAD_IDENTIFIER =
"/sightly/template.bad-id.html";
private static final String SLING_JS_USE = "/sightly/use.jsuse.html";
@@ -228,6 +230,17 @@ public class SlingSpecificsSightlyIT {
assertTrue(pageContent.contains("Compilation errors in
apps/sightly/scripts/crlf/RepoPojoWrongPkgCRLF.java"));
}
+ @Test
+ public void actualResource() {
+ String url = launchpadURL + SLING_RESOURCE_ACTUAL;
+ String pageContent = client.getStringContent(url, 200);
+ String hash = HTMLExtractor.innerHTML(url, pageContent, "#hash");
+ String actual = HTMLExtractor.innerHTML(url, pageContent, "#actual");
+ String path = HTMLExtractor.innerHTML(url, pageContent, "#path");
+ assertEquals(hash, actual);
+ assertNotEquals(hash, path);
+ }
+
private void uploadFile(String fileName, String serverFileName, String
url) throws IOException {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(launchpadURL + url);