INT-3746: (S)FTP outbound: support InputStream

JIRA: https://jira.spring.io/browse/INT-3746
This commit is contained in:
Artem Bilan
2017-03-03 15:46:35 -05:00
committed by Gary Russell
parent 4403519e5f
commit 56708b448c
3 changed files with 26 additions and 17 deletions

View File

@@ -443,7 +443,10 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
Object payload = message.getPayload();
InputStream dataInputStream = null;
String name = null;
if (payload instanceof File) {
if (payload instanceof InputStream) {
dataInputStream = (InputStream) payload;
}
else if (payload instanceof File) {
File inputFile = (File) payload;
if (inputFile.exists()) {
dataInputStream = new BufferedInputStream(new FileInputStream(inputFile));
@@ -464,7 +467,7 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
}
else {
throw new IllegalArgumentException("Unsupported payload type. The only supported payloads are " +
"java.io.File, java.lang.String, and byte[]");
"java.io.File, java.lang.String, byte[] and InputStream");
}
if (dataInputStream == null) {
return null;

View File

@@ -24,8 +24,10 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.regex.Matcher;
@@ -79,8 +81,8 @@ public class FtpTests extends FtpTestSupport {
.localFilename(f -> f.toUpperCase() + ".a")
.localDirectory(getTargetLocalDirectory()),
e -> e.id("ftpInboundAdapter").poller(Pollers.fixedDelay(100)))
.channel(out)
.get();
.channel(out)
.get();
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
Message<?> message = out.receive(10_000);
assertNotNull(message);
@@ -119,8 +121,8 @@ public class FtpTests extends FtpTestSupport {
.remoteDirectory("ftpSource")
.regexFilter(".*\\.txt$"),
e -> e.id("ftpInboundAdapter").poller(Pollers.fixedDelay(100)))
.channel(out)
.get();
.channel(out)
.get();
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
Message<?> message = out.receive(10_000);
assertNotNull(message);
@@ -140,15 +142,17 @@ public class FtpTests extends FtpTestSupport {
@Test
public void testFtpOutboundFlow() {
IntegrationFlow flow = f -> f
.handle(Ftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL)
.useTemporaryFileName(false)
.fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
.remoteDirectory("ftpTarget"));
.handle(Ftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL)
.useTemporaryFileName(false)
.fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
.remoteDirectory("ftpTarget"));
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
String fileName = "foo.file";
registration.getInputChannel().send(MessageBuilder.withPayload("foo")
.setHeader(FileHeaders.FILENAME, fileName)
.build());
Message<ByteArrayInputStream> message = MessageBuilder
.withPayload(new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8)))
.setHeader(FileHeaders.FILENAME, fileName)
.build();
registration.getInputChannel().send(message);
RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<>(sessionFactory());
FTPFile[] files = template.execute(session ->
session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
@@ -165,10 +169,10 @@ public class FtpTests extends FtpTestSupport {
IntegrationFlow flow = f -> f
.handle(Ftp.outboundGateway(sessionFactory(),
AbstractRemoteFileOutboundGateway.Command.MGET, "payload")
.options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE)
.filterExpression("name matches 'subFtpSource|.*1.txt'")
.localDirectoryExpression("'" + getTargetLocalDirectoryName() + "' + #remoteDirectory")
.localFilenameExpression("#remoteFileName.replaceFirst('ftpSource', 'localTarget')"))
.options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE)
.filterExpression("name matches 'subFtpSource|.*1.txt'")
.localDirectoryExpression("'" + getTargetLocalDirectoryName() + "' + #remoteDirectory")
.localFilenameExpression("#remoteFileName.replaceFirst('ftpSource', 'localTarget')"))
.channel(out);
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
String dir = "ftpSource/";

View File

@@ -99,6 +99,8 @@ See <<ftp-outbound-gateway>> and <<sftp-outbound-gateway>> for more information.
The (S)FTP streaming inbound channel adapters now add remote file information in a message header.
See <<ftp-streaming>> and <<sftp-streaming>> for more information.
The FTP and SFTP outbound channel adapters, as well as `PUT` command of the outbound gateways, now support `InputStream` as `payload`, too.
==== Integration Properties
Since _version 4.3.2_ a new `spring.integration.readOnly.headers` global property has been added to customize the list of headers which should not be copied to a newly created `Message` by the `MessageBuilder`.