INT-4344: AbstRemoteFileOutGw: Add assert for NPE

JIRA: https://jira.spring.io/browse/INT-4344

The `FtpOutboundGateway` provides ctors without expression for remote path.
In this case it is treated as a `working directory` but only for the
`LS`, `NLST`, `PUT` and `MPUT` commands.

* Add assertion in the `AbstractRemoteFileOutboundGateway` to discard
configuration for all other commands when `expression` is `null`

**Cherry-pick to 4.3.x**

Conflicts:
	spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java
Resolved.
This commit is contained in:
Artem Bilan
2017-09-20 16:10:19 -04:00
committed by Gary Russell
parent 6ec34c114c
commit 7d24b8ec67
4 changed files with 34 additions and 18 deletions

View File

@@ -307,11 +307,20 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
Assert.notNull(remoteFileTemplate, "'remoteFileTemplate' cannot be null");
this.remoteFileTemplate = remoteFileTemplate;
this.command = command;
Expression parsedExpression = new SpelExpressionParser().parseExpression(expression);
this.fileNameProcessor = new ExpressionEvaluatingMessageProcessor<String>(
parsedExpression);
if (expression == null) {
Assert.state(Command.LS.equals(this.command)
|| Command.PUT.equals(this.command)
|| Command.MPUT.equals(this.command),
"Only LS, PUT and MPUT commands can rely on the working directory.\n" +
"All other commands must be supplied with the filename expression");
this.fileNameProcessor = null;
}
else {
Expression parsedExpression = new SpelExpressionParser().parseExpression(expression);
this.fileNameProcessor = new ExpressionEvaluatingMessageProcessor<String>(parsedExpression);
setPrimaryExpression(parsedExpression);
}
this.messageSessionCallback = null;
setPrimaryExpression(parsedExpression);
}
/**
@@ -503,10 +512,14 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
"Cannot use " + Option.SUBDIRS.toString() + " when using 'mget' use "
+ Option.RECURSIVE.toString() + " to obtain files in subdirectories");
}
if (this.fileNameProcessor != null && getBeanFactory() != null) {
this.fileNameProcessor.setBeanFactory(this.getBeanFactory());
this.renameProcessor.setBeanFactory(this.getBeanFactory());
this.remoteFileTemplate.setBeanFactory(this.getBeanFactory());
if (getBeanFactory() != null) {
if (this.fileNameProcessor != null) {
this.fileNameProcessor.setBeanFactory(getBeanFactory());
}
this.renameProcessor.setBeanFactory(getBeanFactory());
this.remoteFileTemplate.setBeanFactory(getBeanFactory());
}
}
@@ -542,7 +555,9 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
}
private Object doLs(Message<?> requestMessage) {
String dir = this.fileNameProcessor.processMessage(requestMessage);
String dir = this.fileNameProcessor != null
? this.fileNameProcessor.processMessage(requestMessage)
: null;
if (dir != null && !dir.endsWith(this.remoteFileTemplate.getRemoteFileSeparator())) {
dir += this.remoteFileTemplate.getRemoteFileSeparator();
}

View File

@@ -821,7 +821,7 @@ public class RemoteFileOutboundGatewayTests {
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "put", null);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "put", "payload");
FileTransferringMessageHandler<TestLsEntry> handler = new FileTransferringMessageHandler<TestLsEntry>(sessionFactory);
handler.setRemoteDirectoryExpressionString("'foo/'");
handler.setBeanFactory(mock(BeanFactory.class));
@@ -856,7 +856,7 @@ public class RemoteFileOutboundGatewayTests {
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "put", null);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "put", "payload");
FileTransferringMessageHandler<TestLsEntry> handler = new FileTransferringMessageHandler<TestLsEntry>(sessionFactory);
handler.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
handler.setBeanFactory(mock(BeanFactory.class));
@@ -917,7 +917,7 @@ public class RemoteFileOutboundGatewayTests {
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "mput", null);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "mput", "payload");
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(session);
final AtomicReference<String> written = new AtomicReference<String>();