@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(); } }
template = new RemoteFileTemplate<LsEntry>(sessionFactory); SftpTestUtils.createTestFiles(template, file1, file2);
@SuppressWarnings("unchecked") SessionFactory<LsEntry> sessionFactory = context.getBean(CachingSessionFactory.class); template = new RemoteFileTemplate<LsEntry>(sessionFactory); SftpTestUtils.createTestFiles(template, file1, file2, file3);
/** * 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); }
/** * Construct an instance with the supplied session factory, a command ('ls', 'get' * etc), and an expression to determine the filename. * @param sessionFactory the session factory. * @param command the command. * @param expression the filename expression. */ public AbstractRemoteFileOutboundGateway(SessionFactory<F> sessionFactory, Command command, @Nullable String expression) { this(new RemoteFileTemplate<F>(sessionFactory), command, expression); }
/** * Create a synchronizer with the {@link SessionFactory} used to acquire {@link Session} instances. * * @param sessionFactory The session factory. */ public AbstractInboundFileSynchronizer(SessionFactory<F> sessionFactory) { Assert.notNull(sessionFactory, "sessionFactory must not be null"); this.remoteFileTemplate = new RemoteFileTemplate<F>(sessionFactory); }
public FileTransferringMessageHandler(SessionFactory<F> sessionFactory) { Assert.notNull(sessionFactory, "sessionFactory must not be null"); this.remoteFileTemplate = new RemoteFileTemplate<F>(sessionFactory); this.mode = FileExistsMode.REPLACE; }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { SessionFactory<Object> sessionFactory = mock(SessionFactory.class); this.template = new RemoteFileTemplate<Object>(sessionFactory); this.template.setRemoteDirectoryExpression(new LiteralExpression("/foo")); this.template.setBeanFactory(mock(BeanFactory.class)); this.template.afterPropertiesSet(); this.session = mock(Session.class); when(sessionFactory.getSession()).thenReturn(this.session); this.file = this.folder.newFile(); }
@Test public void testDirtySession() throws Exception { @SuppressWarnings("unchecked") SessionFactory<Object> factory = mock(SessionFactory.class); @SuppressWarnings("unchecked") Session<Object> session = mock(Session.class); when(factory.getSession()).thenReturn(session); when(session.readRaw("foo")).thenReturn(new ByteArrayInputStream("".getBytes())); when(session.finalizeRaw()).thenReturn(true); CachingSessionFactory<Object> ccf = new CachingSessionFactory<Object>(factory); RemoteFileTemplate<Object> template = new RemoteFileTemplate<Object>(ccf); template.setFileNameExpression(new LiteralExpression("foo")); template.setBeanFactory(mock(BeanFactory.class)); template.afterPropertiesSet(); try { template.get(new GenericMessage<String>("foo"), (InputStreamCallback) stream -> { throw new RuntimeException("bar"); }); fail("Expected exception"); } catch (Exception e) { assertThat(e.getCause(), instanceOf(RuntimeException.class)); assertThat(e.getCause().getMessage(), equalTo("bar")); } verify(session).close(); }
@SuppressWarnings("unchecked") Session<TestLsEntry> session = mock(Session.class); RemoteFileTemplate<TestLsEntry> template = new RemoteFileTemplate<TestLsEntry>(sessionFactory); template.setRemoteDirectoryExpression(new LiteralExpression("foo/")); template.setBeanFactory(mock(BeanFactory.class));
@SuppressWarnings("unchecked") Session<TestLsEntry> session = mock(Session.class); RemoteFileTemplate<TestLsEntry> template = new RemoteFileTemplate<TestLsEntry>(sessionFactory); template.setRemoteDirectoryExpression(new LiteralExpression("foo/")); template.setBeanFactory(mock(BeanFactory.class));
@SuppressWarnings("unchecked") Session<TestLsEntry> session = mock(Session.class); RemoteFileTemplate<TestLsEntry> template = new RemoteFileTemplate<TestLsEntry>(sessionFactory); template.setRemoteDirectoryExpression(new LiteralExpression("foo/")); template.setBeanFactory(mock(BeanFactory.class));
@Test public void testRawGETWithTemplate() throws Exception { RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<FTPFile>(this.ftpSessionFactory); template.setFileNameExpression(new SpelExpressionParser().parseExpression("payload")); template.setBeanFactory(mock(BeanFactory.class)); template.afterPropertiesSet(); final ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); assertTrue(template.get(new GenericMessage<String>("ftpSource/ ftpSource1.txt"), (InputStreamCallback) stream -> FileCopyUtils.copy(stream, baos1))); assertEquals("source1", new String(baos1.toByteArray())); final ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); assertTrue(template.get(new GenericMessage<String>("ftpSource/ftpSource2.txt"), (InputStreamCallback) stream -> FileCopyUtils.copy(stream, baos2))); assertEquals("source2", new String(baos2.toByteArray())); }
assertEquals("test remove .writing", e.getCause().getMessage()); @SuppressWarnings("unchecked") RemoteFileTemplate template = new RemoteFileTemplate(sessionFactory); File outFile = new File(this.tmpDir + "/f1" + template.getTemporaryFileSuffix()); assertFalse(outFile.exists());
@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(); }
@Test public void testSftpOutboundFlow() { IntegrationFlow flow = f -> f.handle(Sftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL) .useTemporaryFileName(false) .fileNameExpression("headers['" + FileHeaders.FILENAME + "']") .remoteDirectory("sftpTarget")); IntegrationFlowRegistration registration = this.flowContext.registration(flow).register(); String fileName = "foo.file"; registration.getInputChannel().send(MessageBuilder.withPayload("foo") .setHeader(FileHeaders.FILENAME, fileName) .build()); RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(sessionFactory()); ChannelSftp.LsEntry[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName)); assertEquals(1, files.length); assertEquals(3, files[0].getAttrs().getSize()); registration.destroy(); }
@Test public void testFtpOutboundFlow() { String fileName = "foo.file"; this.toFtpChannel.send(MessageBuilder.withPayload("foo") .setHeader(FileHeaders.FILENAME, fileName) .build()); RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<>(this.ftpSessionFactory); FTPFile[] files = template.execute(session -> session.list(this.ftpServer.getTargetFtpDirectory().getName() + "/" + fileName)); assertEquals(1, files.length); assertEquals(3, files[0].getSize()); }
@Test public void testSftpOutboundFlow() { String fileName = "foo.file"; this.toSftpChannel.send(MessageBuilder.withPayload("foo") .setHeader(FileHeaders.FILENAME, fileName) .build()); RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(this.sftpSessionFactory); ChannelSftp.LsEntry[] files = template.execute(session -> session.list(this.sftpServer.getTargetSftpDirectory().getName() + "/" + fileName)); assertEquals(1, files.length); assertEquals(3, files[0].getAttrs().getSize()); }