You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ol...@apache.org on 2019/09/22 09:13:47 UTC
[sling-org-apache-sling-scripting-freemarker] 02/04: SLING-8729
Make value map of current resource available in bindings as properties
This is an automated email from the ASF dual-hosted git repository.
olli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-freemarker.git
commit aba3a6083579e4fa2711350e8f7055998a9cb876
Author: Oliver Lietz <ol...@apache.org>
AuthorDate: Sun Sep 22 11:04:36 2019 +0200
SLING-8729 Make value map of current resource available in bindings as properties
---
.../internal/FreemarkerBindingsValuesProvider.java | 45 +++++++++++++
.../tests/FreemarkerBindingsValuesProviderIT.java | 73 ++++++++++++++++++++++
.../apps/freemarker/page/bindings/html.ftl | 27 ++++++++
src/test/resources/content/freemarker.json | 6 ++
4 files changed, 151 insertions(+)
diff --git a/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerBindingsValuesProvider.java b/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerBindingsValuesProvider.java
new file mode 100644
index 0000000..989abb8
--- /dev/null
+++ b/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerBindingsValuesProvider.java
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+package org.apache.sling.scripting.freemarker.internal;
+
+import javax.script.Bindings;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.scripting.api.BindingsValuesProvider;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+ property = {
+ "javax.script.name=freemarker"
+ }
+)
+public class FreemarkerBindingsValuesProvider implements BindingsValuesProvider {
+
+ private static final String PROPERTIES = "properties";
+
+ @Override
+ public void addBindings(final Bindings bindings) {
+ if (!bindings.containsKey(PROPERTIES)) {
+ final Resource resource = (Resource) bindings.get(SlingBindings.RESOURCE);
+ if (resource != null) {
+ bindings.put(PROPERTIES, resource.getValueMap());
+ }
+ }
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/scripting/freemarker/it/tests/FreemarkerBindingsValuesProviderIT.java b/src/test/java/org/apache/sling/scripting/freemarker/it/tests/FreemarkerBindingsValuesProviderIT.java
new file mode 100644
index 0000000..434fdfa
--- /dev/null
+++ b/src/test/java/org/apache/sling/scripting/freemarker/it/tests/FreemarkerBindingsValuesProviderIT.java
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ */
+package org.apache.sling.scripting.freemarker.it.tests;
+
+import java.io.IOException;
+
+import javax.inject.Inject;
+
+import org.apache.sling.resource.presence.ResourcePresence;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.exam.util.Filter;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.factoryConfiguration;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class FreemarkerBindingsValuesProviderIT extends FreemarkerTestSupport {
+
+ private Document document;
+
+ @Inject
+ @Filter(value = "(path=/apps/freemarker/page/bindings/html.ftl)")
+ private ResourcePresence resourcePresence;
+
+ @Configuration
+ public Option[] configuration() {
+ return new Option[]{
+ baseConfiguration(),
+ factoryConfiguration("org.apache.sling.resource.presence.internal.ResourcePresenter")
+ .put("path", "/apps/freemarker/page/bindings/html.ftl")
+ .asOption(),
+ };
+ }
+
+ @Before
+ public void setup() throws IOException {
+ final String url = String.format("http://localhost:%s/freemarker/bindings.html", httpPort());
+ document = Jsoup.connect(url).get();
+ }
+
+ @Test
+ public void testTitle() {
+ assertThat(document.title(), is("FreeMarker Bindings"));
+ }
+
+}
diff --git a/src/test/resources/apps/freemarker/page/bindings/html.ftl b/src/test/resources/apps/freemarker/page/bindings/html.ftl
new file mode 100644
index 0000000..f99a537
--- /dev/null
+++ b/src/test/resources/apps/freemarker/page/bindings/html.ftl
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<!--
+ 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.
+-->
+<html lang="en">
+<head>
+ <meta charset="UTF-8"/>
+ <title>${properties.title!''?html}</title>
+</head>
+<body>
+</body>
+</html>
diff --git a/src/test/resources/content/freemarker.json b/src/test/resources/content/freemarker.json
index ec418d9..ab27dd8 100644
--- a/src/test/resources/content/freemarker.json
+++ b/src/test/resources/content/freemarker.json
@@ -15,6 +15,12 @@
"sling:resourceSuperType": "freemarker/page",
"title": "Sling Models adaptTo()"
},
+ "bindings": {
+ "jcr:primaryType": "nt:unstructured",
+ "sling:resourceType": "freemarker/page/bindings",
+ "sling:resourceSuperType": "freemarker/page",
+ "title": "FreeMarker Bindings"
+ },
"include": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "freemarker/page/include",