|
16 | 16 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
17 | 17 |
|
18 | 18 | import java.io.StringWriter; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
19 | 21 |
|
20 | 22 | import javax.script.ScriptContext; |
21 | 23 | import javax.script.ScriptEngine; |
22 | 24 | import javax.script.ScriptException; |
23 | 25 | import javax.script.SimpleScriptContext; |
24 | 26 |
|
| 27 | +import org.flowable.common.engine.api.variable.VariableContainer; |
| 28 | +import org.flowable.common.engine.impl.de.odysseus.el.util.SimpleResolver; |
| 29 | +import org.flowable.common.engine.impl.javax.el.BeanELResolver; |
| 30 | +import org.flowable.common.engine.impl.javax.el.CompositeELResolver; |
| 31 | +import org.flowable.common.engine.impl.javax.el.ELContext; |
| 32 | +import org.flowable.common.engine.impl.javax.el.ELException; |
| 33 | +import org.flowable.common.engine.impl.javax.el.ELResolver; |
| 34 | +import org.flowable.common.engine.impl.javax.el.MapELResolver; |
| 35 | +import org.flowable.common.engine.impl.scripting.FlowableScriptException; |
| 36 | +import org.flowable.common.engine.impl.scripting.JSR223FlowableScriptEngine; |
| 37 | +import org.flowable.common.engine.impl.scripting.Resolver; |
25 | 38 | import org.junit.jupiter.api.Test; |
26 | 39 |
|
27 | 40 | /** |
@@ -65,4 +78,79 @@ void printWritesToScriptContextWriter() throws ScriptException { |
65 | 78 | assertThat(writer).hasToString("Hello Kermit"); |
66 | 79 | } |
67 | 80 |
|
| 81 | + @Test |
| 82 | + void customScriptEngineFactoryCanProvideDifferentElResolver() throws FlowableScriptException { |
| 83 | + JSR223FlowableScriptEngine scriptEngine = new JSR223FlowableScriptEngine(); |
| 84 | + scriptEngine.addScriptEngineFactory(new NoMethodInvocationJuelScriptEngineFactory()); |
| 85 | + Map<String, Object> variables = Collections.singletonMap("person", Collections.singletonMap("name", "Kermit")); |
| 86 | + |
| 87 | + assertThat(evaluate(scriptEngine, "${person.name}", variables)).isEqualTo("Kermit"); |
| 88 | + assertThatThrownBy(() -> evaluate(scriptEngine, "${person.name.toUpperCase()}", variables)) |
| 89 | + .isInstanceOf(FlowableScriptException.class) |
| 90 | + .hasMessageContaining("Method invocation is not allowed"); |
| 91 | + |
| 92 | + assertThat(evaluate(new JSR223FlowableScriptEngine(), "${person.name.toUpperCase()}", variables)).isEqualTo("KERMIT"); |
| 93 | + } |
| 94 | + |
| 95 | + protected Object evaluate(JSR223FlowableScriptEngine scriptEngine, String script, Map<String, Object> variables) throws FlowableScriptException { |
| 96 | + return scriptEngine.createEvaluationRequest() |
| 97 | + .language("juel") |
| 98 | + .script(script) |
| 99 | + .resolver(new MapResolver(variables)) |
| 100 | + .scopeContainer(VariableContainer.empty()) |
| 101 | + .inputVariableContainer(VariableContainer.empty()) |
| 102 | + .evaluate() |
| 103 | + .getResult(); |
| 104 | + } |
| 105 | + |
| 106 | + protected static class MapResolver implements Resolver { |
| 107 | + |
| 108 | + protected final Map<String, Object> variables; |
| 109 | + |
| 110 | + public MapResolver(Map<String, Object> variables) { |
| 111 | + this.variables = variables; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean containsKey(Object key) { |
| 116 | + return variables.containsKey(key); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Object get(Object key) { |
| 121 | + return variables.get(key); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + protected static class NoMethodInvocationJuelScriptEngineFactory extends JuelScriptEngineFactory { |
| 126 | + |
| 127 | + @Override |
| 128 | + public ScriptEngine getScriptEngine() { |
| 129 | + return new NoMethodInvocationJuelScriptEngine(this); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + protected static class NoMethodInvocationJuelScriptEngine extends JuelScriptEngine { |
| 134 | + |
| 135 | + public NoMethodInvocationJuelScriptEngine(JuelScriptEngineFactory scriptEngineFactory) { |
| 136 | + super(scriptEngineFactory, false); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + protected ELResolver createElResolver() { |
| 141 | + CompositeELResolver compositeResolver = new CompositeELResolver(); |
| 142 | + compositeResolver.add(new MapELResolver()); |
| 143 | + compositeResolver.add(new NoMethodInvocationBeanELResolver()); |
| 144 | + return new SimpleResolver(compositeResolver); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + protected static class NoMethodInvocationBeanELResolver extends BeanELResolver { |
| 149 | + |
| 150 | + @Override |
| 151 | + public Object invoke(ELContext context, Object base, Object method, Class<?>[] paramTypes, Object[] params) { |
| 152 | + throw new ELException("Method invocation is not allowed"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
68 | 156 | } |
0 commit comments