public static void cleanUp(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { template.execute((SessionCallback<LsEntry, Void>) session -> { for (int i = 0; i < fileNames.length; i++) { try { session.remove("si.sftp.sample/" + fileNames[i]); } catch (IOException e) {} } // should be empty session.rmdir("si.sftp.sample"); return null; }); } }
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; }); } }
public static boolean fileExists(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { return template.execute(session -> { ChannelSftp channel = (ChannelSftp) session.getClientInstance(); for (int i = 0; i < fileNames.length; i++) { try { SftpATTRS stat = channel.stat("si.sftp.sample/" + fileNames[i]); if (stat == null) { System.out.println("stat returned null for " + fileNames[i]); return false; } } catch (SftpException e) { System.out.println("Remote file not present: " + e.getMessage() + ": " + fileNames[i]); return false; } } return true; }); } else { return false; } }
/** * Construct an instance using the provided session factory and callback for * performing operations on the session. * @param sessionFactory the session factory. * @param messageSessionCallback the callback. */ public AbstractRemoteFileOutboundGateway(SessionFactory<F> sessionFactory, MessageSessionCallback<F, ?> messageSessionCallback) { this(new RemoteFileTemplate<F>(sessionFactory), messageSessionCallback); }
/** * Specify a remote directory path SpEL expression. * @param remoteDirectoryExpression the remote directory expression * @see RemoteFileTemplate#setRemoteDirectoryExpression(Expression) */ public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) { this.remoteFileTemplate.setRemoteDirectoryExpression(remoteDirectoryExpression); }
@Override public String send(final Message<?> message, String subDirectory, FileExistsMode... mode) { FileExistsMode modeToUse = mode == null || mode.length < 1 || mode[0] == null ? FileExistsMode.REPLACE : mode[0]; return send(message, subDirectory, modeToUse); }
/** * Specify a remote file separator symbol. * @param remoteFileSeparator the remote file separator. * @see RemoteFileTemplate#setRemoteFileSeparator(String) */ public void setRemoteFileSeparator(String remoteFileSeparator) { this.remoteFileTemplate.setRemoteFileSeparator(remoteFileSeparator); }
/** * Set the temporary suffix to use when transferring files to the remote system. * Default ".writing". * @param temporaryFileSuffix the temporary file suffix. * @see RemoteFileTemplate#setTemporaryFileSuffix(String) */ public void setTemporaryFileSuffix(String temporaryFileSuffix) { this.remoteFileTemplate.setTemporaryFileSuffix(temporaryFileSuffix); }
/** * Set the file name generator used to generate the remote filename to be used when * transferring files to the remote system. Default * {@link org.springframework.integration.file.DefaultFileNameGenerator}. * @param fileNameGenerator the file name generator. * @see RemoteFileTemplate#setFileNameGenerator(FileNameGenerator) */ public void setFileNameGenerator(FileNameGenerator fileNameGenerator) { this.remoteFileTemplate.setFileNameGenerator(fileNameGenerator); }
/** * A {@code boolean} flag to indicate automatically create the directory or not. * @param autoCreateDirectory true to automatically create the directory. * @see RemoteFileTemplate#setAutoCreateDirectory(boolean) */ public void setAutoCreateDirectory(boolean autoCreateDirectory) { this.remoteFileTemplate.setAutoCreateDirectory(autoCreateDirectory); }
@Override default Object doInSession(Session<F> session) throws IOException { doInSessionWithoutResult(session); return null; }
/** * Set the filter to be applied to the remote files before transferring. * @param filter the file list filter. */ public void setFilter(FileListFilter<F> filter) { doSetFilter(filter); }
@Override default Object doWithClient(C client) { doWithClientWithoutResult(client); return null; }
@Override protected Object doReceive(int maxFetchSize) { return doReceive(); }
/** * Set the charset to use when converting String payloads to bytes as the content of the * remote file. Default {@code UTF-8}. * @param charset the charset. * @see RemoteFileTemplate#setCharset(String) */ public void setCharset(String charset) { this.remoteFileTemplate.setCharset(charset); }
template = new RemoteFileTemplate<LsEntry>(sessionFactory); SftpTestUtils.createTestFiles(template, file1, file2);
/** * Set the file separator when dealing with remote files; default '/'. * @param remoteFileSeparator the separator. * @see RemoteFileTemplate#setRemoteFileSeparator(String) */ public void setRemoteFileSeparator(String remoteFileSeparator) { this.remoteFileTemplate.setRemoteFileSeparator(remoteFileSeparator); }
/** * Set the temporary suffix to use when transferring files to the remote system. * Default {@code .writing}. * @param temporaryFileSuffix the temporaryFileSuffix to set * @see RemoteFileTemplate#setTemporaryFileSuffix(String) */ public void setTemporaryFileSuffix(String temporaryFileSuffix) { this.remoteFileTemplate.setTemporaryFileSuffix(temporaryFileSuffix); }
@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); SftpTestUtils.createTestFiles(template); // Just the directory 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(); } }
@SuppressWarnings("unchecked") SessionFactory<LsEntry> sessionFactory = context.getBean(CachingSessionFactory.class); template = new RemoteFileTemplate<LsEntry>(sessionFactory); SftpTestUtils.createTestFiles(template, file1, file2, file3);