Example usage for org.springframework.integration.ftp.session FtpRemoteFileTemplate FtpRemoteFileTemplate

List of usage examples for org.springframework.integration.ftp.session FtpRemoteFileTemplate FtpRemoteFileTemplate

Introduction

In this page you can find the example usage for org.springframework.integration.ftp.session FtpRemoteFileTemplate FtpRemoteFileTemplate.

Prototype

public FtpRemoteFileTemplate(SessionFactory<FTPFile> sessionFactory) 

Source Link

Usage

From source file:org.springframework.cloud.stream.app.ftp.sink.FtpSinkConfiguration.java

@Bean
public IntegrationFlow ftpInboundFlow(FtpSinkProperties properties, SessionFactory<FTPFile> ftpSessionFactory) {
    FtpMessageHandlerSpec handlerSpec = Ftp
            .outboundAdapter(new FtpRemoteFileTemplate(ftpSessionFactory), properties.getMode())
            .remoteDirectory(properties.getRemoteDir()).remoteFileSeparator(properties.getRemoteFileSeparator())
            .autoCreateDirectory(properties.isAutoCreateDir())
            .temporaryFileSuffix(properties.getTmpFileSuffix());
    if (properties.getFilenameExpression() != null) {
        handlerSpec.fileNameExpression(properties.getFilenameExpression().getExpressionString());
    }//from  w ww  .j  a  va  2s . c  om
    return IntegrationFlows.from(Sink.INPUT)
            .handle(handlerSpec, new Consumer<GenericEndpointSpec<FileTransferringMessageHandler<FTPFile>>>() {
                @Override
                public void accept(GenericEndpointSpec<FileTransferringMessageHandler<FTPFile>> e) {
                    e.autoStartup(false);
                }
            }).get();
}

From source file:org.springframework.cloud.stream.module.ftp.FtpSink.java

@Bean
public IntegrationFlow ftpInboundFlow() {
    FtpMessageHandlerSpec handlerSpec = Ftp
            .outboundAdapter(new FtpRemoteFileTemplate(this.ftpSessionFactory), this.properties.getMode())
            .remoteDirectory(this.properties.getRemoteDir())
            .remoteFileSeparator(this.properties.getRemoteFileSeparator())
            .autoCreateDirectory(this.properties.isAutoCreateDir())
            .temporaryFileSuffix(this.properties.getTmpFileSuffix());
    if (this.properties.getFilenameExpression() != null) {
        handlerSpec.fileNameExpression(this.properties.getFilenameExpression().getExpressionString());
    }/*from  w w w .jav  a2 s  . c o  m*/
    return IntegrationFlows.from(Sink.INPUT)
            .handle(handlerSpec, new Consumer<GenericEndpointSpec<FileTransferringMessageHandler<FTPFile>>>() {
                @Override
                public void accept(GenericEndpointSpec<FileTransferringMessageHandler<FTPFile>> e) {
                    e.autoStartup(false);
                }
            }).get();
}

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

/**
 * A {@link FtpMessageHandlerSpec} factory for an outbound channel adapter spec.
 * @param sessionFactory the session factory.
 * @param fileExistsMode the file exists mode.
 * @return the spec./*from ww w.  ja  v a 2s.  c  o m*/
 */
public static FtpMessageHandlerSpec outboundAdapter(SessionFactory<FTPFile> sessionFactory,
        FileExistsMode fileExistsMode) {
    return outboundAdapter(new FtpRemoteFileTemplate(sessionFactory), fileExistsMode);
}

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

@Test
public void testFtpInboundStreamFlow() throws Exception {
    QueueChannel out = new QueueChannel();
    StandardIntegrationFlow flow = IntegrationFlows.from(
            Ftp.inboundStreamingAdapter(new FtpRemoteFileTemplate(sessionFactory()))
                    .remoteDirectory("ftpSource").regexFilter(".*\\.txt$"),
            e -> e.id("ftpInboundAdapter").poller(Pollers.fixedDelay(100))).channel(out).get();
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    Message<?> message = out.receive(10_000);
    assertNotNull(message);/* w  ww . ja  v a 2s .  c om*/
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" ftpSource1.txt", "ftpSource2.txt"));
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();

    message = out.receive(10_000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" ftpSource1.txt", "ftpSource2.txt"));
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();

    registration.destroy();
}

From source file:org.springframework.integration.ftp.inbound.RotatingServersTests.java

@BeforeClass
public static void setup() {
    FtpRemoteFileTemplate rft = new FtpRemoteFileTemplate(sessionFactory());
    rft.execute(s -> {/*from w ww  .  j ava2  s . com*/
        s.mkdir("foo");
        s.mkdir("bar");
        s.mkdir("baz");
        s.mkdir("qux");
        s.mkdir("fiz");
        s.mkdir("buz");
        ByteArrayInputStream bais = new ByteArrayInputStream("foo".getBytes());
        s.write(bais, "foo/f1");
        s.write(bais, "baz/f2");
        s.write(bais, "fiz/f3");
        return null;
    });
}

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

public FtpMessageHandler(SessionFactory<FTPFile> sessionFactory) {
    this(new FtpRemoteFileTemplate(sessionFactory));
    ((FtpRemoteFileTemplate) this.remoteFileTemplate).setExistsMode(FtpRemoteFileTemplate.ExistsMode.NLST);
}

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

@Test
public void testInt3412FileMode() {
    FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(ftpSessionFactory);
    assertFalse(template.exists("ftpTarget/appending.txt"));
    Message<String> m = MessageBuilder.withPayload("foo").setHeader(FileHeaders.FILENAME, "appending.txt")
            .build();// w w w.j  a v  a 2 s .c  o m
    appending.send(m);
    appending.send(m);

    assertLength6(template);

    ignoring.send(m);
    assertLength6(template);
    try {
        failing.send(m);
        fail("Expected exception");
    } catch (MessagingException e) {
        assertThat(e.getCause().getCause().getMessage(), containsString("The destination file already exists"));
    }

}

From source file:org.springframework.integration.ftp.session.FtpRemoteFileTemplateTests.java

@Test
public void testINT3412AppendStatRmdir() throws IOException {
    FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
    DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
    fileNameGenerator.setExpression("'foobar.txt'");
    template.setFileNameGenerator(fileNameGenerator);
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
    template.setUseTemporaryFileName(false);
    template.execute(session -> {//from www.  j  a  v  a2  s. c o m
        session.mkdir("foo/");
        return session.mkdir("foo/bar/");
    });
    template.append(new GenericMessage<String>("foo"));
    template.append(new GenericMessage<String>("bar"));
    assertTrue(template.exists("foo/foobar.txt"));
    template.executeWithClient((ClientCallbackWithoutResult<FTPClient>) client -> {
        try {
            FTPFile[] files = client.listFiles("foo/foobar.txt");
            assertEquals(6, files[0].getSize());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    template.execute((SessionCallbackWithoutResult<FTPFile>) session -> {
        assertTrue(session.remove("foo/foobar.txt"));
        assertTrue(session.rmdir("foo/bar/"));
        FTPFile[] files = session.list("foo/");
        assertEquals(0, files.length);
        assertTrue(session.rmdir("foo/"));
    });
    assertFalse(template.getSession().exists("foo"));
}

From source file:org.springframework.integration.ftp.session.FtpRemoteFileTemplateTests.java

@Test
public void testFileCloseOnBadConnect() throws Exception {
    @SuppressWarnings("unchecked")
    SessionFactory<FTPFile> sessionFactory = mock(SessionFactory.class);
    when(sessionFactory.getSession()).thenThrow(new RuntimeException("bar"));
    FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
    template.setRemoteDirectoryExpression(new LiteralExpression("foo"));
    template.afterPropertiesSet();// www.j av  a2 s  .c  o m
    File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.write("foo".getBytes());
    fileOutputStream.close();
    try {
        template.send(new GenericMessage<File>(file));
        fail("exception expected");
    } catch (MessagingException e) {
        assertEquals("bar", e.getCause().getMessage());
    }
    File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    assertTrue(file.renameTo(newFile));
    file.delete();
    newFile.delete();
}