[java-shib-attribute] branch dev/IDP-2226 updated: Use template attributes to control the inliner

Rod Widdowson rdw at steadingsoftware.com
Wed Jan 31 11:11:55 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-attribute.

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

The following commit(s) were added to refs/heads/dev/IDP-2226 by this push:
     new c6e9e99a3 Use template attributes to control the inliner
c6e9e99a3 is described below

commit c6e9e99a3446dbeb76805bff32a897e00a9451ef
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Wed Jan 31 11:11:20 2024 +0000

    Use template attributes to control the inliner
---
 .../resolver/dc/impl/ThymeLeafSandbox.java         | 60 +++++++++++++++++-----
 1 file changed, 47 insertions(+), 13 deletions(-)

diff --git a/shib-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/dc/impl/ThymeLeafSandbox.java b/shib-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/dc/impl/ThymeLeafSandbox.java
index 7e6d9e6ab..ee62458db 100644
--- a/shib-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/dc/impl/ThymeLeafSandbox.java
+++ b/shib-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/dc/impl/ThymeLeafSandbox.java
@@ -37,14 +37,13 @@ import org.thymeleaf.standard.processor.StandardInliningTextProcessor;
 import org.thymeleaf.templatemode.TemplateMode;
 import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
 import org.thymeleaf.templateresolver.StringTemplateResolver;
-import org.thymeleaf.templateresolver.WebApplicationTemplateResolver;
 
 import net.shibboleth.shared.collection.CollectionSupport;
-import net.shibboleth.shared.xml.ClasspathResolver;
 
 public class ThymeLeafSandbox {
 
     private final String templateStr = "Nibble a happy [[${warthog}]] says [[${thing.thing1}]] or [[${thing.thing2}]]";
+    private final String REP_ADDR =  "net.shibboleth.template.repStr";
 
 	@Test public void testEngine() {
 		final Map<String, Object> map = new HashMap<>(2);
@@ -67,14 +66,37 @@ public class ThymeLeafSandbox {
         var obj = new MyObject();
         map.put("thing", obj);
 
-        final Context ctx = new Context(null, map);
         final TemplateEngine engine = new TemplateEngine();
-        engine.setAdditionalDialects(CollectionSupport.singleton(new SandBoxDialect()));
+        engine.setAdditionalDialects(CollectionSupport.singleton(new SandBoxDialect())); 
         engine.setTemplateResolver(new StringTemplateResolver());
-        final TemplateSpec spec = new TemplateSpec(templateStr, TemplateMode.TEXT);
 
-        final String output = engine.process(spec, ctx);
+        //
+        // No replacement
+        //
+        TemplateSpec spec = new TemplateSpec(templateStr, TemplateMode.TEXT);
+        String output = engine.process(spec, new Context(null, map));
+        assertEquals(output, "Nibble a happy " + map.get("warthog") + " says " + obj.getThing1() + " or " +  obj.getThing2());
+
+        //
+        // No replacement - explicitly
+        //
+        spec = new TemplateSpec(templateStr, null, TemplateMode.TEXT, CollectionSupport.singletonMap(REP_ADDR, null));
+        output = engine.process(spec, new Context(null, map));
+        assertEquals(output, "Nibble a happy " + map.get("warthog") + " says " + obj.getThing1() + " or " +  obj.getThing2());
+
+        //
+        // Substring and insert nothing
+        //
+        spec = new TemplateSpec(templateStr, null, TemplateMode.TEXT, CollectionSupport.singletonMap(REP_ADDR, ""));
+        output = engine.process(spec, new Context(null, map));
         assertEquals(output, "Nibble a happy " + map.get("warthog") + " says " + obj.getThing1().substring(3) + " or " +  obj.getThing2().substring(3));
+
+        //
+        // Substring and insert "aardvark"
+        //
+        spec = new TemplateSpec(templateStr, null, TemplateMode.TEXT, CollectionSupport.singletonMap(REP_ADDR, "aadvark"));
+        output = engine.process(spec, new Context(null, map));
+        assertEquals(output, "Nibble a happy " + map.get("warthog") + " says aadvark" + obj.getThing1().substring(3) + " or aadvark" +  obj.getThing2().substring(3));
     }
 
 	private class MyObject {
@@ -99,7 +121,19 @@ public class ThymeLeafSandbox {
 	        switch (getTemplateMode()) {
 
             case TEXT:
-                structureHandler.setInliner(new MyInliner(context.getConfiguration()));
+                final Map<String, Object> attrs = context.getTemplateResolutionAttributes();
+                final String repStr;
+                if (attrs != null) {
+                    final Object rep = attrs.get(REP_ADDR);
+                    if ((rep != null) && (rep instanceof String rs)) {
+                        repStr = rs;
+                    } else {
+                        repStr = null;
+                    }
+                } else {
+                    repStr = null;
+                }
+                structureHandler.setInliner(new MyInliner(context.getConfiguration(), repStr));
                 break;
 
 	            default:
@@ -123,29 +157,29 @@ public class ThymeLeafSandbox {
 	
 	private class MyInliner extends AbstractStandardInliner {
 
-        protected MyInliner(IEngineConfiguration configuration) {
+	    final String replacer;
+
+        protected MyInliner(IEngineConfiguration configuration, final String replaceWith) {
             super(configuration, TemplateMode.TEXT);
-            // TODO Auto-generated constructor stub
+            replacer = replaceWith;
         }
 
         @Override
         protected String produceEscapedOutput(Object input) {
-            if (input instanceof String str) {
+            if ((replacer != null) && (input instanceof String str)) {
                 if (str.startsWith("Thi")) {
-                    return str.substring(3);
+                    return replacer + str.substring(3);
                 }
                 return str;
              }
             return input.toString();
         }
-	    
 	}
 
 	private class SandBoxDialect extends AbstractProcessorDialect {
 
 	    private static final String DIALECT_NAME = "SANDBOX Dialect";
 
-
 	    public SandBoxDialect() {
 	        super(DIALECT_NAME, "sandbox", StandardDialect.PROCESSOR_PRECEDENCE);
 	    }

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


More information about the commits mailing list