diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/advice/ExpressionEvaluatingRequestHandlerAdvice.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/advice/ExpressionEvaluatingRequestHandlerAdvice.java
index ea14ec3528..5ae8ea7ef2 100644
--- a/spring-integration-core/src/main/java/org/springframework/integration/handler/advice/ExpressionEvaluatingRequestHandlerAdvice.java
+++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/advice/ExpressionEvaluatingRequestHandlerAdvice.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,17 +16,21 @@
package org.springframework.integration.handler.advice;
+import org.springframework.beans.factory.BeanFactory;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
-import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.expression.ExpressionUtils;
+import org.springframework.integration.expression.FunctionExpression;
import org.springframework.integration.message.AdviceMessage;
+import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessagingException;
+import org.springframework.messaging.core.DestinationResolver;
import org.springframework.messaging.support.ErrorMessage;
+import org.springframework.util.StringUtils;
/**
* Used to advise {@link org.springframework.messaging.MessageHandler}s.
@@ -36,53 +40,61 @@ import org.springframework.messaging.support.ErrorMessage;
* containing the evaluation result in its payload and the {@code inputMessage} property containing
* the original message that was sent to the endpoint.
* The failure expression is NOT evaluated if the success expression throws an exception.
+ *
+ * When expressions are not configured, but channels are, the default expression is evaluated
+ * just into a {@code payload} from the message.
*
* @author Gary Russell
* @author Artem Bilan
+ *
* @since 2.2
*
*/
public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHandlerAdvice {
- private volatile Expression onSuccessExpression;
-
- private volatile MessageChannel successChannel;
-
- private volatile String successChannelName;
-
- private volatile Expression onFailureExpression;
-
- private volatile MessageChannel failureChannel;
-
- private volatile String failureChannelName;
+ private static final Expression DEFAULT_EXPRESSION = new FunctionExpression>(Message::getPayload);
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
- private volatile boolean trapException = false;
+ private Expression onSuccessExpression;
- private volatile boolean returnFailureExpressionResult = false;
+ private MessageChannel successChannel;
- private volatile boolean propagateOnSuccessEvaluationFailures;
+ private String successChannelName;
- private volatile EvaluationContext evaluationContext;
+ private Expression onFailureExpression;
+
+ private MessageChannel failureChannel;
+
+ private String failureChannelName;
+
+ private boolean trapException = false;
+
+ private boolean returnFailureExpressionResult = false;
+
+ private boolean propagateOnSuccessEvaluationFailures;
+
+ private EvaluationContext evaluationContext;
/**
* Set the expression to evaluate against the message after a successful
* handler invocation.
+ * Defaults to {@code payload}, if {@code successChannel} is configured.
* @param onSuccessExpression the SpEL expression.
* @since 4.3.7
*/
public void setOnSuccessExpressionString(String onSuccessExpression) {
- this.onSuccessExpression = new SpelExpressionParser().parseExpression(onSuccessExpression);
+ setOnSuccessExpression(EXPRESSION_PARSER.parseExpression(onSuccessExpression));
}
/**
* Set the expression to evaluate against the message after a successful
* handler invocation.
+ * Defaults to {@code payload}, if {@code successChannel} is configured.
* @param onSuccessExpression the SpEL expression.
* @since 5.0
*/
- public void setOnSuccessExpression(Expression onSuccessExpression) {
+ public void setOnSuccessExpression(@Nullable Expression onSuccessExpression) {
this.onSuccessExpression = onSuccessExpression;
}
@@ -94,26 +106,28 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
*/
@Deprecated
public void setExpressionOnSuccess(Expression onSuccessExpression) {
- this.onSuccessExpression = onSuccessExpression;
+ setOnSuccessExpression(onSuccessExpression);
}
/**
* Set the expression to evaluate against the root message after a failed
- * handler invocation. The exception is available as the variable {@code #exception}
+ * handler invocation. The exception is available as the variable {@code #exception}.
+ * Defaults to {@code payload}, if {@code failureChannel} is configured.
* @param onFailureExpression the SpEL expression.
* @since 4.3.7
*/
public void setOnFailureExpressionString(String onFailureExpression) {
- this.onFailureExpression = new SpelExpressionParser().parseExpression(onFailureExpression);
+ setOnFailureExpression(EXPRESSION_PARSER.parseExpression(onFailureExpression));
}
/**
* Set the expression to evaluate against the root message after a failed
- * handler invocation. The exception is available as the variable {@code #exception}
+ * handler invocation. The exception is available as the variable {@code #exception}.
+ * Defaults to {@code payload}, if {@code failureChannel} is configured.
* @param onFailureExpression the SpEL expression.
* @since 5.0
*/
- public void setOnFailureExpression(Expression onFailureExpression) {
+ public void setOnFailureExpression(@Nullable Expression onFailureExpression) {
this.onFailureExpression = onFailureExpression;
}
@@ -125,7 +139,7 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
*/
@Deprecated
public void setExpressionOnFailure(Expression onFailureExpression) {
- this.onFailureExpression = onFailureExpression;
+ setOnFailureExpression(onFailureExpression);
}
/**
@@ -178,7 +192,6 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
/**
* If true, the result of evaluating the onFailureExpression will
* be returned as the result of AbstractReplyProducingMessageHandler.handleRequestMessage(Message).
- *
* @param returnFailureExpressionResult true to return the result of the evaluation.
*/
public void setReturnFailureExpressionResult(boolean returnFailureExpressionResult) {
@@ -200,24 +213,39 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
@Override
protected void onInit() {
super.onInit();
- if (this.getBeanFactory() != null) {
- this.messagingTemplate.setBeanFactory(this.getBeanFactory());
+ BeanFactory beanFactory = getBeanFactory();
+ if (beanFactory != null) {
+ this.messagingTemplate.setBeanFactory(beanFactory);
+ }
+
+ if (this.onSuccessExpression == null
+ && (this.successChannel != null || StringUtils.hasText(this.successChannelName))) {
+
+ this.onSuccessExpression = DEFAULT_EXPRESSION;
+ }
+
+ if (this.onFailureExpression == null
+ && (this.failureChannel != null || StringUtils.hasText(this.failureChannelName))) {
+
+ this.onFailureExpression = DEFAULT_EXPRESSION;
}
}
@Override
- protected Object doInvoke(ExecutionCallback callback, Object target, Message> message) throws Exception {
+ protected Object doInvoke(ExecutionCallback callback, Object target, Message> message)
+ throws Exception { // NOSONAR
+
try {
Object result = callback.execute();
if (this.onSuccessExpression != null) {
- this.evaluateSuccessExpression(message);
+ evaluateSuccessExpression(message);
}
return result;
}
catch (Exception e) {
- Exception actualException = this.unwrapExceptionIfNecessary(e);
+ Exception actualException = unwrapExceptionIfNecessary(e);
if (this.onFailureExpression != null) {
- Object evalResult = this.evaluateFailureExpression(message, actualException);
+ Object evalResult = evaluateFailureExpression(message, actualException);
if (this.returnFailureExpressionResult) {
return evalResult;
}
@@ -229,68 +257,69 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
}
}
- private void evaluateSuccessExpression(Message> message) throws Exception {
+ private void evaluateSuccessExpression(Message> message) throws Exception { // NOSONAR
Object evalResult;
- boolean evaluationFailed = false;
try {
- evalResult = this.onSuccessExpression.getValue(this.prepareEvaluationContextToUse(null), message);
+ evalResult = this.onSuccessExpression.getValue(prepareEvaluationContextToUse(null), message);
}
catch (Exception e) {
evalResult = e;
- evaluationFailed = true;
}
- if (this.successChannel == null && this.successChannelName != null && getChannelResolver() != null) {
- this.successChannel = getChannelResolver().resolveDestination(this.successChannelName);
+ DestinationResolver channelResolver = getChannelResolver();
+ if (this.successChannel == null && this.successChannelName != null && channelResolver != null) {
+ this.successChannel = channelResolver.resolveDestination(this.successChannelName);
}
if (evalResult != null && this.successChannel != null) {
- AdviceMessage> resultMessage = new AdviceMessage