use of org.apache.commons.net.ftp.FTPFile in project AnExplorer by 1hakr.
the class NetworkStorageProvider method queryChildDocuments.
@Override
public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
final NetworkFile parent = getFileForDocId(parentDocumentId);
final NetworkConnection connection = getNetworkConnection(parentDocumentId);
try {
connection.getConnectedClient().changeWorkingDirectory(parent.getPath());
for (FTPFile file : connection.getConnectedClient().listFiles()) {
includeFile(result, null, new NetworkFile(parent, file));
}
} catch (IOException e) {
CrashReportingManager.logException(e);
}
return result;
}
use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.
the class FtpInboundRemoteFileSystemSynchronizerTests method testCopyFileToLocalDir.
@Test
public void testCopyFileToLocalDir() throws Exception {
File localDirectory = new File("test");
assertFalse(localDirectory.exists());
TestFtpSessionFactory ftpSessionFactory = new TestFtpSessionFactory();
ftpSessionFactory.setUsername("kermit");
ftpSessionFactory.setPassword("frog");
ftpSessionFactory.setHost("foo.com");
FtpInboundFileSynchronizer synchronizer = spy(new FtpInboundFileSynchronizer(ftpSessionFactory));
synchronizer.setDeleteRemoteFiles(true);
synchronizer.setPreserveTimestamp(true);
synchronizer.setRemoteDirectory("remote-test-dir");
FtpRegexPatternFileListFilter patternFilter = new FtpRegexPatternFileListFilter(".*\\.test$");
PropertiesPersistingMetadataStore store = spy(new PropertiesPersistingMetadataStore());
store.setBaseDirectory("test");
store.afterPropertiesSet();
FtpPersistentAcceptOnceFileListFilter persistFilter = new FtpPersistentAcceptOnceFileListFilter(store, "foo");
List<FileListFilter<FTPFile>> filters = new ArrayList<FileListFilter<FTPFile>>();
filters.add(persistFilter);
filters.add(patternFilter);
CompositeFileListFilter<FTPFile> filter = new CompositeFileListFilter<FTPFile>(filters);
synchronizer.setFilter(filter);
ExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression expression = expressionParser.parseExpression("'subdir/' + #this.toUpperCase() + '.a'");
synchronizer.setLocalFilenameGeneratorExpression(expression);
synchronizer.setBeanFactory(mock(BeanFactory.class));
synchronizer.afterPropertiesSet();
FtpInboundFileSynchronizingMessageSource ms = new FtpInboundFileSynchronizingMessageSource(synchronizer);
ms.setAutoCreateLocalDirectory(true);
ms.setLocalDirectory(localDirectory);
ms.setBeanFactory(mock(BeanFactory.class));
CompositeFileListFilter<File> localFileListFilter = new CompositeFileListFilter<File>();
localFileListFilter.addFilter(new RegexPatternFileListFilter(".*\\.TEST\\.a$"));
AcceptOnceFileListFilter<File> localAcceptOnceFilter = new AcceptOnceFileListFilter<File>();
localFileListFilter.addFilter(localAcceptOnceFilter);
RecursiveDirectoryScanner scanner = new RecursiveDirectoryScanner();
ms.setScanner(scanner);
ms.setLocalFilter(localFileListFilter);
ms.afterPropertiesSet();
ms.start();
Message<File> atestFile = ms.receive();
assertNotNull(atestFile);
assertEquals("A.TEST.a", atestFile.getPayload().getName());
// The test remote files are created with the current timestamp + 1 day.
assertThat(atestFile.getPayload().lastModified(), Matchers.greaterThan(System.currentTimeMillis()));
assertEquals("A.TEST.a", atestFile.getHeaders().get(FileHeaders.FILENAME));
Message<File> btestFile = ms.receive();
assertNotNull(btestFile);
assertEquals("B.TEST.a", btestFile.getPayload().getName());
// The test remote files are created with the current timestamp + 1 day.
assertThat(atestFile.getPayload().lastModified(), Matchers.greaterThan(System.currentTimeMillis()));
Message<File> nothing = ms.receive();
assertNull(nothing);
// two times because on the third receive (above) the internal queue will be empty, so it will attempt
verify(synchronizer, times(2)).synchronizeToLocalDirectory(localDirectory, Integer.MIN_VALUE);
assertTrue(new File("test/subdir/A.TEST.a").exists());
assertTrue(new File("test/subdir/B.TEST.a").exists());
TestUtils.getPropertyValue(localAcceptOnceFilter, "seenSet", Collection.class).clear();
new File("test/subdir/A.TEST.a").delete();
new File("test/subdir/B.TEST.a").delete();
// the remote filter should prevent a re-fetch
nothing = ms.receive();
assertNull(nothing);
ms.stop();
verify(synchronizer).close();
verify(store).close();
}
use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.
the class FtpServerOutboundTests method testNlstAndWorkingDirExpression.
@Test
@SuppressWarnings("unchecked")
public void testNlstAndWorkingDirExpression() throws IOException {
this.inboundNlst.send(new GenericMessage<>("foo"));
Message<?> receive = this.output.receive(10000);
assertNotNull(receive);
assertThat(receive.getPayload(), instanceOf(List.class));
List<String> files = (List<String>) receive.getPayload();
assertEquals(3, files.size());
assertThat(files, containsInAnyOrder("subFtpSource", " ftpSource1.txt", "ftpSource2.txt"));
FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null);
for (FTPFile ftpFile : ftpFiles) {
if (!ftpFile.isDirectory()) {
assertTrue(files.contains(ftpFile.getName()));
}
}
}
use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.
the class FtpServerOutboundTests method testMputRecursivePartial.
@Test
public void testMputRecursivePartial() throws Exception {
Session<FTPFile> session = spyOnSession();
File sourceLocalSubDirectory = new File(getSourceLocalDirectory(), "subLocalSource");
assertTrue(sourceLocalSubDirectory.isDirectory());
File extra = new File(sourceLocalSubDirectory, "subLocalSource2.txt");
FileOutputStream writer = new FileOutputStream(extra);
writer.write("foo".getBytes());
writer.close();
doAnswer(invocation -> {
throw new IOException("Failed to send subLocalSource2");
}).when(session).write(Mockito.any(InputStream.class), Mockito.contains("subLocalSource2"));
try {
this.inboundMPutRecursive.send(new GenericMessage<File>(getSourceLocalDirectory()));
fail("expected exception");
} catch (PartialSuccessException e) {
assertEquals(3, e.getDerivedInput().size());
assertEquals(2, e.getPartialResults().size());
assertThat(e.getCause(), Matchers.instanceOf(PartialSuccessException.class));
PartialSuccessException cause = (PartialSuccessException) e.getCause();
assertEquals(2, cause.getDerivedInput().size());
assertEquals(1, cause.getPartialResults().size());
assertThat(cause.getCause().getMessage(), containsString("Failed to send subLocalSource2"));
}
extra.delete();
}
use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.
the class FtpTests method testFtpOutboundFlow.
@Test
public void testFtpOutboundFlow() {
IntegrationFlow flow = f -> f.handle(Ftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL).useTemporaryFileName(false).fileNameExpression("headers['" + FileHeaders.FILENAME + "']").remoteDirectory("ftpTarget"));
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
String fileName = "foo.file";
Message<ByteArrayInputStream> message = MessageBuilder.withPayload(new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8))).setHeader(FileHeaders.FILENAME, fileName).build();
registration.getInputChannel().send(message);
RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<>(sessionFactory());
FTPFile[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
assertEquals(1, files.length);
assertEquals(3, files[0].getSize());
registration.destroy();
}
Aggregations