Search in sources :

Example 21 with FTPFile

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;
}
Also used : NetworkConnection(dev.dworks.apps.anexplorer.network.NetworkConnection) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) NetworkFile(dev.dworks.apps.anexplorer.network.NetworkFile) MatrixCursor(dev.dworks.apps.anexplorer.cursor.MatrixCursor)

Example 22 with FTPFile

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();
}
Also used : FtpRegexPatternFileListFilter(org.springframework.integration.ftp.filters.FtpRegexPatternFileListFilter) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) FtpRegexPatternFileListFilter(org.springframework.integration.ftp.filters.FtpRegexPatternFileListFilter) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) SpelParserConfiguration(org.springframework.expression.spel.SpelParserConfiguration) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) FileListFilter(org.springframework.integration.file.filters.FileListFilter) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) FtpRegexPatternFileListFilter(org.springframework.integration.ftp.filters.FtpRegexPatternFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter) Expression(org.springframework.expression.Expression) Collection(java.util.Collection) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) PropertiesPersistingMetadataStore(org.springframework.integration.metadata.PropertiesPersistingMetadataStore) RecursiveDirectoryScanner(org.springframework.integration.file.RecursiveDirectoryScanner) Test(org.junit.Test)

Example 23 with FTPFile

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()));
        }
    }
}
Also used : List(java.util.List) FTPFile(org.apache.commons.net.ftp.FTPFile) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 24 with FTPFile

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) Test(org.junit.Test)

Example 25 with FTPFile

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();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) Assert.assertThat(org.junit.Assert.assertThat) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) FtpInboundFileSynchronizingMessageSource(org.springframework.integration.ftp.inbound.FtpInboundFileSynchronizingMessageSource) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) FtpStreamingMessageSource(org.springframework.integration.ftp.inbound.FtpStreamingMessageSource) EnableIntegration(org.springframework.integration.config.EnableIntegration) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) FtpTestSupport(org.springframework.integration.ftp.FtpTestSupport) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) FTPFile(org.apache.commons.net.ftp.FTPFile) Matchers.containsString(org.hamcrest.Matchers.containsString) FileCopyUtils(org.springframework.util.FileCopyUtils) QueueChannel(org.springframework.integration.channel.QueueChannel) RunWith(org.junit.runner.RunWith) FileHeaders(org.springframework.integration.file.FileHeaders) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) TestUtils(org.springframework.integration.test.util.TestUtils) MessageSource(org.springframework.integration.core.MessageSource) MessageBuilder(org.springframework.integration.support.MessageBuilder) Pollers(org.springframework.integration.dsl.Pollers) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) Assert.assertNotNull(org.junit.Assert.assertNotNull) FileOutputStream(java.io.FileOutputStream) Matchers(org.hamcrest.Matchers) AbstractRemoteFileOutboundGateway(org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway) IOException(java.io.IOException) Test(org.junit.Test) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) Assert.assertNull(org.junit.Assert.assertNull) FileReader(java.io.FileReader) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) InputStream(java.io.InputStream) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) FTPFile(org.apache.commons.net.ftp.FTPFile) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

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