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