Search in sources :

Example 1 with FileListFilter

use of org.springframework.integration.file.filters.FileListFilter 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 2 with FileListFilter

use of org.springframework.integration.file.filters.FileListFilter in project spring-integration by spring-projects.

the class FileInboundChannelAdapterWithRegexPatternParserTests method regexFilter.

@Test
@SuppressWarnings("unchecked")
public void regexFilter() {
    DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(accessor.getPropertyValue("scanner"));
    Object extractedFilter = scannerAccessor.getPropertyValue("filter");
    assertThat(extractedFilter, is(instanceOf(CompositeFileListFilter.class)));
    Set<FileListFilter<?>> filters = (Set<FileListFilter<?>>) new DirectFieldAccessor(extractedFilter).getPropertyValue("fileFilters");
    Pattern pattern = null;
    for (FileListFilter<?> filter : filters) {
        if (filter instanceof RegexPatternFileListFilter) {
            pattern = (Pattern) new DirectFieldAccessor(filter).getPropertyValue("pattern");
        }
    }
    assertNotNull("expected PatternMatchingFileListFilter", pattern);
    assertEquals("^.*\\.txt$", pattern.pattern());
}
Also used : Pattern(java.util.regex.Pattern) Set(java.util.Set) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) FileListFilter(org.springframework.integration.file.filters.FileListFilter) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) Test(org.junit.Test)

Example 3 with FileListFilter

use of org.springframework.integration.file.filters.FileListFilter in project spring-integration by spring-projects.

the class SftpInboundRemoteFileSystemSynchronizerTests method testCopyFileToLocalDir.

@Test
public void testCopyFileToLocalDir() throws Exception {
    File localDirectory = new File("test");
    assertFalse(localDirectory.exists());
    TestSftpSessionFactory ftpSessionFactory = new TestSftpSessionFactory();
    ftpSessionFactory.setUser("kermit");
    ftpSessionFactory.setPassword("frog");
    ftpSessionFactory.setHost("foo.com");
    SftpInboundFileSynchronizer synchronizer = spy(new SftpInboundFileSynchronizer(ftpSessionFactory));
    synchronizer.setDeleteRemoteFiles(true);
    synchronizer.setPreserveTimestamp(true);
    synchronizer.setRemoteDirectory("remote-test-dir");
    SftpRegexPatternFileListFilter patternFilter = new SftpRegexPatternFileListFilter(".*\\.test$");
    PropertiesPersistingMetadataStore store = spy(new PropertiesPersistingMetadataStore());
    store.setBaseDirectory("test");
    store.afterPropertiesSet();
    SftpPersistentAcceptOnceFileListFilter persistFilter = new SftpPersistentAcceptOnceFileListFilter(store, "foo");
    List<FileListFilter<LsEntry>> filters = new ArrayList<FileListFilter<LsEntry>>();
    filters.add(persistFilter);
    filters.add(patternFilter);
    CompositeFileListFilter<LsEntry> filter = new CompositeFileListFilter<LsEntry>(filters);
    synchronizer.setFilter(filter);
    synchronizer.setBeanFactory(mock(BeanFactory.class));
    synchronizer.afterPropertiesSet();
    SftpInboundFileSynchronizingMessageSource ms = new SftpInboundFileSynchronizingMessageSource(synchronizer);
    ms.setAutoCreateLocalDirectory(true);
    ms.setLocalDirectory(localDirectory);
    ms.setBeanFactory(mock(BeanFactory.class));
    CompositeFileListFilter<File> localFileListFilter = new CompositeFileListFilter<File>();
    localFileListFilter.addFilter(new RegexPatternFileListFilter(".*\\.test$"));
    AcceptOnceFileListFilter<File> localAcceptOnceFilter = new AcceptOnceFileListFilter<File>();
    localFileListFilter.addFilter(localAcceptOnceFilter);
    ms.setLocalFilter(localFileListFilter);
    ms.afterPropertiesSet();
    ms.start();
    Message<File> atestFile = ms.receive();
    assertNotNull(atestFile);
    assertEquals("a.test", atestFile.getPayload().getName());
    // The test remote files are created with the current timestamp + 1 day.
    assertThat(atestFile.getPayload().lastModified(), Matchers.greaterThan(System.currentTimeMillis()));
    Message<File> btestFile = ms.receive();
    assertNotNull(btestFile);
    assertEquals("b.test", 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/a.test").exists());
    assertTrue(new File("test/b.test").exists());
    TestUtils.getPropertyValue(localAcceptOnceFilter, "seenSet", Collection.class).clear();
    new File("test/a.test").delete();
    new File("test/b.test").delete();
    // the remote filter should prevent a re-fetch
    nothing = ms.receive();
    assertNull(nothing);
    ms.stop();
    verify(synchronizer).close();
    verify(store).close();
}
Also used : CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) SftpPersistentAcceptOnceFileListFilter(org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) FileListFilter(org.springframework.integration.file.filters.FileListFilter) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) SftpRegexPatternFileListFilter(org.springframework.integration.sftp.filters.SftpRegexPatternFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) ArrayList(java.util.ArrayList) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) SftpRegexPatternFileListFilter(org.springframework.integration.sftp.filters.SftpRegexPatternFileListFilter) SftpPersistentAcceptOnceFileListFilter(org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) BeanFactory(org.springframework.beans.factory.BeanFactory) Collection(java.util.Collection) SftpRegexPatternFileListFilter(org.springframework.integration.sftp.filters.SftpRegexPatternFileListFilter) File(java.io.File) PropertiesPersistingMetadataStore(org.springframework.integration.metadata.PropertiesPersistingMetadataStore) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SftpPersistentAcceptOnceFileListFilter(org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter) Test(org.junit.Test)

Example 4 with FileListFilter

use of org.springframework.integration.file.filters.FileListFilter in project spring-integration-aws by spring-projects.

the class S3InboundChannelAdapterParserTests method testS3InboundChannelAdapterComplete.

@Test
@SuppressWarnings("unchecked")
public void testS3InboundChannelAdapterComplete() throws Exception {
    assertThat(TestUtils.getPropertyValue(this.s3Inbound, "autoStartup", Boolean.class)).isFalse();
    PriorityBlockingQueue<?> blockingQueue = TestUtils.getPropertyValue(this.s3Inbound, "source.fileSource.toBeReceived", PriorityBlockingQueue.class);
    Comparator<?> comparator = blockingQueue.comparator();
    assertThat(comparator).isSameAs(this.comparator);
    assertThat(this.s3Inbound.getComponentName()).isEqualTo("s3Inbound");
    assertThat(this.s3Inbound.getComponentType()).isEqualTo("aws:s3-inbound-channel-adapter");
    assertThat(TestUtils.getPropertyValue(this.s3Inbound, "outputChannel")).isSameAs(this.s3Channel);
    S3InboundFileSynchronizingMessageSource inbound = TestUtils.getPropertyValue(this.s3Inbound, "source", S3InboundFileSynchronizingMessageSource.class);
    S3InboundFileSynchronizer fisync = TestUtils.getPropertyValue(inbound, "synchronizer", S3InboundFileSynchronizer.class);
    assertThat(TestUtils.getPropertyValue(fisync, "remoteDirectoryExpression", Expression.class).getExpressionString()).isEqualTo("'foo/bar'");
    assertThat(TestUtils.getPropertyValue(fisync, "localFilenameGeneratorExpression")).isNotNull();
    assertThat(TestUtils.getPropertyValue(fisync, "preserveTimestamp", Boolean.class)).isTrue();
    assertThat(TestUtils.getPropertyValue(fisync, "temporaryFileSuffix", String.class)).isEqualTo(".foo");
    String remoteFileSeparator = (String) TestUtils.getPropertyValue(fisync, "remoteFileSeparator");
    assertThat(remoteFileSeparator).isEqualTo("\\");
    CompositeFileListFilter<?> filter = TestUtils.getPropertyValue(fisync, "filter", CompositeFileListFilter.class);
    assertThat(filter).isNotNull();
    Set<FileListFilter<?>> fileFilters = TestUtils.getPropertyValue(filter, "fileFilters", Set.class);
    assertThat(fileFilters).size().isEqualTo(2);
    List<FileListFilter<?>> filters = new ArrayList<>(fileFilters);
    assertThat(filters.get(0)).isInstanceOf(S3SimplePatternFileListFilter.class);
    assertThat(filters.get(1)).isInstanceOf(S3PersistentAcceptOnceFileListFilter.class);
    assertThat(TestUtils.getPropertyValue(fisync, "remoteFileTemplate.sessionFactory")).isSameAs(this.s3SessionFactory);
    assertThat(TestUtils.getPropertyValue(inbound, "fileSource.scanner.filter.fileFilters", Collection.class).contains(this.acceptAllFilter)).isTrue();
    final AtomicReference<Method> genMethod = new AtomicReference<Method>();
    ReflectionUtils.doWithMethods(AbstractInboundFileSynchronizer.class, new ReflectionUtils.MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if ("generateLocalFileName".equals(method.getName())) {
                method.setAccessible(true);
                genMethod.set(method);
            }
        }
    });
    assertThat(genMethod.get().invoke(fisync, "foo")).isEqualTo("FOO.afoo");
}
Also used : S3PersistentAcceptOnceFileListFilter(org.springframework.integration.aws.support.filters.S3PersistentAcceptOnceFileListFilter) AcceptAllFileListFilter(org.springframework.integration.file.filters.AcceptAllFileListFilter) FileListFilter(org.springframework.integration.file.filters.FileListFilter) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) S3SimplePatternFileListFilter(org.springframework.integration.aws.support.filters.S3SimplePatternFileListFilter) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) S3InboundFileSynchronizer(org.springframework.integration.aws.inbound.S3InboundFileSynchronizer) Expression(org.springframework.expression.Expression) Collection(java.util.Collection) S3InboundFileSynchronizingMessageSource(org.springframework.integration.aws.inbound.S3InboundFileSynchronizingMessageSource) ReflectionUtils(org.springframework.util.ReflectionUtils) Test(org.junit.Test)

Example 5 with FileListFilter

use of org.springframework.integration.file.filters.FileListFilter in project spring-integration by spring-projects.

the class FileListFilterFactoryBean method initializeFileListFilter.

private void initializeFileListFilter() {
    if (this.result != null) {
        return;
    }
    FileListFilter<File> createdFilter = null;
    if ((this.filter != null) && (this.filenamePattern != null || this.filenameRegex != null)) {
        throw new IllegalArgumentException("The 'filter' reference is mutually exclusive with " + "either the 'filename-pattern' or 'filename-regex' attribute.");
    }
    if (this.filenamePattern != null && this.filenameRegex != null) {
        throw new IllegalArgumentException("The 'filename-pattern' and 'filename-regex' attributes are mutually exclusive.");
    }
    final List<FileListFilter<File>> filtersNeeded = new ArrayList<FileListFilter<File>>();
    if (!Boolean.FALSE.equals(this.ignoreHidden)) {
        filtersNeeded.add(new IgnoreHiddenFileListFilter());
    }
    // 'filter' is set
    if (this.filter != null) {
        if (Boolean.TRUE.equals(this.preventDuplicates)) {
            filtersNeeded.add(new AcceptOnceFileListFilter<File>());
            filtersNeeded.add(this.filter);
        } else {
            // preventDuplicates is either FALSE or NULL
            filtersNeeded.add(this.filter);
        }
    } else // 'file-pattern' or 'file-regex' is set
    if (this.filenamePattern != null || this.filenameRegex != null) {
        if (!Boolean.FALSE.equals(this.preventDuplicates)) {
            // preventDuplicates is either null or true
            filtersNeeded.add(new AcceptOnceFileListFilter<File>());
        }
        if (this.filenamePattern != null) {
            SimplePatternFileListFilter patternFilter = new SimplePatternFileListFilter(this.filenamePattern);
            if (this.alwaysAcceptDirectories != null) {
                patternFilter.setAlwaysAcceptDirectories(this.alwaysAcceptDirectories);
            }
            filtersNeeded.add(patternFilter);
        }
        if (this.filenameRegex != null) {
            RegexPatternFileListFilter regexFilter = new RegexPatternFileListFilter(this.filenameRegex);
            if (this.alwaysAcceptDirectories != null) {
                regexFilter.setAlwaysAcceptDirectories(this.alwaysAcceptDirectories);
            }
            filtersNeeded.add(regexFilter);
        }
    } else // no filters are provided
    if (Boolean.FALSE.equals(this.preventDuplicates)) {
        filtersNeeded.add(new AcceptAllFileListFilter<File>());
    } else {
        // preventDuplicates is either TRUE or NULL
        filtersNeeded.add(new AcceptOnceFileListFilter<File>());
    }
    if (filtersNeeded.size() == 1) {
        createdFilter = filtersNeeded.get(0);
    } else {
        createdFilter = new CompositeFileListFilter<File>(filtersNeeded);
    }
    this.result = createdFilter;
}
Also used : AcceptAllFileListFilter(org.springframework.integration.file.filters.AcceptAllFileListFilter) IgnoreHiddenFileListFilter(org.springframework.integration.file.filters.IgnoreHiddenFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) FileListFilter(org.springframework.integration.file.filters.FileListFilter) SimplePatternFileListFilter(org.springframework.integration.file.filters.SimplePatternFileListFilter) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) ArrayList(java.util.ArrayList) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) IgnoreHiddenFileListFilter(org.springframework.integration.file.filters.IgnoreHiddenFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) SimplePatternFileListFilter(org.springframework.integration.file.filters.SimplePatternFileListFilter) File(java.io.File)

Aggregations

FileListFilter (org.springframework.integration.file.filters.FileListFilter)11 Test (org.junit.Test)10 CompositeFileListFilter (org.springframework.integration.file.filters.CompositeFileListFilter)9 File (java.io.File)8 AcceptOnceFileListFilter (org.springframework.integration.file.filters.AcceptOnceFileListFilter)6 RegexPatternFileListFilter (org.springframework.integration.file.filters.RegexPatternFileListFilter)5 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)4 SimplePatternFileListFilter (org.springframework.integration.file.filters.SimplePatternFileListFilter)4 Collection (java.util.Collection)3 BeanFactory (org.springframework.beans.factory.BeanFactory)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 Expression (org.springframework.expression.Expression)2 AcceptAllFileListFilter (org.springframework.integration.file.filters.AcceptAllFileListFilter)2 PropertiesPersistingMetadataStore (org.springframework.integration.metadata.PropertiesPersistingMetadataStore)2 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)1 Method (java.lang.reflect.Method)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Pattern (java.util.regex.Pattern)1