Java Code Examples for org.apache.commons.jexl3.JexlContext#set()

The following examples show how to use org.apache.commons.jexl3.JexlContext#set() . 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 vote down vote up
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: JexlRowFactory.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public String getRawValue(String key) {
  if (data.containsKey(key)) {
    return data.get(key);
  } else if (expressions.containsKey(key)) {
    try {
      JexlContext jexlContext = new MapContext();
      jexlContext.set("v", data);
      Object result = expressions.get(key).evaluate(jexlContext);
      if (result != null) {
        return result.toString();
      } else {
        return null;
      }
    } catch (Throwable throwable) {
      LOG.info("Error during mapping", throwable);
      errorHandler.valueGenerateFailed(
        key,
        String.format("Could not execute expression '%s' for row with values: '%s.", expressions.get(key), data)
      );
      return null;
    }
  } else {
    return null;
  }
}
 
Example 3
Source File: SandboxTest.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: GlobalVariableValidationAction.java    From android-uiconductor with Apache License 2.0 5 votes vote down vote up
@Override
boolean validateRaw(ActionContext actionContext, AndroidDeviceDriver androidDeviceDriver) {
  // Create or retrieve an engine
  JexlEngine jexl = new JexlBuilder().create();

  // Create an expression
  JexlExpression e = jexl.createExpression(expression);

  JexlContext jc = new MapContext();

  // To use advanced expression, need a converter to do the trick, the expression will be like
  // "uicdTypeConverter.toInt($uicd_var1) + uicdTypeConverter.toInt($uicd_var2)";
  if (expression.contains(TYPE_CONVERTER_OBJ_KEYWORD)) {
    jc.set(TYPE_CONVERTER_OBJ_KEYWORD, new UicdTypeConverter());
  }
  // Create a context and add data
  // jc.set("$uicd_var1", new String("adbcd"));
  // Set the displayStr so that we can see the result in the test details.
  displayStr = expandUicdGlobalVariableToJexl(expression, jc, actionContext);

  // Now evaluate the expression, getting the result
  boolean ret = false;
  try {
    Object o = e.evaluate(jc);
    ret = Boolean.parseBoolean(o.toString());
  } catch (Exception ex) {
    System.out.println(ex.getMessage());
  }
  displayStr = String.format("%s|validation result:%s", displayStr, ret);
  return ret;
}
 
Example 5
Source File: GlobalVariableValidationAction.java    From android-uiconductor with Apache License 2.0 5 votes vote down vote up
private String expandUicdGlobalVariableToJexl(
    String jexlExp, JexlContext jc, ActionContext actionContext) {
  StringBuilder sb = new StringBuilder();
  // target could have multiple "$uicd_", we need replace all of them
  for (String key : actionContext.getGlobalVariableMap().getRawMap().keySet()) {
    if (jexlExp.contains(key)) {
      jc.set(key, actionContext.getGlobalVariableMap().getRawValue(key));
      sb.append(
          String.format("%s:%s|", key, actionContext.getGlobalVariableMap().getRawValue(key)));
    }
  }
  return sb.toString();
}
 
Example 6
Source File: AntennaDoctorEngine.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public List<AntennaDoctorMessage> onStage(AntennaDoctorStageContext context, Exception exception) {
  JexlContext jexlContext = new MapContext();
  jexlContext.set("issue", new StageIssueJexl(exception));
  jexlContext.set("stageDef", context.getStageDefinition());
  jexlContext.set("stageConf", context.getStageConfiguration());
  return evaluate(context, AntennaDoctorRuleBean.Entity.STAGE, jexlContext);
}
 
Example 7
Source File: AntennaDoctorEngine.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public List<AntennaDoctorMessage> onStage(AntennaDoctorStageContext context, ErrorCode errorCode, Object... args) {
  JexlContext jexlContext = new MapContext();
  jexlContext.set("issue", new StageIssueJexl(errorCode, args));
  jexlContext.set("stageDef", context.getStageDefinition());
  jexlContext.set("stageConf", context.getStageConfiguration());
  return evaluate(context, AntennaDoctorRuleBean.Entity.STAGE, jexlContext);
}
 
Example 8
Source File: AntennaDoctorEngine.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public List<AntennaDoctorMessage> onStage(AntennaDoctorStageContext context, String errorMessage) {
  JexlContext jexlContext = new MapContext();
  jexlContext.set("issue", new StageIssueJexl(errorMessage));
  jexlContext.set("stageDef", context.getStageDefinition());
  jexlContext.set("stageConf", context.getStageConfiguration());
  return evaluate(context, AntennaDoctorRuleBean.Entity.STAGE, jexlContext);
}
 
Example 9
Source File: AntennaDoctorEngine.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public List<AntennaDoctorMessage> onValidation(AntennaDoctorStageContext context, String groupName, String configName, ErrorCode errorCode, Object... args) {
  JexlContext jexlContext = new MapContext();
  jexlContext.set("issue", new StageIssueJexl(groupName, configName, errorCode, args));
  jexlContext.set("stageDef", context.getStageDefinition());
  jexlContext.set("stageConf", context.getStageConfiguration());
  return evaluate(context, AntennaDoctorRuleBean.Entity.VALIDATION, jexlContext);
}
 
Example 10
Source File: ArrayTest.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * An example for array access.
 */
static void example(Output out) throws Exception {
    /*
     * First step is to retrieve an instance of a JexlEngine;
     * it might be already existing and shared or created anew.
     */
    JexlEngine jexl = new JexlBuilder().create();
    /*
     *  Second make a jexlContext and put stuff in it
     */
    JexlContext jc = new MapContext();

    List<Object> l = new ArrayList<Object>();
    l.add("Hello from location 0");
    Integer two = 2;
    l.add(two);
    jc.set("array", l);

    JexlExpression e = jexl.createExpression("array[1]");
    Object o = e.evaluate(jc);
    out.print("Object @ location 1 = ", o, two);

    e = jexl.createExpression("array[0].length()");
    o = e.evaluate(jc);

    out.print("The length of the string at location 0 is : ", o, 21);
}
 
Example 11
Source File: MethodPropertyTest.java    From tools-journey with Apache License 2.0 4 votes vote down vote up
/**
 * An example for method access.
 */
public static void example(final Output out) throws Exception {
    /**
     * First step is to retrieve an instance of a JexlEngine;
     * it might be already existing and shared or created anew.
     */
    JexlEngine jexl = new JexlBuilder().create();
    /*
     *  Second make a jexlContext and put stuff in it
     */
    JexlContext jc = new MapContext();

    /**
     * The Java equivalents of foo and number for comparison and checking
     */
    Foo foo = new Foo();
    Integer number = new Integer(10);

    jc.set("foo", foo);
    jc.set("number", number);

    /*
     *  access a method w/o args
     */
    JexlExpression e = jexl.createExpression("foo.getFoo()");
    Object o = e.evaluate(jc);
    out.print("value returned by the method getFoo() is : ", o, foo.getFoo());

    /*
     *  access a method w/ args
     */
    e = jexl.createExpression("foo.convert(1)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1));

    e = jexl.createExpression("foo.convert(1+7)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+7));

    e = jexl.createExpression("foo.convert(1+number)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+number.intValue()));

    /*
     * access a property
     */
    e = jexl.createExpression("foo.bar");
    o = e.evaluate(jc);
    out.print("value returned for the property 'bar' is : ", o, foo.get("bar"));

}
 
Example 12
Source File: AntennaDoctorEngine.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public List<AntennaDoctorMessage> onRest(AntennaDoctorContext context, ErrorCode errorCode, Object... args) {
  JexlContext jexlContext = new MapContext();
  jexlContext.set("issue", new StageIssueJexl(errorCode, args));
  return evaluate(context, AntennaDoctorRuleBean.Entity.REST, jexlContext);
}
 
Example 13
Source File: AntennaDoctorEngine.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public List<AntennaDoctorMessage> onRest(AntennaDoctorContext context, Exception exception) {
  JexlContext jexlContext = new MapContext();
  jexlContext.set("issue", new StageIssueJexl(exception));
  return evaluate(context, AntennaDoctorRuleBean.Entity.REST, jexlContext);
}
 
Example 14
Source File: MethodPropertyTest.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
/**
 * An example for method access.
 */
public static void example(final Output out) throws Exception {
    /*
     * First step is to retrieve an instance of a JexlEngine;
     * it might be already existing and shared or created anew.
     */
    JexlEngine jexl = new JexlBuilder().create();
    /*
     *  Second make a jexlContext and put stuff in it
     */
    JexlContext jc = new MapContext();

    /*
     * The Java equivalents of foo and number for comparison and checking
     */
    Foo foo = new Foo();
    Integer number = 10;

    jc.set("foo", foo);
    jc.set("number", number);

    /*
     *  access a method w/o args
     */
    JexlExpression e = jexl.createExpression("foo.getFoo()");
    Object o = e.evaluate(jc);
    out.print("value returned by the method getFoo() is : ", o, foo.getFoo());

    /*
     *  access a method w/ args
     */
    e = jexl.createExpression("foo.convert(1)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1));

    e = jexl.createExpression("foo.convert(1+7)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+7));

    e = jexl.createExpression("foo.convert(1+number)");
    o = e.evaluate(jc);
    out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+ number));

    /*
     * access a property
     */
    e = jexl.createExpression("foo.bar");
    o = e.evaluate(jc);
    out.print("value returned for the property 'bar' is : ", o, foo.get("bar"));

}