Java Code Examples for org.springframework.context.expression.MapAccessor

The following examples show how to use org.springframework.context.expression.MapAccessor. These examples are extracted from open source projects. 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
@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
@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 3
/**
 * 创建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 4
Source Project: spring-analysis-note   Source File: EvalTag.java    License: 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 5
Source Project: Moss   Source File: PagerdutyNotifier.java    License: 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 Project: Moss   Source File: TelegramNotifier.java    License: 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 7
Source Project: Moss   Source File: HipchatNotifier.java    License: 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 8
Source Project: Moss   Source File: SlackNotifier.java    License: 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 Project: Moss   Source File: LetsChatNotifier.java    License: 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 Project: Moss   Source File: DiscordNotifier.java    License: 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 11
Source Project: Moss   Source File: OpsGenieNotifier.java    License: 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 12
Source Project: java-technology-stack   Source File: EvalTag.java    License: 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 13
Source Project: joyrpc   Source File: SpelProvider.java    License: 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 14
Source Project: syncer   Source File: SpringELTest.java    License: 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 15
Source Project: lams   Source File: EvalTag.java    License: 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 16
Source Project: spring4-understanding   Source File: EvalTag.java    License: 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 Project: haven-platform   Source File: SpelMailTemplateEngine.java    License: 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 18
Source Project: spring-boot-admin   Source File: PagerdutyNotifier.java    License: 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 19
Source Project: spring-boot-admin   Source File: TelegramNotifier.java    License: 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 20
Source Project: spring-boot-admin   Source File: HipchatNotifier.java    License: 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 21
Source Project: spring-boot-admin   Source File: SlackNotifier.java    License: 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 22
Source Project: spring-boot-admin   Source File: LetsChatNotifier.java    License: 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 23
Source Project: spring-boot-admin   Source File: DiscordNotifier.java    License: 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 24
Source Project: spring-boot-admin   Source File: OpsGenieNotifier.java    License: 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 25
Source Project: spring-cloud-function   Source File: RoutingFunction.java    License: 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
/**
 * 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;
}