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();
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations