diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileOutboundGatewayParser.java b/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileOutboundGatewayParser.java index 6f2c0c2b84..0a486a1641 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileOutboundGatewayParser.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileOutboundGatewayParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -58,9 +58,7 @@ public abstract class AbstractRemoteFileOutboundGatewayParser extends AbstractCo } else { builder.addConstructorArgValue(element.getAttribute("command")); - if (element.hasAttribute(EXPRESSION_ATTRIBUTE)) { - builder.addConstructorArgValue(element.getAttribute(EXPRESSION_ATTRIBUTE)); - } + builder.addConstructorArgValue(element.getAttribute(EXPRESSION_ATTRIBUTE)); } IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "command-options", "options"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "reply-timeout", "sendTimeout"); diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java index deb458534b..1c141dc5cd 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java @@ -187,21 +187,23 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply * 'get' etc), and an expression to determine the filename. * @param remoteFileTemplate the remote file template. * @param command the command. - * @param expression the filename expression. + * @param expressionArg the filename expression. */ public AbstractRemoteFileOutboundGateway(RemoteFileTemplate remoteFileTemplate, Command command, - @Nullable String expression) { + @Nullable String expressionArg) { Assert.notNull(remoteFileTemplate, "'remoteFileTemplate' cannot be null"); this.remoteFileTemplate = remoteFileTemplate; this.command = command; - if (expression == null) { - Assert.state(Command.LS.equals(this.command) - || Command.NLST.equals(this.command) - || Command.PUT.equals(this.command) - || Command.MPUT.equals(this.command), - "Only LS, NLST, PUT and MPUT commands can rely on the working directory.\n" + - "All other commands must be supplied with the filename expression"); + String expression = expressionArg; + boolean expressionNeeded = !(Command.LS.equals(this.command) + || Command.NLST.equals(this.command) + || Command.PUT.equals(this.command) + || Command.MPUT.equals(this.command)); + if (!StringUtils.hasText(expression) && expressionNeeded) { + expression = "payload"; + } + if (!StringUtils.hasText(expression)) { this.fileNameProcessor = null; } else { diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpOutboundGatewayParserTests-context.xml b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpOutboundGatewayParserTests-context.xml index 2df2601ec7..8a833ed79e 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpOutboundGatewayParserTests-context.xml +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpOutboundGatewayParserTests-context.xml @@ -113,4 +113,26 @@ + + + + + + diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpOutboundGatewayParserTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpOutboundGatewayParserTests.java index e4a3ab327b..bf6011053c 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpOutboundGatewayParserTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpOutboundGatewayParserTests.java @@ -69,6 +69,15 @@ public class SftpOutboundGatewayParserTests { @Autowired AbstractEndpoint advised; + @Autowired + AbstractEndpoint noExpressionLS; + + @Autowired + AbstractEndpoint noExpressionPUT; + + @Autowired + AbstractEndpoint noExpressionGET; + @Autowired FileNameGenerator generator; @@ -165,6 +174,14 @@ public class SftpOutboundGatewayParserTests { assertThat(adviceCalled).isEqualTo(1); } + @Test + void noExpression() { + assertThat(TestUtils.getPropertyValue(this.noExpressionLS, "handler.fileNameProcessor")).isNull(); + assertThat(TestUtils.getPropertyValue(this.noExpressionPUT, "handler.fileNameProcessor")).isNull(); + assertThat(TestUtils.getPropertyValue(this.noExpressionGET, + "handler.fileNameProcessor.expression.expression")).isEqualTo("payload"); + } + public static class FooAdvice extends AbstractRequestHandlerAdvice { @Override