Chapter 16. The File Language Abstract The file language is an extension to the simple language, not an independent language in its own right. But the file language extension can only be used in conjunction with File or FTP endpoints. 16.1. When to Use the File Language Overview The file language is an extension to the simple language which is not always available. You can use it under the following circumstances: In a File or FTP consumer endpoint . On exchanges created by a File or FTP consumer . Note The escape character, \ , is not available in the file language. In a File or FTP consumer endpoint There are several URI options that you can set on a File or FTP consumer endpoint, which take a file language expression as their value. For example, in a File consumer endpoint URI you can set the fileName , move , preMove , moveFailed , and sortBy options using a file expression. In a File consumer endpoint, the fileName option acts as a filter, determining which file will actually be read from the starting directory. If a plain text string is specified (for example, fileName=report.txt ), the File consumer reads the same file each time it is updated. You can make this option more dynamic, however, by specifying a simple expression. For example, you could use a counter bean to select a different file each time the File consumer polls the starting directory, as follows: Where the USD{bean:counter.} expression invokes the () method on the bean registered under the ID, counter . The move option is used to move files to a backup location after then have been read by a File consumer endpoint. For example, the following endpoint moves files to a backup directory, after they have been processed: Where the USD{file:name.noext}.bak expression modifies the original file name, replacing the file extension with .bak . You can use the sortBy option to specify the order in which file should be processed. For example, to process files according to the alphabetical order of their file name, you could use the following File consumer endpoint: To process file according to the order in which they were last modified, you could use the following File consumer endpoint: You can reverse the order by adding the reverse: prefix - for example: On exchanges created by a File or FTP consumer When an exchange originates from a File or FTP consumer endpoint, it is possible to apply file language expressions to the exchange throughout the route (as long as the original message headers are not erased). For example, you could define a content-based router, which routes messages according to their file extension, as follows: 16.2. File Variables Overview File variables can be used whenever a route starts with a File or FTP consumer endpoint, which implies that the underlying message body is of java.io.File type. The file variables enable you to access various parts of the file pathname, almost as if you were invoking the methods of the java.io.File class (in fact, the file language extracts the information it needs from message headers that have been set by the File or FTP endpoint). Starting directory Some of file variables return paths that are defined relative to a starting directory , which is just the directory that is specified in the File or FTP endpoint. For example, the following File consumer endpoint has the starting directory, ./filetransfer (a relative path): The following FTP consumer endpoint has the starting directory, ./ftptransfer (a relative path): Naming convention of file variables In general, the file variables are named after corresponding methods on the java.io.File class. For example, the file:absolute variable gives the value that would be returned by the java.io.File.getAbsolute() method. Note This naming convention is not strictly followed, however. For example, there is no such method as java.io.File.getSize() . Table of variables Table 16.1, "Variables for the File Language" shows all of the variable supported by the file language. Table 16.1. Variables for the File Language Variable Type Description file:name String The pathname relative to the starting directory. file:name.ext String The file extension (characters following the last . character in the pathname). Supports file extensions with multiple dots, for example, .tar.gz. file:name.ext.single String The file extension (characters following the last . character in the pathname). If the file extension has mutiple dots, then this expression only returns the last part. file:name.noext String The pathname relative to the starting directory, omitting the file extension. file:name.noext.single String The pathname relative to the starting directory, omitting the file extension. If the file extension has multiple dots, then this expression strips only the last part, and keep the others. file:onlyname String The final segment of the pathname. That is, the file name without the parent directory path. file:onlyname.noext String The final segment of the pathname, omitting the file extension. file:onlyname.noext.single String The final segment of the pathname, omitting the file extension. If the file extension has multiple dots, then this expression strips only the last part, and keep the others. file:ext String The file extension (same as file:name.ext ). file:parent String The pathname of the parent directory, including the starting directory in the path. file:path String The file pathname, including the starting directory in the path. file:absolute Boolean true , if the starting directory was specified as an absolute path; false , otherwise. file:absolute.path String The absolute pathname of the file. file:length Long The size of the referenced file. file:size Long Same as file:length . file:modified java.util.Date Date last modified. 16.3. Examples Relative pathname Consider a File consumer endpoint, where the starting directory is specified as a relative pathname . For example, the following File endpoint has the starting directory, ./filelanguage : Now, while scanning the filelanguage directory, suppose that the endpoint has just consumed the following file: And, finally, assume that the filelanguage directory itself has the following absolute location: Given the preceding scenario, the file language variables return the following values, when applied to the current exchange: Expression Result file:name test/hello.txt file:name.ext txt file:name.noext test/hello file:onlyname hello.txt file:onlyname.noext hello file:ext txt file:parent filelanguage/test file:path filelanguage/test/hello.txt file:absolute false file:absolute.path /workspace/camel/camel-core/target/filelanguage/test/hello.txt Absolute pathname Consider a File consumer endpoint, where the starting directory is specified as an absolute pathname . For example, the following File endpoint has the starting directory, /workspace/camel/camel-core/target/filelanguage : Now, while scanning the filelanguage directory, suppose that the endpoint has just consumed the following file: Given the preceding scenario, the file language variables return the following values, when applied to the current exchange: Expression Result file:name test/hello.txt file:name.ext txt file:name.noext test/hello file:onlyname hello.txt file:onlyname.noext hello file:ext txt file:parent /workspace/camel/camel-core/target/filelanguage/test file:path /workspace/camel/camel-core/target/filelanguage/test/hello.txt file:absolute true file:absolute.path /workspace/camel/camel-core/target/filelanguage/test/hello.txt

Commands:
file://target/filelanguage/bean/?fileName=USD{bean:counter.next}.txt&delete=true
file://target/filelanguage/?move=backup/USD{date:now:yyyyMMdd}/USD{file:name.noext}.bak&recursive=false
file://target/filelanguage/?sortBy=file:name
file://target/filelanguage/?sortBy=file:modified
file://target/filelanguage/?sortBy=reverse:file:modified
<from uri="file://input/orders"/> <choice> <when> <simple>USD{file:ext} == 'txt'</simple> <to uri="bean:orderService?method=handleTextFiles"/> </when> <when> <simple>USD{file:ext} == 'xml'</simple> <to uri="bean:orderService?method=handleXmlFiles"/> </when> <otherwise> <to uri="bean:orderService?method=handleOtherFiles"/> </otherwise> </choice>
file:filetransfer
ftp://myhost:2100/ftptransfer
file://filelanguage
./filelanguage/test/hello.txt
/workspace/camel/camel-core/target/filelanguage
file:///workspace/camel/camel-core/target/filelanguage
./filelanguage/test/hello.txt