Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile spel expressions #1223

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.predic8.membrane.core.lang.spel.*;
import org.slf4j.*;
import org.springframework.expression.*;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.*;

import java.util.*;
Expand Down Expand Up @@ -61,6 +63,7 @@ public class ConditionalInterceptor extends AbstractFlowInterceptor {
/**
* Spring Expression Language
*/
private final SpelParserConfiguration spelConfig = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
private Expression spelExpr;

private final InterceptorFlowController interceptorFlowController = new InterceptorFlowController();
Expand All @@ -82,7 +85,7 @@ public void init(Router router) throws Exception {
switch (language) {
case GROOVY ->
condition = new GroovyLanguageSupport().compileExpression(router.getBackgroundInitializator(), null, test);
case SPEL -> spelExpr = new SpelExpressionParser().parseExpression(test);
case SPEL -> spelExpr = new SpelExpressionParser(spelConfig).parseExpression(test);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.annot.Required;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

@MCElement(name = "additionalVariable", topLevel = false, id = "accessLog-scope")
public class AdditionalVariable {

private final SpelParserConfiguration spelConfig = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
private String name;
private Expression expression;
private String defaultValue = "-";
Expand All @@ -39,7 +42,7 @@ public Expression getExpression() {
@Required
@MCAttribute
public void setExpression(String expression) {
this.expression = new SpelExpressionParser()
this.expression = new SpelExpressionParser(spelConfig)
.parseExpression(expression);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

Expand All @@ -35,7 +37,8 @@
public abstract class AbstractSetterInterceptor extends AbstractInterceptor {

private static final Logger log = LoggerFactory.getLogger(AbstractSetterInterceptor.class);
private static final ExpressionParser parser = new SpelExpressionParser();
private final SpelParserConfiguration spelConfig = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
private final ExpressionParser parser = new SpelExpressionParser(spelConfig);
private final Pattern expressionPattern = Pattern.compile("\\$\\{(.*?)}");

protected String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.predic8.membrane.annot.Required;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

Expand All @@ -39,6 +41,7 @@ public class OAuth2PermissionCheckerInterceptor extends AbstractInterceptor {

private static final Logger log = LoggerFactory.getLogger(OAuth2PermissionCheckerInterceptor.class);

private final SpelParserConfiguration spelConfig = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
String expression;
ValueSource valueSource;
Function<Object, Boolean> valueChecker;
Expand Down Expand Up @@ -105,7 +108,7 @@ public Object evaluate(Exchange exc) {
}

private Function<Object, Boolean> createChecker(String expr) {
ExpressionParser parser = new SpelExpressionParser();
ExpressionParser parser = new SpelExpressionParser(spelConfig);
Expression exp = parser.parseExpression(expr);

return param -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class RateLimitInterceptor extends AbstractInterceptor {

private final RateLimitStrategy strategy;

private final SpelParserConfiguration spelConfig = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
private String keyExpression;
private Expression expression;

Expand Down Expand Up @@ -125,7 +126,7 @@ public void init() throws Exception {
super.init();
if (keyExpression == null || keyExpression.isBlank())
return;
expression = new SpelExpressionParser().parseExpression(keyExpression);
expression = new SpelExpressionParser(spelConfig).parseExpression(keyExpression);
}

private String getKey(Exchange exc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.predic8.membrane.core.rules.*;
import org.slf4j.*;
import org.springframework.expression.*;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.*;

import java.util.*;
Expand All @@ -33,6 +35,7 @@ public class APIProxyKey extends ServiceProxyKey {

private final ArrayList<String> basePaths = new ArrayList<>();

private final SpelParserConfiguration spelConfig = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
private Expression testExpr;

public APIProxyKey(RuleKey key, String test, boolean openAPI) {
Expand All @@ -49,7 +52,7 @@ public APIProxyKey(String ip, String host, int port, String path, String method,

protected void init(String test, boolean openAPI) {
if (test != null)
testExpr = new SpelExpressionParser().parseExpression(test);
testExpr = new SpelExpressionParser(spelConfig).parseExpression(test);

if (!openAPI)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ void rateLimitInitWithoutKeyExpression() throws Exception {
*/

@NotNull
private static Exchange getExchange() {
private static Exchange getExchange() throws URISyntaxException {
final Exchange exc = new Exchange(null);
exc.setRequest(new Request.Builder().header("accept","*/*").build());
exc.setRequest(new Request.Builder().get("/").header("accept","*/*").build());
exc.setResponse(Response.ok().build());
exc.setRemoteAddrIp("192.168.1.100");
return exc;
Expand Down
Loading