INT-4215: Add ChainFileListFilter
JIRA: https://jira.spring.io/browse/INT-4215 Fixes GH-1998 (https://github.com/spring-projects/spring-integration/issues/1998) Just make an `CompositeFileListFilter` extension which chains result of the previous filter to the next **Cherry-pick 4.3.x**
This commit is contained in:
committed by
Gary Russell
parent
eaaa21ef5d
commit
d47bca48cc
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.file.filters;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* The {@link CompositeFileListFilter} extension which chains the result
|
||||
* of the previous filter to the next one.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 4.3.7
|
||||
*
|
||||
* @param <F> The type that will be filtered.
|
||||
*/
|
||||
public class ChainFileListFilter<F> extends CompositeFileListFilter<F> {
|
||||
|
||||
@Override
|
||||
public List<F> filterFiles(F[] files) {
|
||||
Assert.notNull(files, "'files' should not be null");
|
||||
List<F> leftOver = Arrays.asList(files);
|
||||
for (FileListFilter<F> fileFilter : this.fileFilters) {
|
||||
@SuppressWarnings("unchecked")
|
||||
F[] fileArray = (F[]) leftOver.toArray();
|
||||
leftOver = fileFilter.filterFiles(fileArray);
|
||||
}
|
||||
return leftOver;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,17 +32,18 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* Simple {@link FileListFilter} that predicates its matches against <b>all</b> of the
|
||||
* configured {@link FileListFilter}.
|
||||
* @param <F> The type that will be filtered.
|
||||
*
|
||||
* @author Iwein Fuld
|
||||
* @author Josh Long
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @param <F> The type that will be filtered.
|
||||
*
|
||||
*/
|
||||
public class CompositeFileListFilter<F> implements ReversibleFileListFilter<F>, ResettableFileListFilter<F>, Closeable {
|
||||
|
||||
private final Set<FileListFilter<F>> fileFilters;
|
||||
protected final Set<FileListFilter<F>> fileFilters; // NOSONAR
|
||||
|
||||
|
||||
public CompositeFileListFilter() {
|
||||
|
||||
@@ -16,26 +16,31 @@
|
||||
|
||||
package org.springframework.integration.file;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayWithSize;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.file.filters.ChainFileListFilter;
|
||||
import org.springframework.integration.file.filters.CompositeFileListFilter;
|
||||
import org.springframework.integration.file.filters.FileListFilter;
|
||||
|
||||
/**
|
||||
* @author Iwein Fuld
|
||||
* @author Gary Russell
|
||||
* @author Aaron Grant
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class CompositeFileListFilterTests {
|
||||
|
||||
@@ -52,7 +57,7 @@ public class CompositeFileListFilterTests {
|
||||
CompositeFileListFilter<File> compositeFileFilter = new CompositeFileListFilter<File>();
|
||||
compositeFileFilter.addFilter(fileFilterMock1);
|
||||
compositeFileFilter.addFilter(fileFilterMock2);
|
||||
List<File> returnedFiles = Arrays.asList(fileMock);
|
||||
List<File> returnedFiles = Collections.singletonList(fileMock);
|
||||
when(fileFilterMock1.filterFiles(isA(File[].class))).thenReturn(returnedFiles);
|
||||
when(fileFilterMock2.filterFiles(isA(File[].class))).thenReturn(returnedFiles);
|
||||
assertEquals(returnedFiles, compositeFileFilter.filterFiles(new File[] { fileMock }));
|
||||
@@ -66,7 +71,7 @@ public class CompositeFileListFilterTests {
|
||||
CompositeFileListFilter<File> compositeFileFilter = new CompositeFileListFilter<File>();
|
||||
compositeFileFilter.addFilter(fileFilterMock1);
|
||||
compositeFileFilter.addFilter(fileFilterMock2);
|
||||
List<File> returnedFiles = Arrays.asList(fileMock);
|
||||
List<File> returnedFiles = Collections.singletonList(fileMock);
|
||||
when(fileFilterMock1.filterFiles(isA(File[].class))).thenReturn(returnedFiles);
|
||||
when(fileFilterMock2.filterFiles(isA(File[].class))).thenReturn(returnedFiles);
|
||||
assertEquals(returnedFiles, compositeFileFilter.filterFiles(new File[] { fileMock }));
|
||||
@@ -86,4 +91,20 @@ public class CompositeFileListFilterTests {
|
||||
assertTrue(compositeFileFilter.filterFiles(new File[] { fileMock }).isEmpty());
|
||||
compositeFileFilter.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void excludeFromLaterFilters() throws Exception {
|
||||
CompositeFileListFilter<File> compositeFileFilter = new ChainFileListFilter<>();
|
||||
compositeFileFilter.addFilter(this.fileFilterMock1);
|
||||
compositeFileFilter.addFilter(this.fileFilterMock2);
|
||||
List<File> noFiles = new ArrayList<>();
|
||||
when(this.fileFilterMock1.filterFiles(isA(File[].class))).thenReturn(noFiles);
|
||||
assertEquals(noFiles, compositeFileFilter.filterFiles(new File[] { this.fileMock }));
|
||||
|
||||
verify(fileFilterMock1).filterFiles(argThat(arrayWithSize(1)));
|
||||
verify(fileFilterMock2).filterFiles(argThat(arrayWithSize(0)));
|
||||
|
||||
compositeFileFilter.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -168,6 +168,12 @@ This applies when the outbound gateway returns an `InputStream` or the new (S)FT
|
||||
Also a new `markers-json` options has been introduced to convert `FileSplitter.FileMarker` to JSON `String` for relaxed downstream network interaction.
|
||||
See <<file-splitter>> for more information.
|
||||
|
||||
===== File Filters
|
||||
|
||||
A new `ChainFileListFilter` is provided as an alternative to `CompositeFileListFilter`.
|
||||
See <<file-reading>> for more information.
|
||||
|
||||
|
||||
==== AMQP Changes
|
||||
|
||||
===== Content Type Message Converter
|
||||
|
||||
@@ -106,6 +106,19 @@ to, say, network glitches.
|
||||
</bean>
|
||||
----
|
||||
|
||||
Starting with _version 4.3.7_ a `ChainFileListFilter` (an extension of `CompositeFileListFilter`) has been introduced to allow scenarios when subsequent filters should only see the result of the previous filter.
|
||||
(With the `CompositeFileListFilter`, all filters see all the files, but only files that pass all filters are passed by the `CompositeFileListFilter`).
|
||||
An example of where the new behavior is required is a combination of `LastModifiedFileListFilter` and `AcceptOnceFileListFilter`, when we do not wish to accept the file until some amount of time has elapsed.
|
||||
With the `CompositeFileListFilter`, since the `AcceptOnceFileListFilter` sees all the files on the first pass, it won't pass it later when the other filter does.
|
||||
The `CompositeFileListFilter` approach is useful when a pattern filter is combined with a custom filter that looks for a secondary indicating file transfer is complete.
|
||||
The pattern filter might only pass the primary file (e.g. `foo.txt`) but the "done" filter needs to see if, say `foo.done` is present.
|
||||
|
||||
Say we have files `a.txt`, `a.done`, and `b.txt`.
|
||||
|
||||
The pattern filter only passes `a.txt` and `b.txt`, the "done" filter will see all three files and only pass `a.txt`.
|
||||
The final result of the composite filter is only `a.txt` is released.
|
||||
|
||||
|
||||
Starting with _version 5.0_ an `ExpressionFileListFilter` has been introduced to allow to execute SpEL expression against file as a context evaluation root object.
|
||||
For this purpose all the XML components for file handling (local and remote), alongside with an existing `filter` attribute, have been supplied with the `filter-expression` option:
|
||||
[source, xml]
|
||||
|
||||
Reference in New Issue
Block a user