org.apache.commons.jexl3.JexlScript Java Examples
The following examples show how to use
org.apache.commons.jexl3.JexlScript.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JexlClaimsMapper.java From cxf with Apache License 2.0 | 6 votes |
public ProcessedClaimCollection mapClaims(String sourceRealm, ProcessedClaimCollection sourceClaims, String targetRealm, ClaimsParameters parameters) { JexlContext context = new MapContext(); context.set("sourceClaims", sourceClaims); context.set("targetClaims", new ProcessedClaimCollection()); context.set("sourceRealm", sourceRealm); context.set("targetRealm", targetRealm); context.set("claimsParameters", parameters); JexlScript s = getScript(); if (s == null) { LOG.warning("No claim mapping script defined"); return new ProcessedClaimCollection(); // TODO Check if null or an exception would be more // appropriate } return (ProcessedClaimCollection)s.execute(context); }
Example #2
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testSandboxInherit1() throws Exception { Object result; JexlContext ctxt = null; Operation2 foo = new Operation2(12); JexlSandbox sandbox = new JexlSandbox(false, true); sandbox.allow(Operation.class.getName()); sandbox.block(Operation.class.getName()).execute("nonCallable"); //sandbox.block(Foo.class.getName()).execute(); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); JexlScript someOp = sjexl.createScript("foo.someOp(y)", "foo", "y"); result = someOp.execute(ctxt, foo, 30); Assert.assertEquals(42, result); JexlScript nonCallable = sjexl.createScript("foo.nonCallable(y)", "foo", "y"); try { result = nonCallable.execute(null, foo, 0); Assert.fail("should not be possible"); } catch (JexlException xjm) { // ok LOGGER.info(xjm.toString()); } }
Example #3
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testSandboxInherit0() throws Exception { Object result; JexlContext ctxt = null; List<String> foo = new ArrayList<String>(); JexlSandbox sandbox = new JexlSandbox(false, true); sandbox.allow(java.util.List.class.getName()); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); JexlScript method = sjexl.createScript("foo.add(y)", "foo", "y"); JexlScript set = sjexl.createScript("foo[x] = y", "foo", "x", "y"); JexlScript get = sjexl.createScript("foo[x]", "foo", "x"); result = method.execute(ctxt, foo, "nothing"); Assert.assertEquals(true, result); result = null; result = get.execute(null, foo, 0); Assert.assertEquals("nothing", result); result = null; result = set.execute(null, foo, 0, "42"); Assert.assertEquals("42", result); result = null; result = get.execute(null, foo, 0); Assert.assertEquals("42", result); }
Example #4
Source File: JexlScriptEngine.java From commons-jexl with Apache License 2.0 | 6 votes |
@Override public Object eval(final String script, final ScriptContext context) throws ScriptException { // This is mandated by JSR-223 (see SCR.5.5.2 Methods) if (script == null || context == null) { throw new NullPointerException("script and context must be non-null"); } // This is mandated by JSR-223 (end of section SCR.4.3.4.1.2 - JexlScript Execution) context.setAttribute(CONTEXT_KEY, context, ScriptContext.ENGINE_SCOPE); try { JexlScript jexlScript = jexlEngine.createScript(script); JexlContext ctxt = new JexlContextWrapper(context); return jexlScript.execute(ctxt); } catch (Exception e) { throw new ScriptException(e.toString()); } }
Example #5
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testSetAllow() throws Exception { Foo foo = new Foo("42"); String expr = "foo.alias = $0"; JexlScript script; Object result; JexlSandbox sandbox = new JexlSandbox(); sandbox.allow(Foo.class.getName()).write("alias"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); script = sjexl.createScript(expr, "foo", "$0"); result = script.execute(null, foo, "43"); Assert.assertEquals("43", result); Assert.assertEquals("43", foo.alias); }
Example #6
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testGetAllow() throws Exception { Foo foo = new Foo("42"); String expr = "foo.alias"; JexlScript script; Object result; JexlSandbox sandbox = new JexlSandbox(); sandbox.allow(Foo.class.getName()).read("alias"); sandbox.get(Foo.class.getName()).read().alias("alias", "ALIAS"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); script = sjexl.createScript(expr, "foo"); result = script.execute(null, foo); Assert.assertEquals(foo.alias, result); script = sjexl.createScript("foo.ALIAS", "foo"); result = script.execute(null, foo); Assert.assertEquals(foo.alias, result); }
Example #7
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testMethodNoJexl() throws Exception { Foo foo = new Foo("42"); String[] exprs = { "foo.cantCallMe()", "foo.tryMe()", "foo.tryMeARiver()", "foo.callMeNot()", "foo.NONO", "new('org.apache.commons.jexl3.SandboxTest$Foo', 'one', 'two')" }; JexlScript script; Object result; JexlEngine sjexl = new JexlBuilder().strict(true).safe(false).create(); for (String expr : exprs) { script = sjexl.createScript(expr, "foo"); try { result = script.execute(null, foo); Assert.fail("should have not been possible"); } catch (JexlException.Method | JexlException.Property xjm) { // ok LOGGER.info(xjm.toString()); } } }
Example #8
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testCantSeeMe() throws Exception { JexlContext jc = new MapContext(); String expr = "foo.doIt()"; JexlScript script; Object result = null; JexlSandbox sandbox = new JexlSandbox(false); sandbox.allow(Foo.class.getName()); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); jc.set("foo", new CantSeeMe()); script = sjexl.createScript(expr); try { result = script.execute(jc); Assert.fail("should have failed, doIt()"); } catch (JexlException xany) { // } jc.set("foo", new Foo("42")); result = script.execute(jc); Assert.assertEquals(42, ((Integer) result).intValue()); }
Example #9
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testSetBlock() throws Exception { String expr = "foo.alias = $0"; JexlScript script = JEXL.createScript(expr, "foo", "$0"); Foo foo = new Foo("42"); Object result; result = script.execute(null, foo, "43"); Assert.assertEquals("43", result); JexlSandbox sandbox = new JexlSandbox(); sandbox.block(Foo.class.getName()).write("alias"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr, "foo", "$0"); try { result = script.execute(null, foo, "43"); Assert.fail("alias should not be accessible"); } catch (JexlException.Property xvar) { // ok, alias should not have been accessible LOGGER.info(xvar.toString()); } }
Example #10
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testGetBlock() throws Exception { String expr = "foo.alias"; JexlScript script = JEXL.createScript(expr, "foo"); Foo foo = new Foo("42"); Object result; result = script.execute(null, foo); Assert.assertEquals(foo.alias, result); JexlSandbox sandbox = new JexlSandbox(); sandbox.block(Foo.class.getName()).read("alias"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr, "foo"); try { result = script.execute(null, foo); Assert.fail("alias should not be accessible"); } catch (JexlException.Property xvar) { // ok, alias should not have been accessible LOGGER.info(xvar.toString()); } }
Example #11
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testMethodBlock() throws Exception { String expr = "foo.Quux()"; JexlScript script = JEXL.createScript(expr, "foo"); Foo foo = new Foo("42"); Object result; result = script.execute(null, foo); Assert.assertEquals(foo.Quux(), result); JexlSandbox sandbox = new JexlSandbox(); sandbox.block(Foo.class.getName()).execute("Quux"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr, "foo"); try { result = script.execute(null, foo); Assert.fail("Quux should not be accessible"); } catch (JexlException.Method xmethod) { // ok, Quux should not have been accessible LOGGER.info(xmethod.toString()); } }
Example #12
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testCtorBlock() throws Exception { String expr = "new('" + Foo.class.getName() + "', '42')"; JexlScript script = JEXL.createScript(expr); Object result; result = script.execute(null); Assert.assertEquals("42", ((Foo) result).getName()); JexlSandbox sandbox = new JexlSandbox(); sandbox.block(Foo.class.getName()).execute(""); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr); try { result = script.execute(null); Assert.fail("ctor should not be accessible"); } catch (JexlException.Method xmethod) { // ok, ctor should not have been accessible LOGGER.info(xmethod.toString()); } }
Example #13
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testNoJexl312() throws Exception { JexlContext ctxt = new MapContext(); JexlEngine sjexl = new JexlBuilder().safe(false).strict(true).create(); JexlScript foo = sjexl.createScript("x.getFoo()", "x"); try { foo.execute(ctxt, new Foo44()); Assert.fail("should have thrown"); } catch (JexlException xany) { Assert.assertNotNull(xany); } }
Example #14
Source File: EvalExpressionProcessor.java From vividus with Apache License 2.0 | 5 votes |
@Override public Optional<String> execute(String expression) { Matcher expressionMatcher = EVAL_PATTERN.matcher(expression); if (expressionMatcher.find()) { String expressionToEvaluate = expressionMatcher.group(EVAL_GROUP); JexlScript jexlScript = jexlEngine.get().createScript(expressionToEvaluate); return Optional.of(String.valueOf(jexlScript.execute(new JexlBddVariableContext(bddVariableContext)))); } return Optional.empty(); }
Example #15
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testMethodAllow() throws Exception { Foo foo = new Foo("42"); String expr = "foo.Quux()"; JexlScript script; Object result; JexlSandbox sandbox = new JexlSandbox(); sandbox.allow(Foo.class.getName()).execute("Quux"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr, "foo"); result = script.execute(null, foo); Assert.assertEquals(foo.Quux(), result); }
Example #16
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testCtorAllow() throws Exception { String expr = "new('" + Foo.class.getName() + "', '42')"; JexlScript script; Object result; JexlSandbox sandbox = new JexlSandbox(); sandbox.allow(Foo.class.getName()).execute(""); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr); result = script.execute(null); Assert.assertEquals("42", ((Foo) result).getName()); }
Example #17
Source File: Asserter.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * Performs an assertion that the expression fails throwing an exception. * If matchException is not null, the exception message is expected to match it as a regexp. * The engine is temporarily switched to strict * verbose to maximize error detection abilities. * @param expression the expression that should fail * @param matchException the exception message pattern * @throws Exception if the expression did not fail or the exception did not match the expected pattern */ public void failExpression(String expression, String matchException) throws Exception { try { JexlScript exp = engine.createScript(expression); exp.execute(context); fail("expression: " + expression); } catch (JexlException xjexl) { if (matchException != null && !xjexl.getMessage().matches(matchException)) { fail("expression: " + expression + ", expected: " + matchException + ", got " + xjexl.getMessage()); } } }
Example #18
Source File: Asserter.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * Performs an assertion that the value of the given JEXL expression * evaluates to the given expected value. * * @param expression is the JEXL expression to evaluate * @param expected is the expected value of the expression * @throws Exception if the expression could not be evaluationed or an assertion * fails */ public void assertExpression(String expression, Object expected) throws Exception { JexlScript exp = engine.createScript(expression); Object value = exp.execute(context); if (expected instanceof BigDecimal) { JexlArithmetic jexla = engine.getArithmetic(); Assert.assertEquals("expression: " + expression, 0, ((BigDecimal) expected).compareTo(jexla.toBigDecimal(value))); } if (expected != null && value != null) { if (expected.getClass().isArray() && value.getClass().isArray()) { int esz = Array.getLength(expected); int vsz = Array.getLength(value); String report = "expression: " + expression; Assert.assertEquals(report + ", array size", esz, vsz); for (int i = 0; i < vsz; ++i) { Assert.assertEquals(report + ", value@[]" + i, Array.get(expected, i), Array.get(value, i)); } } else { Assert.assertEquals("expression: " + expression + ", " + expected.getClass().getSimpleName() + " ?= " + value.getClass().getSimpleName(), expected, value); } } else { Assert.assertEquals("expression: " + expression, expected, value); } }
Example #19
Source File: ScriptVisitor.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * Visits all AST constituents of a JEXL script. * @param jscript the expression * @param data some data context * @return the visit result or null if jscript was not a Script implementation */ public Object visitScript(JexlScript jscript, Object data) { if (jscript instanceof Script) { return ((Script) jscript).getScript().jjtAccept(this, data); } return null; }
Example #20
Source File: Debugger.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * Position the debugger on the root of a script. * @param jscript the script * @return true if the script was a {@link Script} instance, false otherwise */ public boolean debug(JexlScript jscript) { if (jscript instanceof Script) { return debug(((Script) jscript).script); } else { return false; } }
Example #21
Source File: Script.java From commons-jexl with Apache License 2.0 | 5 votes |
@Override public JexlScript curry(Object... args) { String[] parms = script.getParameters(); if (parms == null || parms.length == 0) { return this; } return new Closure(this, args); }
Example #22
Source File: JexlScriptEngine.java From commons-jexl with Apache License 2.0 | 5 votes |
@Override public CompiledScript compile(final String script) throws ScriptException { // This is mandated by JSR-223 if (script == null) { throw new NullPointerException("script must be non-null"); } try { JexlScript jexlScript = jexlEngine.createScript(script); return new JexlCompiledScript(jexlScript); } catch (Exception e) { throw new ScriptException(e.toString()); } }
Example #23
Source File: ScriptStepTest.java From gp2srv with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void testExecScript() { ScriptStep scriptStep = new ScriptStep(ScriptStepType.EXEC_SCRIPT, null, "x=2+2", null); JexlEngine mockEngine = Mockito.mock(JexlEngine.class); JexlScript jexlScript = Mockito.mock(JexlScript.class); Mockito.when(mockEngine.createScript(Mockito.any(JexlInfo.class), Mockito.anyString(), Mockito.any(String[].class))).thenReturn(jexlScript); Assert.assertFalse(scriptStep.execute(null, 12345, mockEngine, jexlContext, null, null)); Mockito.verify(mockEngine).createScript(null, "x=2+2", null); Mockito.verify(jexlScript).execute(jexlContext); }
Example #24
Source File: Dumper.java From commons-jexl with Apache License 2.0 | 4 votes |
private Dumper(JexlScript script) { dump(((Script) script).script, null); }
Example #25
Source File: Dumper.java From commons-jexl with Apache License 2.0 | 4 votes |
public static String toString(JexlScript script) { return new Dumper(script).toString(); }
Example #26
Source File: JexlClaimsMapper.java From cxf with Apache License 2.0 | 4 votes |
public final void setScript(JexlScript script) { this.script = script; }
Example #27
Source File: JexlClaimsMapper.java From cxf with Apache License 2.0 | 4 votes |
public JexlScript getScript() { return script; }
Example #28
Source File: Util.java From commons-jexl with Apache License 2.0 | 2 votes |
/** * A helper class to help validate AST problems. * @param e the script * @return an indented version of the AST */ protected static String flattenedStr(JexlScript e) { return "";//e.getText() + "\n" + flattenedStr(((Script)e).script); }
Example #29
Source File: Engine.java From commons-jexl with Apache License 2.0 | 2 votes |
/** * Gets the array of local variable from a script. * @param script the script * @return the local variables array which may be empty (but not null) if no local variables were defined * @since 3.0 */ protected String[] getLocalVariables(JexlScript script) { return script.getLocalVariables(); }
Example #30
Source File: Engine.java From commons-jexl with Apache License 2.0 | 2 votes |
/** * Gets the array of parameters from a script. * @param script the script * @return the parameters which may be empty (but not null) if no parameters were defined * @since 3.0 */ protected String[] getParameters(JexlScript script) { return script.getParameters(); }