Andrey
Andrey

Reputation: 480

What 'filterDirectory' of Camel File component does?

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?

Upvotes: 0

Views: 611

Answers (1)

jwwallin
jwwallin

Reputation: 118

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

Upvotes: 4

Related Questions