Example usage for org.springframework.integration.file.remote RemoteFileTemplate RemoteFileTemplate

List of usage examples for org.springframework.integration.file.remote RemoteFileTemplate RemoteFileTemplate

Introduction

In this page you can find the example usage for org.springframework.integration.file.remote RemoteFileTemplate RemoteFileTemplate.

Prototype

public RemoteFileTemplate(SessionFactory<F> sessionFactory) 

Source Link

Document

Construct a RemoteFileTemplate with the supplied session factory.

Usage

From source file:com.aeg.ims.ftp.SftpOutboundTransferSample.java

@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 {/*w w w  .  j a v a  2 s. c o  m*/
        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));

        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();
    }
}

From source file:com.aeg.ims.ftp.SftpInboundReceiveSample.java

public void runDemo() {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:/META-INF/spring/integration/SftpInboundReceiveSample-context.xml");

    RemoteFileTemplate<LsEntry> template = null;
    String file1 = "a.txt";
    String file2 = "b.txt";
    String file3 = "c.bar";
    new File("local-dir", file1).delete();
    new File("local-dir", file2).delete();
    try {//from   w  ww  .j ava2  s. co  m
        PollableChannel localFileChannel = context.getBean("receiveChannel", PollableChannel.class);
        @SuppressWarnings("unchecked")
        SessionFactory<LsEntry> sessionFactory = context.getBean(CachingSessionFactory.class);
        template = new RemoteFileTemplate<LsEntry>(sessionFactory);
        SftpTestUtils.createTestFiles(template, file1, file2, file3);

        SourcePollingChannelAdapter adapter = context.getBean(SourcePollingChannelAdapter.class);
        adapter.start();

        Message<?> received = localFileChannel.receive();
        assertNotNull("Expected file", received);
        System.out.println("Received first file message: " + received);
        received = localFileChannel.receive();
        assertNotNull("Expected file", received);
        System.out.println("Received second file message: " + received);
        received = localFileChannel.receive(1000);
        assertNull("Expected null", received);
        System.out.println("No third file was received as expected");
    } finally {
        SftpTestUtils.cleanUp(template, file1, file2, file3);
        context.close();
        assertTrue("Could note delete retrieved file", new File("local-dir", file1).delete());
        assertTrue("Could note delete retrieved file", new File("local-dir", file2).delete());
    }
}

From source file:com.aeg.ims.ftp.SftpOutboundGatewaySample.java

@Test
public void testLsGetRm() throws Exception {
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
            "classpath:/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml");
    ToSftpFlowGateway toFtpFlow = ctx.getBean(ToSftpFlowGateway.class);
    RemoteFileTemplate<LsEntry> template = null;
    String file1 = "1.ftptest";
    String file2 = "2.ftptest";
    File tmpDir = new File(System.getProperty("java.io.tmpdir"));

    try {//  w w w .j  a v a2  s .  co  m
        // remove the previous output files if necessary
        new File(tmpDir, file1).delete();
        new File(tmpDir, file2).delete();

        @SuppressWarnings("unchecked")
        SessionFactory<LsEntry> sessionFactory = ctx.getBean(CachingSessionFactory.class);
        template = new RemoteFileTemplate<LsEntry>(sessionFactory);
        SftpTestUtils.createTestFiles(template, file1, file2);

        // execute the flow (ls, get, rm, aggregate results)
        List<Boolean> rmResults = toFtpFlow.lsGetAndRmFiles("si.sftp.sample");

        //Check everything went as expected, and clean up
        assertEquals(2, rmResults.size());
        for (Boolean result : rmResults) {
            assertTrue(result);
        }

    } finally {
        SftpTestUtils.cleanUp(template, file1, file2);
        ctx.close();
        assertTrue("Could note delete retrieved file", new File(tmpDir, file1).delete());
        assertTrue("Could note delete retrieved file", new File(tmpDir, file2).delete());
    }
}

From source file:org.springframework.integration.dsl.ftp.Ftp.java

public static FtpMessageHandlerSpec outboundAdapter(SessionFactory<FTPFile> sessionFactory,
        FileExistsMode fileExistsMode) {
    return outboundAdapter(new RemoteFileTemplate<FTPFile>(sessionFactory), fileExistsMode);
}

From source file:org.springframework.integration.dsl.test.ftp.FtpTests.java

@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(/*  www  .  ja  v  a  2  s . c  om*/
            session -> session.list(this.ftpServer.getTargetFtpDirectory().getName() + "/" + fileName));
    assertEquals(1, files.length);
    assertEquals(3, files[0].getSize());
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@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);//  w w w.j ava2  s .c o  m
    assertEquals(3, files[0].getAttrs().getSize());
}

From source file:org.springframework.integration.ftp.dsl.FtpTests.java

@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/*w  w  w . j  ava 2s.  co m*/
            .execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
    assertEquals(1, files.length);
    assertEquals(3, files[0].getSize());

    registration.destroy();
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@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();/*  w w  w . j av a  2  s  .co m*/
    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()));
}