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

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

Introduction

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

Prototype

@SuppressWarnings("rawtypes")
    @Override
    public <T> T execute(SessionCallback<F, T> callback) 

Source Link

Usage

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

public static void cleanUp(RemoteFileTemplate<LsEntry> template, final String... fileNames) {
    if (template != null) {
        template.execute(new SessionCallback<LsEntry, Void>() {

            @Override//from   w  w w .  j  a va2s.c o m
            public Void doInSession(Session<LsEntry> session) throws IOException {
                // TODO: avoid DFAs with Spring 4.1 (INT-3412)
                ChannelSftp channel = (ChannelSftp) new DirectFieldAccessor(
                        new DirectFieldAccessor(session).getPropertyValue("targetSession"))
                                .getPropertyValue("channel");
                for (int i = 0; i < fileNames.length; i++) {
                    try {
                        session.remove("Test" + fileNames[i]);
                    } catch (IOException e) {
                    }
                }
                try {
                    // should be empty
                    channel.rmdir("Test");
                } catch (SftpException e) {
                    fail("Expected remote directory to be empty " + e.getMessage());
                }
                return null;
            }
        });
    }
}

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

public static boolean fileExists(RemoteFileTemplate<LsEntry> template, final String... fileNames) {
    if (template != null) {
        return template.execute(new SessionCallback<LsEntry, Boolean>() {

            @Override//from w  w  w.j  a  v  a 2s. c  o  m
            public Boolean doInSession(Session<LsEntry> session) throws IOException {
                // TODO: avoid DFAs with Spring 4.1 (INT-3412)
                ChannelSftp channel = (ChannelSftp) new DirectFieldAccessor(
                        new DirectFieldAccessor(session).getPropertyValue("targetSession"))
                                .getPropertyValue("channel");
                for (int i = 0; i < fileNames.length; i++) {
                    try {
                        SftpATTRS stat = channel.stat("Test/" + 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;
    }
}

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

public static void createTestFiles(RemoteFileTemplate<LsEntry> template, final String... fileNames) {
    if (template != null) {
        final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes());
        template.execute(new SessionCallback<LsEntry, Void>() {

            @Override/*from   w  w w .ja va 2  s. c o m*/
            public Void doInSession(Session<LsEntry> session) throws IOException {
                try {
                    session.mkdir("BriansTest");
                } catch (Exception e) {
                    assertThat(e.getMessage(), containsString("failed to create"));
                }
                for (int i = 0; i < fileNames.length; i++) {
                    stream.reset();
                    session.write(stream, "Test" + fileNames[i]);
                }
                return null;
            }
        });
    }
}

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(
            session -> session.list(this.ftpServer.getTargetFtpDirectory().getName() + "/" + fileName));
    assertEquals(1, files.length);/*  w w w . j av a2 s. co m*/
    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 ww.  ja  v a2  s. c om
    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
            .execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
    assertEquals(1, files.length);/*  w ww .j  a v a2s  .  co m*/
    assertEquals(3, files[0].getSize());

    registration.destroy();
}