Search in sources :

Example 26 with FTPFile

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

the class FtpRemoteFileTemplateTests method testFileCloseOnBadConnect.

@Test
public void testFileCloseOnBadConnect() throws Exception {
    @SuppressWarnings("unchecked") SessionFactory<FTPFile> sessionFactory = mock(SessionFactory.class);
    when(sessionFactory.getSession()).thenThrow(new RuntimeException("bar"));
    FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
    template.setRemoteDirectoryExpression(new LiteralExpression("foo"));
    template.afterPropertiesSet();
    File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.write("foo".getBytes());
    fileOutputStream.close();
    try {
        template.send(new GenericMessage<File>(file));
        fail("exception expected");
    } catch (MessagingException e) {
        assertEquals("bar", e.getCause().getMessage());
    }
    File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    assertTrue(file.renameTo(newFile));
    file.delete();
    newFile.delete();
}
Also used : MessagingException(org.springframework.messaging.MessagingException) LiteralExpression(org.springframework.expression.common.LiteralExpression) FileOutputStream(java.io.FileOutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) Test(org.junit.Test)

Example 27 with FTPFile

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

the class FtpInboundChannelAdapterSpec method composeFilters.

@SuppressWarnings("unchecked")
private CompositeFileListFilter<FTPFile> composeFilters(FileListFilter<FTPFile> fileListFilter) {
    CompositeFileListFilter<FTPFile> compositeFileListFilter = new CompositeFileListFilter<>();
    compositeFileListFilter.addFilters(fileListFilter, new FtpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "ftpMessageSource"));
    return compositeFileListFilter;
}
Also used : CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) FTPFile(org.apache.commons.net.ftp.FTPFile) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter)

Example 28 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project irida by phac-nml.

the class ExportUploadService method getLatestXMLStream.

/**
 * Get the latest result.#.xml file for the given submission
 *
 * @param client
 *            {@link FTPClient} to use for the connection
 * @param submission
 *            {@link NcbiExportSubmission} to get results for
 * @return {@link InputStream} for the newest file if found. null if no file
 *         was found
 * @throws NcbiXmlParseException
 *             if the file couldn't be found
 */
private InputStream getLatestXMLStream(FTPClient client, NcbiExportSubmission submission) throws NcbiXmlParseException {
    InputStream retrieveFileStream = null;
    try {
        String directoryPath = submission.getDirectoryPath();
        // cd to submission base directory
        if (!client.changeWorkingDirectory(directoryPath)) {
            throw new NcbiXmlParseException("Couldn't change to base directory " + baseDirectory + " : " + client.getReplyString());
        }
        Pattern regex = Pattern.compile("report.(\\d+).xml");
        String latestFile = null;
        int highestNumber = 0;
        // search for the highest number in the report.#.xml files
        FTPFile[] listFiles = client.listFiles();
        for (FTPFile file : listFiles) {
            String fileName = file.getName();
            Matcher matcher = regex.matcher(fileName);
            if (matcher.matches()) {
                int reportNumber = Integer.parseInt(matcher.group(1));
                if (reportNumber > highestNumber) {
                    highestNumber = reportNumber;
                    latestFile = fileName;
                }
            }
        }
        if (latestFile != null) {
            logger.trace("newest file is " + latestFile);
            retrieveFileStream = client.retrieveFileStream(latestFile);
        }
    } catch (IOException e) {
        throw new NcbiXmlParseException("Couldn't get response xml", e);
    }
    return retrieveFileStream;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) NcbiXmlParseException(ca.corefacility.bioinformatics.irida.exceptions.NcbiXmlParseException)

Example 29 with FTPFile

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

the class FTPClientImpl method mdelete.

public void mdelete(String path) throws FTPException {
    try {
        File file = new File(path);
        File dir = file.getParentFile();
        FTPFile[] files = null;
        if (dir == null) {
            if (ftpFileListParser == null) {
                files = client.listFiles();
            } else {
                files = client.listFiles(ftpFileListParser);
            }
        } else {
            if (ftpFileListParser == null) {
                files = client.listFiles(dir.getPath());
            } else {
                files = client.listFiles(ftpFileListParser, dir.getPath());
            }
        }
        if (files == null || files.length == 0) {
            return;
        }
        String fileName = file.getName();
        if (!isJavaRegexEnabled) {
            fileName = fileName.replaceAll("\\.", "\\\\.");
            fileName = fileName.replaceAll("\\*", ".*");
        }
        Pattern p = Pattern.compile(fileName);
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                continue;
            }
            Matcher m = p.matcher(files[i].getName());
            if (!m.matches()) {
                continue;
            }
            delete(dir == null ? files[i].getName() : new File(dir, files[i].getName()).getPath());
        }
    } catch (IOException e) {
        throw new FTPException(e);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 30 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project fmv by f-agu.

the class FTPItem method listChildren.

/**
 * @see org.fagu.sync.Item#listChildren()
 */
@Override
public Map<String, Item> listChildren() throws IOException {
    FTPFile[] listFiles = ftpClient.listFiles(ftpPath);
    Map<String, Item> items = new TreeMap<>(Collections.reverseOrder());
    for (FTPFile ftpFile : listFiles) {
        items.put(ftpFile.getName(), create(ftpClient, ftpPath + '/' + ftpFile.getName(), ftpFile.getName(), ftpFile));
    }
    return items;
}
Also used : Item(org.fagu.fmv.mymedia.sync.Item) FTPFile(org.apache.commons.net.ftp.FTPFile) TreeMap(java.util.TreeMap)

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