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/15 11:39:04 UTC

[sling-org-apache-sling-scripting-thymeleaf] 03/03: SLING-8709 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-thymeleaf.git

commit 19537f7ec370758e20d778c5dbb37f49df9198ea
Author: Oliver Lietz <ol...@apache.org>
AuthorDate: Sun Sep 15 13:38:24 2019 +0200

    SLING-8709 Make value map of current resource available in bindings as properties
---
 .../internal/ThymeleafBindingsValuesProvider.java  | 47 +++++++++++++++++++
 .../html/ThymeleafBindingsValuesProviderIT.java    | 53 ++++++++++++++++++++++
 .../apps/thymeleaf/page/bindings/html.html         | 27 +++++++++++
 src/test/resources/content/thymeleaf.json          |  6 +++
 4 files changed, 133 insertions(+)

diff --git a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ThymeleafBindingsValuesProvider.java b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ThymeleafBindingsValuesProvider.java
new file mode 100644
index 0000000..7d88204
--- /dev/null
+++ b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ThymeleafBindingsValuesProvider.java
@@ -0,0 +1,47 @@
+/*
+ * 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.thymeleaf.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=thymeleaf"
+    }
+)
+public class ThymeleafBindingsValuesProvider 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/thymeleaf/it/tests/html/ThymeleafBindingsValuesProviderIT.java b/src/test/java/org/apache/sling/scripting/thymeleaf/it/tests/html/ThymeleafBindingsValuesProviderIT.java
new file mode 100644
index 0000000..a800db9
--- /dev/null
+++ b/src/test/java/org/apache/sling/scripting/thymeleaf/it/tests/html/ThymeleafBindingsValuesProviderIT.java
@@ -0,0 +1,53 @@
+/*
+ * 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.thymeleaf.it.tests.html;
+
+import java.io.IOException;
+
+import org.apache.sling.scripting.thymeleaf.it.tests.ThymeleafTestSupport;
+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.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class ThymeleafBindingsValuesProviderIT extends ThymeleafTestSupport {
+
+    private Document document;
+
+    @Before
+    public void setup() throws IOException {
+        final String url = String.format("http://localhost:%s/thymeleaf/bindings.html", httpPort());
+        document = Jsoup.connect(url).get();
+    }
+
+    @Test
+    public void testTitle() {
+        assertThat(document.title(), is("Thymeleaf Bindings"));
+    }
+
+}
diff --git a/src/test/resources/apps/thymeleaf/page/bindings/html.html b/src/test/resources/apps/thymeleaf/page/bindings/html.html
new file mode 100644
index 0000000..1bf4a7f
--- /dev/null
+++ b/src/test/resources/apps/thymeleaf/page/bindings/html.html
@@ -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="de">
+<head>
+  <meta charset="UTF-8"/>
+  <title data-th-text="${properties.title}">Thymeleaf</title>
+</head>
+<body>
+</body>
+</html>
diff --git a/src/test/resources/content/thymeleaf.json b/src/test/resources/content/thymeleaf.json
index b79a222..61247ca 100644
--- a/src/test/resources/content/thymeleaf.json
+++ b/src/test/resources/content/thymeleaf.json
@@ -9,6 +9,12 @@
         "sling:resourceSuperType": "thymeleaf/page",
         "title": "Sling Models adaptTo()"
     },
+    "bindings": {
+        "jcr:primaryType": "nt:unstructured",
+        "sling:resourceType": "thymeleaf/page/bindings",
+        "sling:resourceSuperType": "thymeleaf/page",
+        "title": "Thymeleaf Bindings"
+    },
     "decoupled": {
         "jcr:primaryType": "nt:unstructured",
         "sling:resourceType": "thymeleaf/page/decoupled",