[java-idp-plugin-thymeleaf] 02/02: Commit extended version of Thymeleaf view resolver.

Scott Cantor cantor.2 at osu.edu
Tue Feb 6 18:23:43 UTC 2024


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository java-idp-plugin-thymeleaf.

View the commit online:
http://git.shibboleth.net/view/?p=java-idp-plugin-thymeleaf.git;a=commit;h=17b7b77dbf2f8e707e5910ae6b65f14afa2dfe74

commit 17b7b77dbf2f8e707e5910ae6b65f14afa2dfe74
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Feb 6 13:23:39 2024 -0500

    Commit extended version of Thymeleaf view resolver.
---
 thymeleaf-impl/pom.xml                             | 19 +++++-
 .../spring/impl/ChainingThymeleafViewResolver.java | 79 ++++++++++++++++++++++
 2 files changed, 97 insertions(+), 1 deletion(-)

diff --git a/thymeleaf-impl/pom.xml b/thymeleaf-impl/pom.xml
index 964b174..ec533a8 100644
--- a/thymeleaf-impl/pom.xml
+++ b/thymeleaf-impl/pom.xml
@@ -45,7 +45,24 @@
             <artifactId>shib-profile-api</artifactId>
             <scope>provided</scope>
         </dependency>
-        
+
+        <dependency>
+            <groupId>${shib-shared.groupId}</groupId>
+            <artifactId>shib-support</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>${spring.groupId}</groupId>
+            <artifactId>spring-webmvc</artifactId>
+            <scope>provided</scope>
+        </dependency>        
+
+        <dependency>
+            <groupId>jakarta.servlet</groupId>
+            <artifactId>jakarta.servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
         <!-- Test dependencies -->
     </dependencies>
 
diff --git a/thymeleaf-impl/src/main/java/net/shibboleth/idp/plugin/thymeleaf/spring/impl/ChainingThymeleafViewResolver.java b/thymeleaf-impl/src/main/java/net/shibboleth/idp/plugin/thymeleaf/spring/impl/ChainingThymeleafViewResolver.java
new file mode 100644
index 0000000..8783e71
--- /dev/null
+++ b/thymeleaf-impl/src/main/java/net/shibboleth/idp/plugin/thymeleaf/spring/impl/ChainingThymeleafViewResolver.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed 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 net.shibboleth.idp.plugin.thymeleaf.spring.impl;
+
+import java.util.Collections;
+import java.util.Locale;
+import java.util.function.Predicate;
+import javax.annotation.Nonnull;
+
+import net.shibboleth.shared.annotation.ParameterName;
+import net.shibboleth.shared.logic.Constraint;
+
+import org.springframework.web.servlet.View;
+import org.thymeleaf.IEngineConfiguration;
+import org.thymeleaf.spring6.view.ThymeleafViewResolver;
+import org.thymeleaf.templateresolver.ITemplateResolver;
+
+/**
+ * View resolver for Thymeleaf templates that support chaining to the next view resolver in the chain if the
+ * template does not exists. This component is necessary for progressive adoption of Thymeleaf for IdP views.
+ *
+ * @author Marvin S. Addison
+ */
+public class ChainingThymeleafViewResolver extends ThymeleafViewResolver {
+    
+    /** Activation condition for view resolver. */
+    @Nonnull private final Predicate<String> activationCondition;
+    
+    /**
+     * Constructor.
+     * 
+     * @param condition activation condition
+     * 
+     * @since 9.1.0
+     */
+    public ChainingThymeleafViewResolver(@Nonnull @ParameterName(name="condition") final Predicate<String> condition) {
+        activationCondition = Constraint.isNotNull(condition, "Activation condition cannot be null");
+    }
+    
+    /** {@inheritDoc} */
+    @Override
+    protected View loadView(final String viewName, final Locale locale) throws Exception {
+        if (activationCondition.test(viewName)) {
+            return checkResource(viewName) ? super.loadView(viewName, locale) : null;
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Check for the underlying view resource.
+     * 
+     * @param viewName name of view
+     * 
+     * @return true iff the view resource exists
+     */
+    private boolean checkResource(final String viewName) {
+        final IEngineConfiguration config = getTemplateEngine().getConfiguration();
+        for (final ITemplateResolver resolver : config.getTemplateResolvers()) {
+            if (resolver.resolveTemplate(config, null, viewName, Collections.emptyMap()) != null) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list