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

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

Introduction

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

Prototype

void read(String source, OutputStream outputStream) throws IOException;

Source Link

Usage

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

@Override
public final File getFile(final String importSetId, final String fileName) {
    File file = null;/*from   ww w  .j  ava 2  s. c  o m*/
    FileOutputStream fileOutputStream = null;
    Session<ChannelSftp.LsEntry> sftpSession = null;
    String path = this.tempFileDirectory;
    if (StringUtils.isEmpty(path)) {
        String userDir = System.getProperty("user.dir");
        LOGGER.info("The property 'tib.file.pathname' is not populated, defaulting to :" + userDir);
        path = userDir;
    }

    try {
        sftpSession = this.sftpSessionFactory.getSession();
        final String[] pathPortions = fileName.split("/");
        final String justFileName = pathPortions[pathPortions.length - 1];
        LOGGER.debug("TIB temp dir is: " + path + " file name: " + justFileName);

        path = path + "/importSet_" + importSetId;
        new File(path).mkdirs();

        file = new File(path, justFileName);
        fileOutputStream = new FileOutputStream(file);
        sftpSession.read(fileName, fileOutputStream);

    } catch (final Exception e) {
        LOGGER.error("unable to get file " + fileName + " from sftp site:" + e.getMessage(), e);
        throw new TestItemBankException("sftp.read.error", new String[] { fileName });
    } finally {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
            if (sftpSession != null) {
                sftpSession.close();
            }
        } catch (final IOException e) {
            LOGGER.error("unable to get file " + fileName + " from sftp site:" + e.getMessage(), e);
            throw new TestItemBankException("sftp.read.error", new String[] { fileName });
        }
    }
    return file;
}

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

private void copyFileToLocalDirectory(String remoteDirectoryPath, F remoteFile, File localDirectory,
        Session<F> session) throws IOException {
    String remoteFileName = this.getFilename(remoteFile);
    String localFileName = this.generateLocalFileName(remoteFileName);
    String remoteFilePath = remoteDirectoryPath + remoteFileSeparator + remoteFileName;
    if (!this.isFile(remoteFile)) {
        if (logger.isDebugEnabled()) {
            logger.debug("cannot copy, not a file: " + remoteFilePath);
        }//from  www .  j  a  v  a 2  s  . c o  m
        return;
    }

    File localFile = new File(localDirectory, localFileName);
    if (!localFile.exists()) {
        String tempFileName = localFile.getAbsolutePath() + this.temporaryFileSuffix;
        File tempFile = new File(tempFileName);
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
        try {
            session.read(remoteFilePath, fileOutputStream);
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new MessagingException("Failure occurred while copying from remote to local directory",
                        e);
            }
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception ignored1) {
            }
            try {
                fileOutputStream.close();
            } catch (Exception ignored2) {
            }
        }

        if (tempFile.renameTo(localFile)) {
            if (this.deleteRemoteFiles) {
                session.remove(remoteFilePath);
                if (logger.isDebugEnabled()) {
                    logger.debug("deleted " + remoteFilePath);
                }
            }
        }
    }
}

From source file:org.springframework.integration.sftp.outbound.SftpServerOutboundTests.java

@Test
public void testInt3047ConcurrentSharedSession() throws Exception {
    final Session<?> session1 = this.sessionFactory.getSession();
    final Session<?> session2 = this.sessionFactory.getSession();
    final PipedInputStream pipe1 = new PipedInputStream();
    PipedOutputStream out1 = new PipedOutputStream(pipe1);
    final PipedInputStream pipe2 = new PipedInputStream();
    PipedOutputStream out2 = new PipedOutputStream(pipe2);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    Executors.newSingleThreadExecutor().execute(() -> {
        try {//from  w w w . j a  v  a  2s.  c o  m
            session1.write(pipe1, "foo.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        latch1.countDown();
    });
    Executors.newSingleThreadExecutor().execute(() -> {
        try {
            session2.write(pipe2, "bar.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        latch2.countDown();
    });

    out1.write('a');
    out2.write('b');
    out1.write('c');
    out2.write('d');
    out1.write('e');
    out2.write('f');
    out1.close();
    out2.close();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
    ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
    session1.read("foo.txt", bos1);
    session2.read("bar.txt", bos2);
    assertEquals("ace", new String(bos1.toByteArray()));
    assertEquals("bdf", new String(bos2.toByteArray()));
    session1.remove("foo.txt");
    session2.remove("bar.txt");
    session1.close();
    session2.close();
}