1

I have an ftp inbound endpoint where a lot of files are getting stored (not all for me) so I added a filename-regex-filter with the different regular expressions that I want to fetch from the FTP directory:

<file:filename-regex-filter pattern="
                            ${environment}\.TEST.xml,
                            0\.${environment}\.REPD\.(.*).GZ,
                            ${environment}(.*)PERSONNOTI(.*)\.xml\.gz,
                            ${environment}(.*)PERSONNOTI(.*)voucher\.xml" caseSensitive="false" />

This works great, but now I want to execute specific behavior based on the type of file. Therefore I use a choice component with different when expressions:

My first idea was to use a choice component with when expressions that use the same regular expressions that are used in the filename-regex-filter and that are working there:

<when expression="#[message.inboundProperties.originalFilename.matches('0\.${environment}\.REPD\.(.*).GZ')]">
    // Specific behaviour for this type of files
</when>    

This produced following error message:

 Exception stack is:
 1. [Error: illegal escape sequence: .]
 [Near : {... age.inboundProperties.originalFilename.matches('0\.T\.REPD\.(.*).GZ') ....}]
                                                              ^
 [Line: 1, Column: 55] (org.mule.api.expression.InvalidExpressionException) 
  org.mule.el.mvel.MVELExpressionLanguage:238(http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/expression/InvalidExpressionException.html)
 2. [Error: illegal escape sequence: .]
 [Near : {... age.inboundProperties.originalFilename.matches('0\.T\.REPD(.*).GZ') ....}]     

This means the expression attribute doensn't expect escape characters, so I decided not to escape them

<when expression="#[message.inboundProperties['originalFilename'].matches('${environment}.TEST.xml')]">
    //Specific behavior for this type of files...          
</when>

<when expression="#[message.inboundProperties.originalFilename.matches('0.${environment}.REPD.*.GZ')]">
    //Specific behavior for this type of files...
</when>

<otherwise>
    <file:outbound-endpoint path="C:/test/result/other" responseTimeout="10000" outputPattern="#[message.inboundProperties.originalFilename]"/>
</otherwise> 

Here the message is always routed towards the otherwise clause, except for the first expression where no wildcards are used...

Is it a good practice to work with a when expressions, and what is the correct way of putting the expression?

Mathias G.
  • 4,875
  • 3
  • 39
  • 60
  • 1
    Your input mixes with your regular expression so if you include those regex special characters in the input, they eventually break the syntax of your regex. So you either need to fit them correctly into your existing regex or escape them all: `*` to `\*`, `.` to `\.`, `[` to `\[`, ` \ ` to ` \\ ` and so on. [Which characters need escaping.](http://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions). Maybe your language already has something like `Regex.Escape()` method like some other languages do. Then just use it on your input sequence. – miroxlav Aug 01 '15 at 18:27
  • Could you please move it from comment to the question? (Including code you used for escaping.) This will make your question more complete with your research to date and easier to answer by someone who knows this area. – miroxlav Aug 02 '15 at 08:51

1 Answers1

0

I found a solution that works for now, but I think there must be a better solution using regular expressions:

<file:inbound-endpoint responseTimeout="10000" connector-ref="input" path="C:/test">
        <file:filename-regex-filter pattern="
                            0\.${environment}\.REPD\.(.*).GZ,
                            ${environment}(.*)PERSONNOTI(.*)\.xml\.gz,
                            ${environment}(.*)PERSONNOTI(.*)voucher\.xml" caseSensitive="false" />
    </file:inbound-endpoint>
    <choice>
        <when expression="#[(message.inboundProperties.originalFilename.toLowerCase()).startsWith('0.${environment}.REPD'.toLowerCase()) &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).endsWith('gz')]">
            <gzip-uncompress-transformer/>
            // Specific behaviour for this type of files 
        </when>
        <when expression="#[(message.inboundProperties.originalFilename.toLowerCase()).startsWith('${environment}'.toLowerCase()) &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).contains('personnoti') &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).endsWith('.xml.gz')]">
            // Specific behaviour for this type of files
        </when>
        <when expression="#[(message.inboundProperties.originalFilename.toLowerCase()).startsWith('${environment}'.toLowerCase()) &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).contains('personnoti') &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).endsWith('voucher.xml')]">
            // Specific behaviour for this type of files
        </when>
    </choice>

I searched for hours on the internet, but didn't find a valid solution for my problem. If anyone has an idea how this could be solved using regex or other evaluators, I'm very curious...

Mathias G.
  • 4,875
  • 3
  • 39
  • 60