Search in sources :

Example 31 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project onebusaway-application-modules by camsys.

the class OrbcadRecordFtpSource method getUpdatedFilesToDownload.

private List<String> getUpdatedFilesToDownload() throws IOException {
    long t1 = SystemTime.currentTimeMillis();
    FTPListParseEngine engine = _ftpClient.initiateListParsing(_dataDirectory);
    Set<String> paths = new HashSet<String>();
    List<String> toDownload = new ArrayList<String>();
    while (engine.hasNext()) {
        // "page size" you want
        FTPFile[] files = engine.getNext(25);
        for (FTPFile file : files) {
            String path = _dataDirectory + "/" + file.getName();
            paths.add(path);
            if (!_paths.contains(path))
                toDownload.add(path);
        }
    }
    _totalFtpFiles = paths.size();
    _newFtpFiles = toDownload.size();
    long t2 = SystemTime.currentTimeMillis();
    if (_log.isDebugEnabled())
        _log.debug("file listing time: " + (t2 - t1) + " totalFiles: " + paths.size() + " newFiles: " + toDownload.size());
    _paths = paths;
    if (_maxDownloadCount > 0 && toDownload.size() > _maxDownloadCount) {
        List<String> reduced = new ArrayList<String>(_maxDownloadCount);
        for (int i = 0; i < _maxDownloadCount; i++) reduced.add(toDownload.get(toDownload.size() - _maxDownloadCount + i));
        toDownload = reduced;
    }
    return toDownload;
}
Also used : ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) FTPListParseEngine(org.apache.commons.net.ftp.FTPListParseEngine) HashSet(java.util.HashSet)

Example 32 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project structr by structr.

the class FtpDirectoriesTest method test07CdToSiblingDirectory.

@Test
public void test07CdToSiblingDirectory() {
    FTPClient ftp = setupFTPClient("ftpuser1");
    try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
        FTPFile[] dirs = ftp.listDirectories();
        assertNotNull(dirs);
        assertEquals(0, dirs.length);
        String name1 = "/FTPdir1";
        String name2 = "/FTPdir2";
        // Create folders by mkdir FTP command
        ftp.makeDirectory(name1);
        ftp.makeDirectory(name2);
        ftp.changeWorkingDirectory(name1);
        String newWorkingDirectory = ftp.printWorkingDirectory();
        assertEquals(name1, newWorkingDirectory);
        ftp.changeWorkingDirectory("../" + name2);
        newWorkingDirectory = ftp.printWorkingDirectory();
        assertEquals(name2, newWorkingDirectory);
        ftp.disconnect();
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.error("Error while changing FTP directories", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) FtpTest(org.structr.web.files.FtpTest) Test(org.junit.Test)

Example 33 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project structr by structr.

the class FtpFilesTest method test06OverwriteFile.

@Test
public void test06OverwriteFile() {
    FTPClient ftp = setupFTPClient("ftpuser1");
    final String name1 = "file1";
    try (final Tx tx = app.tx()) {
        FTPFile[] files = ftp.listFiles();
        assertNotNull(files);
        assertEquals(0, files.length);
        ftp.setFileType(FTP.ASCII_FILE_TYPE);
        ftp.setAutodetectUTF8(true);
        // Store a file
        InputStream in = IOUtils.toInputStream("Initial Content");
        ftp.storeFile(name1, in);
        in.close();
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
    try (final Tx tx = app.tx()) {
        // Store a file
        InputStream in = IOUtils.toInputStream("Overwritten Content");
        ftp.storeFile(name1, in);
        in.close();
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
    try (final Tx tx = app.tx()) {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        final FTPFile[] files = ftp.listFiles();
        assertNotNull(files);
        assertEquals(1, files.length);
        assertEquals(name1, files[0].getName());
        ftp.retrieveFile(files[0].getName(), os);
        final byte[] data = os.toByteArray();
        final String content = new String(data, 0, data.length);
        assertEquals("Invalid content for overwritten file", "Overwritten Content", content);
        ftp.disconnect();
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) InputStream(java.io.InputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FTPClient(org.apache.commons.net.ftp.FTPClient) FtpTest(org.structr.web.files.FtpTest) Test(org.junit.Test)

Example 34 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project structr by structr.

the class FtpFilesTest method test00StoreFile.

@Test
public void test00StoreFile() {
    FTPClient ftp = setupFTPClient("ftpuser1");
    final String name1 = "file1";
    final String name2 = "file2";
    try (final Tx tx = app.tx()) {
        FTPFile[] files = ftp.listFiles();
        assertNotNull(files);
        assertEquals(0, files.length);
        ftp.setFileType(FTP.ASCII_FILE_TYPE);
        ftp.setAutodetectUTF8(true);
        // Store a file
        InputStream in = IOUtils.toInputStream("Test Content");
        ftp.storeFile(name1, in);
        in.close();
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
    String[] fileNames = null;
    try (final Tx tx = app.tx()) {
        fileNames = ftp.listNames();
        assertNotNull(fileNames);
        assertEquals(1, fileNames.length);
        assertEquals(name1, fileNames[0]);
        // Create second file in /
        createFTPFile(null, name2);
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
    try (final Tx tx = app.tx()) {
        fileNames = ftp.listNames();
        assertNotNull(fileNames);
        assertEquals(2, fileNames.length);
        assertEquals(name1, fileNames[0]);
        assertEquals(name2, fileNames[1]);
        ftp.disconnect();
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) InputStream(java.io.InputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) FtpTest(org.structr.web.files.FtpTest) Test(org.junit.Test)

Example 35 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project spring-integration-samples by spring-projects.

the class FileTransferRenameAfterFailureDemo method main.

public static void main(String[] args) throws Exception {
    LOGGER.info("\n=========================================================" + "\n                                                         " + "\n          Welcome to Spring Integration!                 " + "\n                                                         " + "\n    For more information please visit:                   " + "\n    http://www.springsource.org/spring-integration       " + "\n                                                         " + "\n=========================================================");
    final AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/expression-advice-context.xml");
    context.registerShutdownHook();
    @SuppressWarnings("unchecked") SessionFactory<FTPFile> sessionFactory = context.getBean(SessionFactory.class);
    SourcePollingChannelAdapter fileInbound = context.getBean(SourcePollingChannelAdapter.class);
    when(sessionFactory.getSession()).thenThrow(new RuntimeException("Force Failure"));
    fileInbound.start();
    LOGGER.info("\n=========================================================" + "\n                                                          " + "\n    This is the Expression Advice Sample -                " + "\n                                                          " + "\n    Press 'Enter' to terminate.                           " + "\n                                                          " + "\n    Place a file in " + System.getProperty("java.io.tmpdir") + "/adviceDemo ending   " + "\n    with .txt                                             " + "\n    The demo simulates a file transfer failure followed   " + "\n    by the Advice renaming the file; the result of the    " + "\n    rename is logged.                                     " + "\n                                                          " + "\n=========================================================");
    System.in.read();
    context.close();
    System.exit(0);
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) FTPFile(org.apache.commons.net.ftp.FTPFile)

Aggregations

FTPFile (org.apache.commons.net.ftp.FTPFile)120 IOException (java.io.IOException)59 FTPClient (org.apache.commons.net.ftp.FTPClient)34 Test (org.junit.Test)32 File (java.io.File)28 InputStream (java.io.InputStream)16 ArrayList (java.util.ArrayList)15 FrameworkException (org.structr.common.error.FrameworkException)15 Tx (org.structr.core.graph.Tx)15 FtpTest (org.structr.web.files.FtpTest)15 FileOutputStream (java.io.FileOutputStream)11 OutputStream (java.io.OutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BuildException (org.apache.tools.ant.BuildException)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 HashSet (java.util.HashSet)4