Skip to content

Commit

Permalink
Merge pull request #126 from Linfar/use_simple_evaluation_context
Browse files Browse the repository at this point in the history
Use SimpleEvaluationContext instead of StandardEvaluationContext
  • Loading branch information
morincer authored Feb 5, 2024
2 parents 413126b + cf9900a commit 929f7a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import com.onelogin.saml2.Auth;
import lombok.var;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpelExpressionContext extends StandardEvaluationContext {
public class SpelExpressionContext {
private final EvaluationContext evaluationContext;

public SpelExpressionContext(Map<String, ?> attributes) {
setRootObject(attributes);
addPropertyAccessor(new MapAccessor());
evaluationContext = new SimpleEvaluationContext.Builder(new MapAccessor()).withInstanceMethods().withRootObject(attributes).build();
}

public SpelExpressionContext(Auth auth) {
Expand All @@ -31,11 +31,14 @@ public SpelExpressionContext(Auth auth) {
attributes.put("nameid", auth.getNameId());
attributes.put("lastassertionid", auth.getLastAssertionId());

setRootObject(attributes);
addPropertyAccessor(new MapAccessor());
evaluationContext = new SimpleEvaluationContext.Builder(new MapAccessor()).withRootObject(attributes).build();
}

public <T> Map<String, T> getRootObjectAsMap() {
return (Map<String, T>) getRootObject().getValue();
return (Map<String, T>) evaluationContext.getRootObject().getValue();
}

public EvaluationContext getEvaluationContext() {
return evaluationContext;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package jetbrains.buildServer.auth.saml.plugin.utils;

import com.onelogin.saml2.Auth;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ParseException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SpelExpressionExecutor {
Expand All @@ -29,7 +22,7 @@ public String validate(String expressionString) {
public String evaluate(String expressionString, SpelExpressionContext context) {
log.debug(String.format("Evaluating expression: %s", expressionString));
var expression = parser.parseExpression(expressionString);
String result = (String) expression.getValue(context);
String result = (String) expression.getValue(context.getEvaluationContext());
log.debug(String.format("Evaluation result: %s", result));
return result;
}
Expand Down

0 comments on commit 929f7a1

Please sign in to comment.