Using advanced file filters in File Inbound endpoint

Using advanced file filters in File Inbound endpoint

Mule ESB allows you to connect to anything and anywhere using a wide range of and endpoints. File connector is one of the commonly used connectors that allows you to read/write files with file systems. There are two ways to use file connector –

  • Inbound Endpoint – When file connector is used at the beginning of the flow, it acts as an inbound endpoint where you can receive files for processing.
  • Outbound Endpoint – When used anywhere inside the flow, it becomes an outbound endpoint where you can write files to file systems.

When file connector is used as inbound endpoint, it is very common to be selective about which files should be received and processed. This can be achieved by using Filters. Mule ESB provides two simple file filters to use with

  • Filename regex filter: select the files with names matching to given regex. <file:filename-regex-filter pattern=”inbound(.*).csv”/>
  • Filename wildcard filter: The filename-wildcard-filter element configures a filter that can be used to restrict the files being processed by applying wildcard expressions to the filename. For example, you can read only .xml and .txt files by entering the following: <file:filename-wildcard-filter pattern=”*.txt,*.xml”/>

Apart from these filters, fileAge property on connector can also be used to select files older than a certain time.

Although these three options may be enough for common scenarios, there can be situations where complex file filters are required to further narrow down the list of files being processed. In this article, I will try to show an option that will allow you to use any file filter available in Apache commons-io.

Apache Commons IO ships with Mule runtime and provides various file filters that can easily be used in Mule application. Here is a snapshot of filters provided by apache commons io –

Custom FileFilterWrapper:

File inbound endpoint element only allows configuring filter that implements java.io.FileFilter or java.io.FilenameFilter. So Let’s create a FileFilterWrapper class that implements all required interfaces.


Notes about above class –

  1. org.mule.api.routing.filter.Filter is a required interface to make this class a valid Mule Filter type.
  2. org.mule.api.routing.filter.ObjectFilter is an optional interface that allows the filter to validate any object.
  3. We have added a property ioFileFilter that will hold the instance of any file filter from apache commons. accept(File) method uses this filter to validate the incoming file.

Using FileFilterWrapper:

To use this new wrapper in our mule config, let’s create global spring beans as below –


Once we have these beans, we can create a mule flow and refer to our new filter –


Now when this flow runs, it will only process the files that are accepted by ioFileFilter configured in our FileFilterWrapper. In this case, it will only process files with size/length less than 10240.

Now you can configure any filter from apache commons io or even chain multiple filters using AndFileFilter or OrFileFilter to narrow down the files being processed.

Hope this helps you to process more efficiently! If you like these type of tutorials check out our other official howto guides or our Training Talks series.


 


We'd love to hear your opinion on this post