[java-shib-shared] 01/01: Add Thmyleaf version of Template

Rod Widdowson rdw at steadingsoftware.com
Thu Jan 25 15:01:48 UTC 2024


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

rdw pushed a commit to branch dev/IDP-2226
in repository java-shib-shared.

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-shared.git;a=commit;h=6d6b9c1b1a30bd068cb0788c40d185537ad36c72

commit 6d6b9c1b1a30bd068cb0788c40d185537ad36c72
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Thu Jan 25 15:01:39 2024 +0000

    Add Thmyleaf version of Template
---
 shib-velocity/pom.xml                              |   5 +
 .../shared/velocity/ThymeleafTemplate.java         | 179 +++++++++++++++++++++
 2 files changed, 184 insertions(+)

diff --git a/shib-velocity/pom.xml b/shib-velocity/pom.xml
index ccab6a8e..efbeca9c 100644
--- a/shib-velocity/pom.xml
+++ b/shib-velocity/pom.xml
@@ -36,6 +36,11 @@
             <groupId>org.apache.velocity</groupId>
             <artifactId>velocity-engine-core</artifactId>
         </dependency>
+        
+        <dependency>
+            <groupId>org.thymeleaf</groupId>
+            <artifactId>thymeleaf</artifactId>
+        </dependency>
 
         <!-- Runtime Dependencies -->
 
diff --git a/shib-velocity/src/main/java/net/shibboleth/shared/velocity/ThymeleafTemplate.java b/shib-velocity/src/main/java/net/shibboleth/shared/velocity/ThymeleafTemplate.java
new file mode 100644
index 00000000..594b8448
--- /dev/null
+++ b/shib-velocity/src/main/java/net/shibboleth/shared/velocity/ThymeleafTemplate.java
@@ -0,0 +1,179 @@
+/*
+ * 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.shared.velocity;
+
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.util.Locale;
+import java.util.Objects;
+
+import javax.annotation.Nonnull;
+
+import org.apache.velocity.app.VelocityEngine;
+import org.thymeleaf.TemplateEngine;
+import org.thymeleaf.TemplateSpec;
+import org.thymeleaf.context.Context;
+import org.thymeleaf.templatemode.TemplateMode;
+
+import com.google.common.base.MoreObjects;
+
+import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.logic.Constraint;
+import net.shibboleth.shared.primitive.StringSupport;
+
+/**
+ * Version of {@link ThymeleafTemplate} but for thymeleaf WIP
+ */
+public final class ThymeleafTemplate {
+
+    /** The {@link Template} used when evaluating the template. */
+    @Nonnull private final TemplateEngine engine;
+
+    /** The name or value of the template to be evaluated. */
+    @Nonnull @NotEmpty private final TemplateSpec template;
+
+    /** {{@link #template} the template name or the text
+     */
+    private final boolean isTemplateText;
+
+    /** The character encoding of the template. */
+    @Nonnull @NotEmpty private final Locale templateLocale;
+
+    /**
+     * Constructor.
+     * 
+     * @param thymeleafEngine engine used to evaluate the template
+     * @param velocityTemplateName name of the template to be evaluated
+     * @param velocityTemplateName name of the template to be evaluated
+     * @param velocityTemplateEncoding encoding used by the template
+     */
+    private ThymeleafTemplate(@Nonnull final TemplateEngine thymeleafEngine,
+            @Nonnull final String thymeleafTemplate,
+            final boolean isThymeleafTemplateText,
+            @Nonnull final Locale thymeleafLocale) {
+        engine = Constraint.isNotNull(thymeleafEngine, "Thmeleaf engine can not be null");
+        final String text = Constraint.isNotNull(StringSupport.trimOrNull(thymeleafTemplate),
+                "Template name/text can not be null or empty");
+        template = new TemplateSpec(text, TemplateMode.TEXT);
+        isTemplateText = isThymeleafTemplateText;
+        templateLocale = Constraint.isNotNull(thymeleafLocale, "Locale can not be null");
+    }
+
+    /**
+     * 
+     */
+    @Nonnull public static ThymeleafTemplate fromTemplate(@Nonnull final TemplateEngine engine,
+            @Nonnull @NotEmpty final String template) {
+        return fromTemplate(engine, template, Locale.US);
+    }
+
+    @Nonnull public static ThymeleafTemplate fromTemplate(@Nonnull final TemplateEngine engine,
+            @Nonnull @NotEmpty final String template,
+            @Nonnull final Locale locale) {
+        Constraint.isNotNull(StringSupport.trimOrNull(template), "Template can not be null or empty");
+        Constraint.isNotNull(locale, "Locale set can not be null");
+
+        return new ThymeleafTemplate(engine, template, true, locale);
+    }
+
+    /**
+     * A convenience method that invoked {@link #fromTemplateName(VelocityEngine, String, Charset)} and assumes the
+     * named template is US ASCII encoded.
+     * 
+     * <p>
+     * See {@link #fromTemplateName(VelocityEngine, String, Charset)} for full details.
+     * </p>
+     * 
+     * @param engine engine that will be used to evaluate the template
+     * @param templateName the name, as known to the given engine, of a velocity template
+     * 
+     * @return an instance of this class that can be used to evaluate the named template using the given engine
+     */
+    @Nonnull public static ThymeleafTemplate fromTemplateName(@Nonnull final TemplateEngine engine,
+            @Nonnull @NotEmpty final String templateName) {
+        return fromTemplateName(engine, templateName, Locale.US);
+    }
+
+    @Nonnull public static ThymeleafTemplate fromTemplateName(@Nonnull final TemplateEngine engine,
+            @Nonnull @NotEmpty final String name, final Locale locale) {
+        final String trimmedName =
+                Constraint.isNotNull(StringSupport.trimOrNull(name), "Template name can not be null or empty");
+        Constraint.isNotNull(locale, "Template locale set can not be null");
+
+        Constraint.isNotNull(null, "Need to add code");
+        return new ThymeleafTemplate(engine, trimmedName, false, locale);
+    }
+
+    /**
+     * Gets the name of the template.
+     * 
+     * @return name of the template
+     *
+    @Nonnull public String getTemplateName() {
+        return templateName;
+    }
+
+    /**
+     * Evaluates the template using the given context and returns the result as a string.
+     * 
+     * @param templateContext current template context
+     * 
+     * @return the generated output of the template
+     */
+    @Nonnull public String merge(final Context templateContext) {
+        templateContext.setLocale(templateLocale);
+        final String result = engine.process(template, templateContext);
+        assert result != null;
+        return result;
+    }
+
+    /**
+     * Evaluates the template using the given context and sends the result to a Writer.
+     * 
+     * @param templateContext current template context
+     * @param output writer that will receive the template output
+     */
+    public void merge(final Context templateContext, final Writer output) {
+        engine.process(template, templateContext, output);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean equals(final Object obj) {
+        if (obj == null) {
+            return false;
+        }
+
+        if (obj == this) {
+            return true;
+        }
+
+        if (!(obj instanceof ThymeleafTemplate)) {
+            return false;
+        }
+
+        final ThymeleafTemplate otherTemplate = (ThymeleafTemplate) obj;
+        return otherTemplate.template.equals(template);
+    }
+
+    /** {@inheritDoc} */
+    @Override public int hashCode() {
+        return Objects.hash(engine, template);
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return MoreObjects.toStringHelper(this).add("templateName", template.getTemplate()).toString();
+    }
+}
\ 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