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

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

Introduction

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

Prototype

SessionCallback

Source Link

Usage

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  . j av a 2  s . co 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: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  ww w  .j a  va  2 s  .  co  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  www  .  j  a va 2  s .co  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;
    }
}