ARFOGateway: Directory LiteralExpression->Value...
Using a `LiteralExpression` in the `AbstractRemoteFileOutboundGateway` (when the user provides a `File` for the directory) causes us to create a new `File` object each time, instead of using the user-supplied `File`. Use a `ValueExpression` instead. Move the `FileWritingMessageHandler` logic to `ExpressionUtils` and use it from both places. Also add a String setter variant for convenient Java Configuration.
This commit is contained in:
committed by
Artem Bilan
parent
d5c633d703
commit
f751398cfe
@@ -767,35 +767,8 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
|
||||
}
|
||||
|
||||
private File evaluateDestinationDirectoryExpression(Message<?> message) {
|
||||
|
||||
final File destinationDirectory;
|
||||
|
||||
final Object destinationDirectoryToUse = this.destinationDirectoryExpression.getValue(
|
||||
this.evaluationContext, message);
|
||||
|
||||
if (destinationDirectoryToUse == null) {
|
||||
throw new IllegalStateException(String.format("The provided " +
|
||||
"destinationDirectoryExpression (%s) must not resolve to null.",
|
||||
this.destinationDirectoryExpression.getExpressionString()));
|
||||
}
|
||||
else if (destinationDirectoryToUse instanceof String) {
|
||||
|
||||
final String destinationDirectoryPath = (String) destinationDirectoryToUse;
|
||||
|
||||
Assert.hasText(destinationDirectoryPath, String.format(
|
||||
"Unable to resolve destination directory name for the provided Expression '%s'.",
|
||||
this.destinationDirectoryExpression.getExpressionString()));
|
||||
destinationDirectory = new File(destinationDirectoryPath);
|
||||
}
|
||||
else if (destinationDirectoryToUse instanceof File) {
|
||||
destinationDirectory = (File) destinationDirectoryToUse;
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException(String.format("The provided " +
|
||||
"destinationDirectoryExpression (%s) must be of type " +
|
||||
"java.io.File or be a String.", this.destinationDirectoryExpression.getExpressionString()));
|
||||
}
|
||||
|
||||
final File destinationDirectory = ExpressionUtils.expressionToFile(this.destinationDirectoryExpression,
|
||||
this.evaluationContext, message, "Destination Directory");
|
||||
validateDestinationDirectory(destinationDirectory, this.autoCreateDirectory);
|
||||
return destinationDirectory;
|
||||
}
|
||||
|
||||
@@ -33,11 +33,11 @@ import java.util.Set;
|
||||
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.integration.IntegrationMessageHeaderAccessor;
|
||||
import org.springframework.integration.expression.ExpressionUtils;
|
||||
import org.springframework.integration.expression.FunctionExpression;
|
||||
import org.springframework.integration.expression.ValueExpression;
|
||||
import org.springframework.integration.file.FileHeaders;
|
||||
import org.springframework.integration.file.filters.FileListFilter;
|
||||
import org.springframework.integration.file.remote.AbstractFileInfo;
|
||||
@@ -358,18 +358,29 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
*/
|
||||
public void setLocalDirectory(File localDirectory) {
|
||||
if (localDirectory != null) {
|
||||
this.localDirectoryExpression = new LiteralExpression(localDirectory.getAbsolutePath());
|
||||
this.localDirectoryExpression = new ValueExpression<>(localDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a SpEL expression to evaluate directory path where remote files will be transferred to.
|
||||
* Specify a SpEL expression to evaluate the directory path to which remote files will
|
||||
* be transferred.
|
||||
* @param localDirectoryExpression the SpEL to determine the local directory.
|
||||
*/
|
||||
public void setLocalDirectoryExpression(Expression localDirectoryExpression) {
|
||||
this.localDirectoryExpression = localDirectoryExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a SpEL expression to evaluate the directory path to which remote files will
|
||||
* be transferred.
|
||||
* @param localDirectoryExpression the SpEL to determine the local directory.
|
||||
* @since 5.0
|
||||
*/
|
||||
public void setLocalDirectoryExpressionString(String localDirectoryExpression) {
|
||||
this.localDirectoryExpression = EXPRESSION_PARSER.parseExpression(localDirectoryExpression);
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@code boolean} flag to identify if local directory should be created automatically.
|
||||
* Defaults to {@code true}.
|
||||
@@ -496,8 +507,8 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
if ((Command.GET.equals(this.command) && !this.options.contains(Option.STREAM))
|
||||
|| Command.MGET.equals(this.command)) {
|
||||
Assert.notNull(this.localDirectoryExpression, "localDirectory must not be null");
|
||||
if (this.localDirectoryExpression instanceof LiteralExpression) {
|
||||
File localDirectory = new File(this.localDirectoryExpression.getExpressionString());
|
||||
if (this.localDirectoryExpression instanceof ValueExpression) {
|
||||
File localDirectory = this.localDirectoryExpression.getValue(File.class);
|
||||
try {
|
||||
if (!localDirectory.exists()) {
|
||||
if (this.autoCreateLocalDirectory) {
|
||||
@@ -1059,10 +1070,8 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
if (remoteDirectory != null) {
|
||||
evaluationContext.setVariable("remoteDirectory", remoteDirectory);
|
||||
}
|
||||
//TODO see org.springframework.integration.context.CustomConversionServiceFactoryBean
|
||||
// File localDir = this.localDirectoryExpression.getValue(evaluationContext, message, File.class);
|
||||
String localDirPath = this.localDirectoryExpression.getValue(evaluationContext, message, String.class);
|
||||
File localDir = new File(localDirPath);
|
||||
File localDir = ExpressionUtils.expressionToFile(this.localDirectoryExpression, evaluationContext, message,
|
||||
"Local Directory");
|
||||
if (!localDir.exists()) {
|
||||
Assert.isTrue(localDir.mkdirs(), "Failed to make local directory: " + localDir);
|
||||
}
|
||||
|
||||
@@ -135,7 +135,8 @@ public class FileOutboundChannelAdapterIntegrationTests {
|
||||
this.inputChannelSaveToSubDirEmptyStringExpression.send(message);
|
||||
}
|
||||
catch (MessageHandlingException e) {
|
||||
Assert.assertEquals("Unable to resolve destination directory name for the provided Expression '' ''.", e.getCause().getMessage());
|
||||
Assert.assertEquals("Unable to resolve Destination Directory for the provided Expression '' ''.",
|
||||
e.getCause().getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -192,8 +193,8 @@ public class FileOutboundChannelAdapterIntegrationTests {
|
||||
this.inputChannelSaveToSubDirWithFile.send(messageWithFileHeader);
|
||||
}
|
||||
catch (MessageHandlingException e) {
|
||||
Assert.assertEquals("The provided destinationDirectoryExpression " +
|
||||
"(headers['subDirectory']) must not resolve to null.",
|
||||
Assert.assertEquals("The provided Destination Directory expression " +
|
||||
"(headers['subDirectory']) must not evaluate to null.",
|
||||
e.getCause().getMessage());
|
||||
|
||||
return;
|
||||
@@ -214,9 +215,9 @@ public class FileOutboundChannelAdapterIntegrationTests {
|
||||
this.inputChannelSaveToSubDirWithFile.send(messageWithFileHeader);
|
||||
}
|
||||
catch (MessageHandlingException e) {
|
||||
Assert.assertEquals("The provided destinationDirectoryExpression" +
|
||||
" (headers['subDirectory']) must be of type " +
|
||||
"java.io.File or be a String.",
|
||||
Assert.assertEquals("The provided Destination Directory expression" +
|
||||
" (headers['subDirectory']) must evaluate to type " +
|
||||
"java.io.File or String, not java.lang.Integer.",
|
||||
e.getCause().getMessage());
|
||||
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user