Search code examples
ftpspring-integrationspring-integration-sftp

Order in which filters are applied to Inbound ftp adapters in spring integration


I am using spring integration inbound channel adapter as below

inboundAdapter(csf).preserveTimestamp(true)//
              .remoteDirectory(feed.getRemoteDirectory())//
              .regexFilter(feed.getRegexFilter())// regex expression
              .filter(ftpRemoteFileFilter)// remote filter
              .deleteRemoteFiles(feed.getDeleteRemoteF

So I am using a remote filter and the out of the box regex filter . I wanted to know what is the order in which the regex filter and the remote filter are applied . From initial analysis looks like the regex filter comes first , can some one tell me the clas where this decision is made so I can be sure .

If there is no way of knowing the only other alternative will be to use the CompositeFileListFilter .


Solution

  • The code you looking for is in the FtpInboundChannelAdapterSpec and looks like:

    @Override
    public FtpInboundChannelAdapterSpec regexFilter(String regex) {
        return filter(composeFilters(new FtpRegexPatternFileListFilter(regex)));
    }
    
    @SuppressWarnings("unchecked")
    private CompositeFileListFilter<FTPFile> composeFilters(FileListFilter<FTPFile> fileListFilter) {
        CompositeFileListFilter<FTPFile> compositeFileListFilter = new CompositeFileListFilter<>();
        compositeFileListFilter.addFilters(fileListFilter,
                new FtpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "ftpMessageSource"));
        return compositeFileListFilter;
    }
    

    So, as you see when you declare regexFilter, it is composed together with the FtpPersistentAcceptOnceFileListFilter to the the CompositeFileListFilter, where regexFilter is definitely first. And it is first because FtpPersistentAcceptOnceFileListFilter is persistence and that wouldn't be good to store files which might not match the regexp afterwards.

    If you need some more complicated logic, you really should go CompositeFileListFilter way and inject it via filter() option only. I mean you have to combine your regexpFilter into the CompositeFileListFilter instead of regexFilter().

    Note: after moving Java DSL into Core in 5.0, the .filter() option looks like:

    public S filter(FileListFilter<F> filter) {
        this.synchronizer.setFilter(filter);
        return _this();
    }
    

    It overrides any previously provided filters, including regexp. That is done to avoid confusing with the chain of .filter() in favor of CompositeFileListFilter or ChainFileListFilter configured externally.