org.springframework.context.expression.MapAccessor Java Examples

The following examples show how to use org.springframework.context.expression.MapAccessor. 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: ExpressionEvaluationServiceImpl.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Override
public String evaluateTemplate(String expressionTemplate, Map<String, Object> evaluationContext) {
    Optional.ofNullable(expressionTemplate)
        .filter(template -> !template.isEmpty())
        .orElseThrow(() -> new IllegalArgumentException("Expression template cannot be null or empty"));

    Optional.ofNullable(evaluationContext)
        .orElseThrow(() -> new IllegalArgumentException("Evaluation context cannot be null"));

    Expression expression = new SpelExpressionParser().parseExpression(expressionTemplate
            ,new TemplateParserContext("@{","}"));

    StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
    try {
        standardEvaluationContext.registerFunction("urlEncode",
                Functions.class.getDeclaredMethod("urlEncode", new Class[] {String.class}));
    } catch (NoSuchMethodException e) {
        throw new EvaluationException("Fail to register function to evaluation context", e);
    }
    standardEvaluationContext.addPropertyAccessor(new MapAccessor());
    standardEvaluationContext.setVariables(evaluationContext);

    return expression.getValue(standardEvaluationContext,String.class);
}
 
Example #2
Source File: SpELFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 创建SpEL执行上下文
 *
 * @param rootObject
 *            SpEL表达式根对象
 * @param context
 *            Beetl上下文对象
 * @return SpEL表达式执行上下文
 */
private EvaluationContext createEvaluationContext(Object rootObject, Context beetlContext) {
	StandardEvaluationContext context = new StandardEvaluationContext(rootObject);

	// 允许使用#context访问Beetl上下文
	context.setVariable("context", beetlContext);
	// 允许使用#global访问Beetl上下文的全局变量
	context.setVariable("global", beetlContext.globalVar);
	// 注册WebRender定义的全局变量
	context.setVariable("ctxPath", beetlContext.getGlobal("ctxPath"));
	context.setVariable("servlet", beetlContext.getGlobal("servlet"));
	context.setVariable("parameter", beetlContext.getGlobal("parameter"));
	context.setVariable("request", beetlContext.getGlobal("request"));
	context.setVariable("session", beetlContext.getGlobal("session"));
	// 允许使用属性格式访问Map
	context.addPropertyAccessor(new MapAccessor());
	// 允许访问Spring容器Bean
	context.setBeanResolver(new BeanFactoryResolver(applicationContext));

	return context;
}
 
Example #3
Source File: ExpressionEvaluationServiceImpl.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluateConditional(String conditionalExpression, Map<String, Object> evaluationContext) {
    Optional.ofNullable(conditionalExpression)
            .filter(template -> !template.isEmpty())
            .orElseThrow(() -> new IllegalArgumentException("Conditional expression cannot be null or empty"));

    Optional.ofNullable(evaluationContext)
            .orElseThrow(() -> new IllegalArgumentException("Evaluation context cannot be null"));

    Expression expression = new SpelExpressionParser().parseExpression(conditionalExpression);

    StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
    standardEvaluationContext.addPropertyAccessor(new MapAccessor());
    standardEvaluationContext.setVariables(evaluationContext);

    return expression.getValue(standardEvaluationContext,Boolean.class);
}
 
Example #4
Source File: TelegramNotifier.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getText(InstanceEvent event, Instance instance) {
    Map<String, Object> root = new HashMap<>();
    root.put("event", event);
    root.put("instance", instance);
    root.put("lastStatus", getLastStatus(event.getInstance()));
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.addPropertyAccessor(new MapAccessor());
    return message.getValue(context, String.class);
}
 
Example #5
Source File: PagerdutyNotifier.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getDescription(InstanceEvent event, Instance instance) {
    Map<String, Object> root = new HashMap<>();
    root.put("event", event);
    root.put("instance", instance);
    root.put("lastStatus", getLastStatus(event.getInstance()));
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.addPropertyAccessor(new MapAccessor());

    return description.getValue(context, String.class);
}
 
Example #6
Source File: OpsGenieNotifier.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getMessage(InstanceEvent event, Instance instance) {
	Map<String, Object> root = new HashMap<>();
	root.put("event", event);
	root.put("instance", instance);
	root.put("lastStatus", getLastStatus(event.getInstance()));
	StandardEvaluationContext context = new StandardEvaluationContext(root);
	context.addPropertyAccessor(new MapAccessor());
	return description.getValue(context, String.class);
}
 
Example #7
Source File: DiscordNotifier.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String createContent(InstanceEvent event, Instance instance) {
	Map<String, Object> root = new HashMap<>();
	root.put("event", event);
	root.put("instance", instance);
	root.put("lastStatus", getLastStatus(event.getInstance()));
	StandardEvaluationContext context = new StandardEvaluationContext(root);
	context.addPropertyAccessor(new MapAccessor());
	return message.getValue(context, String.class);
}
 
Example #8
Source File: LetsChatNotifier.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getText(InstanceEvent event, Instance instance) {
	Map<String, Object> root = new HashMap<>();
	root.put("event", event);
	root.put("instance", instance);
	root.put("lastStatus", getLastStatus(event.getInstance()));
	StandardEvaluationContext context = new StandardEvaluationContext(root);
	context.addPropertyAccessor(new MapAccessor());
	return message.getValue(context, String.class);
}
 
Example #9
Source File: SlackNotifier.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getText(InstanceEvent event, Instance instance) {
	Map<String, Object> root = new HashMap<>();
	root.put("event", event);
	root.put("instance", instance);
	root.put("lastStatus", getLastStatus(event.getInstance()));
	StandardEvaluationContext context = new StandardEvaluationContext(root);
	context.addPropertyAccessor(new MapAccessor());
	return message.getValue(context, String.class);
}
 
Example #10
Source File: HipchatNotifier.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getMessage(InstanceEvent event, Instance instance) {
	Map<String, Object> root = new HashMap<>();
	root.put("event", event);
	root.put("instance", instance);
	root.put("lastStatus", getLastStatus(event.getInstance()));
	StandardEvaluationContext context = new StandardEvaluationContext(root);
	context.addPropertyAccessor(new MapAccessor());
	return description.getValue(context, String.class);
}
 
Example #11
Source File: TelegramNotifier.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getText(InstanceEvent event, Instance instance) {
	Map<String, Object> root = new HashMap<>();
	root.put("event", event);
	root.put("instance", instance);
	root.put("lastStatus", getLastStatus(event.getInstance()));
	StandardEvaluationContext context = new StandardEvaluationContext(root);
	context.addPropertyAccessor(new MapAccessor());
	return message.getValue(context, String.class);
}
 
Example #12
Source File: PagerdutyNotifier.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getDescription(InstanceEvent event, Instance instance) {
	Map<String, Object> root = new HashMap<>();
	root.put("event", event);
	root.put("instance", instance);
	root.put("lastStatus", getLastStatus(event.getInstance()));
	StandardEvaluationContext context = new StandardEvaluationContext(root);
	context.addPropertyAccessor(new MapAccessor());

	return description.getValue(context, String.class);
}
 
Example #13
Source File: SpelMailTemplateEngine.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public MailMessage create(MailTemplate mailTemplate, MailSource source) {
    MailMessageImpl.Builder b = MailMessageImpl.builder();
    final StandardEvaluationContext ctx = new StandardEvaluationContext(source.getVariables());
    ctx.addPropertyAccessor(new MapAccessor());
    UnaryOperator<Object> processor = (o) -> MailTemplateUtils.process((s) -> evaluate(ctx, s), o);
    b.setHead(MailHeadImpl.builder().from(mailTemplate.getHeadSource(), processor).build());
    MailPartTemplate bs = mailTemplate.getBodySource();
    String bodyText = (String) evaluate(ctx, bs.getData());
    b.setBody(new MailTextBody(bodyText, bs.getMime()));
    return b.build();
}
 
Example #14
Source File: HipchatNotifier.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getMessage(InstanceEvent event, Instance instance) {
    Map<String, Object> root = new HashMap<>();
    root.put("event", event);
    root.put("instance", instance);
    root.put("lastStatus", getLastStatus(event.getInstance()));
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.addPropertyAccessor(new MapAccessor());
    return description.getValue(context, String.class);
}
 
Example #15
Source File: EvalTag.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #16
Source File: EvalTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #17
Source File: EvalTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #18
Source File: SpringELTest.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void projection() throws Exception {
  StandardEvaluationContext context = new StandardEvaluationContext();
  context.setTypeLocator(new CommonTypeLocator());
  context.addPropertyAccessor(new MapAccessor());
  context.setVariable("content",
      "{\"blocks\":[{\"data\":{},\"depth\":0,\"entityRanges\":[],\"inlineStyleRanges\":[],\"key\":\"ummxd\",\"text\":\"Test\",\"type\":\"unstyled\"}],\"entityMap\":{}}");
  String value = parser
      .parseExpression("T(SyncUtil).fromJson(#content,T(Map))['blocks'].![text]")
      .getValue(context, String.class);
  Assert.assertEquals(value, "Test");
}
 
Example #19
Source File: SpelProvider.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public Object evaluate(final Map<String, Object> context) {
    if (context == null || expression == null) {
        return null;
    }
    StandardEvaluationContext sec = new StandardEvaluationContext(context);
    sec.addPropertyAccessor(new MapAccessor());
    return expression.getValue(sec);
}
 
Example #20
Source File: EvalTag.java    From java-technology-stack with MIT License 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #21
Source File: OpsGenieNotifier.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getMessage(InstanceEvent event, Instance instance) {
    Map<String, Object> root = new HashMap<>();
    root.put("event", event);
    root.put("instance", instance);
    root.put("lastStatus", getLastStatus(event.getInstance()));
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.addPropertyAccessor(new MapAccessor());
    return description.getValue(context, String.class);
}
 
Example #22
Source File: DiscordNotifier.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String createContent(InstanceEvent event, Instance instance) {
    Map<String, Object> root = new HashMap<>();
    root.put("event", event);
    root.put("instance", instance);
    root.put("lastStatus", getLastStatus(event.getInstance()));
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.addPropertyAccessor(new MapAccessor());
    return message.getValue(context, String.class);
}
 
Example #23
Source File: LetsChatNotifier.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getText(InstanceEvent event, Instance instance) {
    Map<String, Object> root = new HashMap<>();
    root.put("event", event);
    root.put("instance", instance);
    root.put("lastStatus", getLastStatus(event.getInstance()));
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.addPropertyAccessor(new MapAccessor());
    return message.getValue(context, String.class);
}
 
Example #24
Source File: SlackNotifier.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getText(InstanceEvent event, Instance instance) {
    Map<String, Object> root = new HashMap<>();
    root.put("event", event);
    root.put("instance", instance);
    root.put("lastStatus", getLastStatus(event.getInstance()));
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.addPropertyAccessor(new MapAccessor());
    return message.getValue(context, String.class);
}
 
Example #25
Source File: RoutingFunction.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
public RoutingFunction(FunctionCatalog functionCatalog, FunctionInspector functionInspector, FunctionProperties functionProperties) {
	this.functionCatalog = functionCatalog;
	this.functionProperties = functionProperties;
	this.functionInspector = functionInspector;
	this.evalContext.addPropertyAccessor(new MapAccessor());
}
 
Example #26
Source File: SpelMessageInterpolator.java    From spring-rest-exception-handler with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new instance with {@link StandardEvaluationContext} including
 * {@link org.springframework.expression.spel.support.ReflectivePropertyAccessor ReflectivePropertyAccessor}
 * and {@link MapAccessor}.
 */
public SpelMessageInterpolator() {
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.addPropertyAccessor(new MapAccessor());
    this.evalContext = ctx;
}