[java-shib-attribute] 01/01: IDP-2235 Abstract Template from being just Velocity
Rod Widdowson
rdw at steadingsoftware.com
Fri Feb 9 13:05:39 UTC 2024
This is an automated email from the git hooks/post-receive script.
rdw pushed a commit to branch dev/IDP-2235
in repository java-shib-attribute.
View the commit online:
http://git.shibboleth.net/view/?p=java-shib-attribute.git;a=commit;h=16610ef33b5914af5c74e8adcb8a3b91516fd214
commit 16610ef33b5914af5c74e8adcb8a3b91516fd214
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Mon Feb 5 20:52:14 2024 +0000
IDP-2235 Abstract Template from being just Velocity
https://shibboleth.atlassian.net/browse/IDP-2235
Very very ropy first attempt to see what the code looks like.
Rewrite TemplatedExecutableSearchFilterBuilder.java to use the new Template.
In practice this will need to be a different class since we are removing
a great deal of Velocity stuff from the API.
PAsses the tests tho.
---
.../TemplatedExecutableSearchFilterBuilder.java | 100 ++++++---------------
1 file changed, 29 insertions(+), 71 deletions(-)
diff --git a/shib-attribute-resolver-api/src/main/java/net/shibboleth/idp/attribute/resolver/dc/ldap/TemplatedExecutableSearchFilterBuilder.java b/shib-attribute-resolver-api/src/main/java/net/shibboleth/idp/attribute/resolver/dc/ldap/TemplatedExecutableSearchFilterBuilder.java
index 3073c1b50..6051c58ad 100644
--- a/shib-attribute-resolver-api/src/main/java/net/shibboleth/idp/attribute/resolver/dc/ldap/TemplatedExecutableSearchFilterBuilder.java
+++ b/shib-attribute-resolver-api/src/main/java/net/shibboleth/idp/attribute/resolver/dc/ldap/TemplatedExecutableSearchFilterBuilder.java
@@ -15,19 +15,14 @@
package net.shibboleth.idp.attribute.resolver.dc.ldap;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
import java.util.Map;
+import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.app.event.EventCartridge;
-import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
-import org.apache.velocity.context.Context;
-import org.apache.velocity.exception.VelocityException;
import org.ldaptive.FilterTemplate;
import org.slf4j.Logger;
@@ -39,7 +34,10 @@ import net.shibboleth.shared.annotation.constraint.NonnullAfterInit;
import net.shibboleth.shared.component.ComponentInitializationException;
import net.shibboleth.shared.primitive.LoggerFactory;
import net.shibboleth.shared.primitive.StringSupport;
-import net.shibboleth.shared.velocity.Template;
+import net.shibboleth.shared.template.Template;
+import net.shibboleth.shared.template.TemplateContext;
+import net.shibboleth.shared.template.TemplateEngine;
+import net.shibboleth.shared.velocity.VelocityTemplateEngine;
/**
* An {@link ExecutableSearchBuilder} that generates the search filter to
@@ -58,10 +56,7 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
@NonnullAfterInit private String templateText;
/** VelocityEngine. */
- @NonnullAfterInit private VelocityEngine engine;
-
- /** Event handler used for escaping. */
- private ReferenceInsertionEventHandler eventHandler = new EscapingReferenceInsertionEventHandler();
+ @NonnullAfterInit private TemplateEngine engine;
/** Do we need to make ourself V2 Compatible? */
private boolean v2Compatibility;
@@ -99,7 +94,7 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
* Gets the {@link VelocityEngine} to be used.
*
* @return the velocity engine
- */
+ *
@Nullable @NonnullAfterInit public VelocityEngine getVelocityEngine() {
return engine;
}
@@ -109,17 +104,17 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
*
* @param velocityEngine engine to be used
*/
- public void setVelocityEngine(final VelocityEngine velocityEngine) {
+ public void setVelocityEngine(@Nonnull VelocityEngine velocityEngine) {
checkSetterPreconditions();
- engine = velocityEngine;
+ engine = new VelocityTemplateEngine(velocityEngine);
}
/**
* Gets the {@link ReferenceInsertionEventHandler} to be used.
*
* @return the reference insertion event handler
- */
+ *
@Nullable public ReferenceInsertionEventHandler getReferenceInsertionEventHandler() {
return eventHandler;
}
@@ -128,13 +123,12 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
* Sets the {@link ReferenceInsertionEventHandler} to be used.
*
* @param handler reference insertion event handler to be used
- */
+ *
public void setReferenceInsertionEventHandler(@Nullable final ReferenceInsertionEventHandler handler) {
checkSetterPreconditions();
eventHandler = handler;
}
-
/**
* Are we in V2 Compatibility mode?
*
@@ -156,8 +150,8 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
/** Method to allow private additions to the velocity context.
* @param velocityContext where to add the information
* @param resolutionContext current resolution context
- */
- protected void addExtraVelocityContext(@Nonnull final VelocityContext velocityContext,
+ *
+ protected void addExtraVelocityContext(@Nonnull final TemplateContext Context,
@Nonnull final AttributeResolutionContext resolutionContext) {
}
@@ -165,7 +159,7 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
@Override protected void doInitialize() throws ComponentInitializationException {
super.doInitialize();
- final VelocityEngine localEngine = engine;
+ final TemplateEngine localEngine = engine;
final String localTemplateText = templateText;
if (null == localEngine) {
throw new ComponentInitializationException(
@@ -177,6 +171,17 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
}
template = Template.fromTemplate(localEngine, localTemplateText);
+ template.setTemplateTextEscaper(new Function<Object, String>() {
+ public String apply(Object value) {
+ if (value instanceof String){
+ return FilterTemplate.encodeValue((String) value);
+ } else if (value instanceof byte[]) {
+ return FilterTemplate.encodeValue((byte[]) value);
+ }
+ return value.toString();
+
+ }
+ });
}
/** {@inheritDoc} */
@@ -184,11 +189,11 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
@Nonnull public ExecutableSearchFilter build(@Nonnull final AttributeResolutionContext resolutionContext,
@Nonnull final Map<String, List<IdPAttributeValue>> dependencyAttributes) throws ResolutionException {
- final VelocityContext context = new VelocityContext();
+ final TemplateContext context = new TemplateContext();
log.trace("Creating search filter using attribute resolution context {}", resolutionContext);
context.put("resolutionContext", resolutionContext);
- addExtraVelocityContext(context, resolutionContext);
+ //addExtraVelocityContext(context, resolutionContext);
if (dependencyAttributes != null && !dependencyAttributes.isEmpty()) {
for (final Map.Entry<String, List<IdPAttributeValue>> entry : dependencyAttributes.entrySet()) {
final List<Object> values = new ArrayList<>(entry.getValue().size());
@@ -200,16 +205,10 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
}
}
- if (eventHandler != null) {
- final EventCartridge cartridge = new EventCartridge();
- cartridge.addEventHandler(eventHandler);
- cartridge.attachToContext(context);
- }
-
try {
final FilterTemplate searchFilter = new FilterTemplate(merge(context));
return super.build(searchFilter);
- } catch (final VelocityException e) {
+ } catch (final Exception e) {
log.error("Error running template: {}", e.getMessage());
throw new ResolutionException("Error running template", e);
}
@@ -222,51 +221,10 @@ public class TemplatedExecutableSearchFilterBuilder extends AbstractExecutableSe
*
* @return result of the merge operation
*/
- @Nonnull protected String merge(@Nonnull final VelocityContext context) {
+ @Nonnull protected String merge(@Nonnull final TemplateContext context) {
final String result = template.merge(context).trim();
log.debug("Template text {} yields {}", templateText, result);
assert result != null;
return result;
}
-
- /** Escapes LDAP attribute values added to the template context. */
- protected static class EscapingReferenceInsertionEventHandler implements ReferenceInsertionEventHandler {
-
- @Override public Object referenceInsert(final Context context, final String reference, final Object value) {
- if (value == null) {
- return null;
- } else if (value instanceof Object[]) {
- final List<Object> encodedValues = new ArrayList<>();
- for (final Object o : (Object[]) value) {
- encodedValues.add(encode(o));
- }
- return encodedValues.toArray();
- } else if (value instanceof Collection) {
- final List<Object> encodedValues = new ArrayList<>();
- for (final Object o : (Collection<?>) value) {
- encodedValues.add(encode(o));
- }
- return encodedValues;
- } else {
- return encode(value);
- }
- }
-
- /**
- * Returns {@link FilterTemplate#encodeValue} if value is a string.
- *
- * @param value to encode
- *
- * @return encoded value if value is a string
- */
- private Object encode(final Object value) {
- if (value instanceof String){
- return FilterTemplate.encodeValue((String) value);
- } else if (value instanceof byte[]) {
- return FilterTemplate.encodeValue((byte[]) value);
- }
- return value;
- }
- }
-
}
\ 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