use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration-samples by spring-projects.
the class SftpOutboundTransferSample method testOutbound.
@Test
public void testOutbound() throws Exception {
final String sourceFileName = "README.md";
final String destinationFileName = sourceFileName + "_foo";
final ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/spring/integration/SftpOutboundTransferSample-context.xml", SftpOutboundTransferSample.class);
@SuppressWarnings("unchecked") SessionFactory<LsEntry> sessionFactory = ac.getBean(CachingSessionFactory.class);
RemoteFileTemplate<LsEntry> template = new RemoteFileTemplate<LsEntry>(sessionFactory);
// Just the directory
SftpTestUtils.createTestFiles(template);
try {
final File file = new File(sourceFileName);
Assert.isTrue(file.exists(), String.format("File '%s' does not exist.", sourceFileName));
final Message<File> message = MessageBuilder.withPayload(file).build();
final MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);
inputChannel.send(message);
Thread.sleep(2000);
Assert.isTrue(SftpTestUtils.fileExists(template, destinationFileName), String.format("File '%s' does not exist.", destinationFileName));
System.out.println(String.format("Successfully transferred '%s' file to a " + "remote location under the name '%s'", sourceFileName, destinationFileName));
} finally {
SftpTestUtils.cleanUp(template, destinationFileName);
ac.close();
}
}
use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testPutExists.
@Test
public void testPutExists() throws Exception {
@SuppressWarnings("unchecked") SessionFactory<TestLsEntry> sessionFactory = mock(SessionFactory.class);
@SuppressWarnings("unchecked") Session<TestLsEntry> session = mock(Session.class);
RemoteFileTemplate<TestLsEntry> template = new RemoteFileTemplate<TestLsEntry>(sessionFactory) {
@Override
public boolean exists(String path) {
return true;
}
};
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "put", "payload");
FileTransferringMessageHandler<TestLsEntry> handler = new FileTransferringMessageHandler<TestLsEntry>(sessionFactory);
handler.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(session);
Message<String> requestMessage = MessageBuilder.withPayload("hello").setHeader(FileHeaders.FILENAME, "bar.txt").build();
// default (null) == REPLACE
String path = (String) gw.handleRequestMessage(requestMessage);
assertEquals("foo/bar.txt", path);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(session).write(any(InputStream.class), captor.capture());
assertEquals("foo/bar.txt.writing", captor.getValue());
verify(session).rename("foo/bar.txt.writing", "foo/bar.txt");
gw.setFileExistsMode(FileExistsMode.FAIL);
try {
path = (String) gw.handleRequestMessage(requestMessage);
fail("Exception expected");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("The destination file already exists"));
}
gw.setFileExistsMode(FileExistsMode.REPLACE);
path = (String) gw.handleRequestMessage(requestMessage);
assertEquals("foo/bar.txt", path);
captor = ArgumentCaptor.forClass(String.class);
verify(session, times(2)).write(any(InputStream.class), captor.capture());
assertEquals("foo/bar.txt.writing", captor.getValue());
verify(session, times(2)).rename("foo/bar.txt.writing", "foo/bar.txt");
gw.setFileExistsMode(FileExistsMode.APPEND);
path = (String) gw.handleRequestMessage(requestMessage);
assertEquals("foo/bar.txt", path);
captor = ArgumentCaptor.forClass(String.class);
verify(session).append(any(InputStream.class), captor.capture());
assertEquals("foo/bar.txt", captor.getValue());
gw.setFileExistsMode(FileExistsMode.IGNORE);
path = (String) gw.handleRequestMessage(requestMessage);
assertEquals("foo/bar.txt", path);
// no more writes/appends
verify(session, times(2)).write(any(InputStream.class), anyString());
verify(session, times(1)).append(any(InputStream.class), anyString());
}
use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testPut.
@Test
public void testPut() throws Exception {
@SuppressWarnings("unchecked") SessionFactory<TestLsEntry> sessionFactory = mock(SessionFactory.class);
@SuppressWarnings("unchecked") Session<TestLsEntry> session = mock(Session.class);
RemoteFileTemplate<TestLsEntry> template = new RemoteFileTemplate<TestLsEntry>(sessionFactory) {
@Override
public boolean exists(String path) {
return false;
}
};
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "put", "payload");
FileTransferringMessageHandler<TestLsEntry> handler = new FileTransferringMessageHandler<TestLsEntry>(sessionFactory);
handler.setRemoteDirectoryExpressionString("'foo/'");
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(session);
Message<String> requestMessage = MessageBuilder.withPayload("hello").setHeader(FileHeaders.FILENAME, "bar.txt").build();
String path = (String) gw.handleRequestMessage(requestMessage);
assertEquals("foo/bar.txt", path);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(session).write(any(InputStream.class), captor.capture());
assertEquals("foo/bar.txt.writing", captor.getValue());
verify(session).rename("foo/bar.txt.writing", "foo/bar.txt");
}
use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration by spring-projects.
the class FtpTests method testFtpOutboundFlow.
@Test
public void testFtpOutboundFlow() {
IntegrationFlow flow = f -> f.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";
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));
assertEquals(1, files.length);
assertEquals(3, files[0].getSize());
registration.destroy();
}
use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration-samples by spring-projects.
the class SftpTestUtils method createTestFiles.
public static void createTestFiles(RemoteFileTemplate<LsEntry> template, final String... fileNames) {
if (template != null) {
final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes());
template.execute((SessionCallback<LsEntry, Void>) session -> {
try {
session.mkdir("si.sftp.sample");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("failed to create"));
}
for (int i = 0; i < fileNames.length; i++) {
stream.reset();
session.write(stream, "si.sftp.sample/" + fileNames[i]);
}
return null;
});
}
}
Aggregations