Issue
I wonder what is the purpose of 'filterDirectory' parameter in Camel File component. I didn't managed to find any examples with this property. According to docs:
Filters the directory based on Simple language. For example to filter on current date, you can use a simple date pattern such as ${date:now:yyyMMdd}.
I wrote some sample route to check it behaviour:
public class FilterDirectoryRoute extends RouteBuilder {
private static final String FILE = "file:";
private static final String START_DIR = "C:/test/camel/filterDirectoryRoute";
private final Map<String, String> params = Map.of(
"noop", "true",
"recursive", "true",
"filterDirectory", "${date:now:yyyy/DDD}"
);
@Override
public void configure() {
from(FILE + START_DIR + fileParams(params))
.log(DEBUG, log, "date = ${date:now:yyyy/DDD}")
.log(DEBUG, log, "body = ${body}")
.end();
}
}
There is next structure of filterDirectoryRoute
directory:
π¦filterDirectoryRoute
┣ π2020
┃ ┣ π1.txt
┃ ┣ π105.txt
┃ ┗ π15.txt
┗ π2021
┃ ┣ π268
┃ ┃ ┗ π268.txt
┃ ┣ π269
┃ ┃ ┗ π269.txt
┃ ┣ π270
┃ ┃ ┗ π270.txt
┃ ┣ π271
┃ ┃ ┗ π271.txt
┃ ┣ π272
┃ ┃ ┗ π272.txt
With such configuration the route just traverse all files in directory and read it. So the filterDirectory
has no effect as it doesn't enter 2021/270
(270 is the current day of the year).
If I delete recursive
parameter no files are processed as there are no files in starting directory directly.
So the question is: how filterDirectory
option can be used and what it does?
Solution
It seems to me that the filterDirectory
query-parameter is used in conjunction with the File Language. That means that you would have to construct a filter that in essence produces a boolean result. Something akin to filterDirectory="${file:name} == ${date:now:yyyy/DDD}"
.
NOTE: I have not verified this myself.
EDIT: I have now verified my hunch
The following works and only reads the file 2.txt
.
@Component
public class SampleFileRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file://folder?recursive=true&filterDirectory=${file:name} == 2021")
.log("${header.CamelFileRelativePath}");
}
}
π¦folder
┣ π2020
┃ ┣ π1.txt
┗ π2021
┃ ┣ π2.txt
Answered By - jwwallin