Example usage for org.springframework.integration.file.remote.session Session list

List of usage examples for org.springframework.integration.file.remote.session Session list

Introduction

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

Prototype

F[] list(String path) throws IOException;

Source Link

Usage

From source file:org.opentestsystem.authoring.testitembank.service.impl.SftpFileTransferServiceImpl.java

private final int cleanDirectory(final String baseDirectory, final int maxAge,
        final Session<ChannelSftp.LsEntry> sftpSession) {
    final long maxAgeInMillis = Long.valueOf(maxAge) * MILLIS_IN_A_DAY;
    final long currentTimeInMillis = new Date().getTime();

    int remainingFileCount = 0;
    try {//from  w w w . ja v a 2s.  co  m
        final LsEntry[] remoteFiles = sftpSession.list(baseDirectory);
        for (final LsEntry remoteFile : remoteFiles) {
            if (isSubDirectory(remoteFile)) {
                final int directoryFileCount = cleanDirectory(baseDirectory + "/" + remoteFile.getFilename(),
                        maxAge, sftpSession);
                remainingFileCount += directoryFileCount;

                // if (!remoteFile.getFilename().startsWith(ImportSetServiceImpl.SFTP_TENANT_FOLDER_PREFIX) && (directoryFileCount == 0)) {
                // TODO - do we need to delete empty sub-directories?
                // TODO - we'll need a different SFTP library for that, or wait for Spring integration 4.1.0 which has rmdir() method
                // }
            } else if (!isDirectory(remoteFile)) {
                final long lastModifiedTimeInMillis = Long.valueOf(remoteFile.getAttrs().getMTime()) * 1000L;
                final long fileAgeInMillis = currentTimeInMillis - lastModifiedTimeInMillis;
                if (fileAgeInMillis > maxAgeInMillis) {
                    sftpSession.remove(baseDirectory + "/" + remoteFile.getFilename());
                } else {
                    remainingFileCount++;
                }
            }
        }
    } catch (final IOException e) {
        LOGGER.error("unable to clean SFTP directory: " + baseDirectory, e);
        throw new TestItemBankException("sftp.clean.error", new String[] { baseDirectory });
    }

    return remainingFileCount;
}

From source file:org.springframework.batch.integration.x.RemoteFileToHadoopTests.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Before//from   w w  w .j a  v a 2 s  .  c o  m
public void setup() throws Exception {
    byte[] bytes = "foobarbaz".getBytes();

    // using this trick here by getting hadoop minicluster from main test
    // context and then using it to override 'hadoopConfiguration' bean
    // which is imported from ftphdfs.xml.
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
    ctx.setConfigLocations("org/springframework/batch/integration/x/RemoteFileToHadoopTests-context.xml",
            "org/springframework/batch/integration/x/miniclusterconfig.xml");
    Properties properties = new Properties();
    properties.setProperty("restartable", "false");
    properties.setProperty("xd.config.home", "file:../../config");
    properties.setProperty("partitionResultsTimeout", "3600000");
    PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("props", properties);
    ctx.getEnvironment().getPropertySources().addLast(propertiesPropertySource);
    ctx.setParent(context);
    ctx.refresh();

    this.sessionFactory = ctx.getBean(SessionFactory.class);

    Session session = mock(Session.class);
    when(this.sessionFactory.getSession()).thenReturn(session);
    when(session.readRaw("/foo/bar.txt")).thenReturn(new ByteArrayInputStream(bytes));
    when(session.readRaw("/foo/baz.txt")).thenReturn(new ByteArrayInputStream(bytes));
    when(session.finalizeRaw()).thenReturn(true);
    Object[] fileList = new FTPFile[2];
    FTPFile file = new FTPFile();
    file.setName("bar.txt");
    fileList[0] = file;
    file = new FTPFile();
    file.setName("baz.txt");
    fileList[1] = file;
    when(session.list("/foo/")).thenReturn(fileList);

    this.launcher = ctx.getBean(JobLauncher.class);
    this.job = ctx.getBean(Job.class);
    this.requestsOut = ctx.getBean("stepExecutionRequests.output", MessageChannel.class);
    this.requestsIn = ctx.getBean("stepExecutionRequests.input", MessageChannel.class);
    this.repliesOut = ctx.getBean("stepExecutionReplies.output", MessageChannel.class);
    this.repliesIn = ctx.getBean("stepExecutionReplies.input", MessageChannel.class);

    this.bus = new LocalMessageBus();
    ((LocalMessageBus) this.bus).setApplicationContext(ctx);
    this.bus.bindRequestor("foo", this.requestsOut, this.repliesIn, null);
    this.bus.bindReplier("foo", this.requestsIn, this.repliesOut, null);
}

From source file:org.springframework.batch.integration.x.RemoteFileToTmpTests.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Before//from  w  w w  .j  a v a 2s  .  com
public void setup() throws Exception {
    byte[] bytes = "foo".getBytes();
    Session session = mock(Session.class);
    when(this.sessionFactory.getSession()).thenReturn(session);
    when(session.readRaw("/foo/bar.txt")).thenReturn(new ByteArrayInputStream(bytes));
    when(session.readRaw("/foo/baz.txt")).thenReturn(new ByteArrayInputStream(bytes));
    when(session.finalizeRaw()).thenReturn(true);
    Object[] fileList = new FTPFile[2];
    FTPFile file = new FTPFile();
    file.setName("bar.txt");
    fileList[0] = file;
    file = new FTPFile();
    file.setName("baz.txt");
    fileList[1] = file;
    when(session.list("/foo/")).thenReturn(fileList);

    this.bus = new LocalMessageBus();
    this.bus.setApplicationContext(BusTestUtils.MOCK_AC);
    this.bus.bindRequestor("foo", this.requestsOut, this.repliesIn, null);
    this.bus.bindReplier("foo", this.requestsIn, this.repliesOut, null);
    this.bus.afterPropertiesSet();
}

From source file:org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.java

public void synchronizeToLocalDirectory(File localDirectory) {
    Session<F> session = null;
    try {/*from w ww.  j  av  a  2  s.c o m*/
        session = this.sessionFactory.getSession();
        Assert.state(session != null, "failed to acquire a Session");
        F[] files = session.list(this.remoteDirectory);
        if (!ObjectUtils.isEmpty(files)) {
            Collection<F> filteredFiles = this.filterFiles(files);
            for (F file : filteredFiles) {
                if (file != null) {
                    this.copyFileToLocalDirectory(this.remoteDirectory, file, localDirectory, session);
                }
            }
        }
    } catch (IOException e) {
        throw new MessagingException("Problem occurred while synchronizing remote to local directory", e);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (Exception ignored) {
                if (logger.isDebugEnabled()) {
                    logger.debug("failed to close Session", ignored);
                }
            }
        }
    }
}