Skip to content

Filter example: read collection configuration options

Description

Filters allow reading collection config options from the FilterContext. Although in this example StringDocumentFilter is implemented, both ByteDocumentFilter and Filter can be used to read collection config options from the FilterContext.

Example

In this example we read filter.should.run from the configuration to decide if the filter should run. We also read filter.append to find what should be appended to the document. This example implements the StringDocumentFilter. We are required to implement canFilter(), used to check if the filter should run based on the config option filter.should.run, as well as filterAsStringDocument() which contains the logic for the filter.

This example also has a simple test method which can be executed by running the main method see testing Groovy filters.

package com.myfilters;

import org.junit.*;
import org.junit.Test;
import com.funnelback.filter.api.*;
import com.funnelback.filter.api.documents.*;
import com.funnelback.filter.api.filters.*;
import com.funnelback.filter.api.mock.*;

@groovy.util.logging.Log4j2
public class ConfigUsingFilter implements StringDocumentFilter {

    @Override
    public PreFilterCheck canFilter(NoContentDocument document, FilterContext context) {
        //Get the 'filter.should.run' config option, if it is not set we assume the default value to be 'false'
        String shouldRun = context.getConfigValue("filter.should.run").orElse("false"); 
        if("true".equals(shouldRun)) {
            return PreFilterCheck.ATTEMPT_FILTER;
        }
        return PreFilterCheck.SKIP_FILTER;
    }

    @Override
    public FilterResult filterAsStringDocument(StringDocument document, FilterContext context) {
        //Get the value to append to the document from the collection config option 'filter.append'.
        //If that config option is not defined we append the default value of '-default'
        String valueToAppend = context.getConfigValue("filter.append").orElse("-default");

        log.debug("Appending: '" + valueToAppend + "' to '" + document.getURI() + "'");

        //Create new content appending the value
        String newContent = document.getContentAsString() + valueToAppend;

        //Create a new document with the new content preserving all other attributes.
        StringDocument filteredDocument = document.cloneWithStringContent(document.getDocumentType(), newContent);

        return FilterResult.of(filteredDocument);
    }

    /*
     * Below are filter test methods. 
     */
    public static class FilterTest {

        @Test
        public void filterNotRuningTest() {
            //Create a mock context with collection.cfg option 'filter.should.run' set to 'false'
            //This way we can run the test without need a collection.
            MockFilterContext context = MockFilterContext.getEmptyContext();
            context.setConfigValue("filter.should.run", "false");

            FilterResult filterResult = new ConfigUsingFilter().filter(MockDocuments.mockEmptyByteDoc(), context);

            Assert.assertTrue("Filter should have been skipped because of collection config option", 
                                    filterResult.isSkipped());
        }

        @Test
        public void filterAppendsValueFromConfig() {
            //Create a mock context with collection.cfg option 'filter.should.run' set to 'false'
            //as well as setting the value to append to the document.
            //This way we can run the test without needing a collection.
            MockFilterContext context = MockFilterContext.getEmptyContext();
            context.setConfigValue("filter.should.run", "true");
            context.setConfigValue("filter.append", "finish");

            StringDocument inputDoc = MockDocuments.mockEmptyStringDoc()
                                        .cloneWithStringContent(DocumentType.MIME_UNKNOWN, "Start to ");

            FilterResult filterResult = new ConfigUsingFilter().filter(inputDoc, context);

            Assert.assertFalse("Filter should NOT have been skipped because filter.should.run=true", 
                                    filterResult.isSkipped());

            StringDocument filteredDocument = (StringDocument) filterResult.getFilteredDocuments().get(0); 

            Assert.assertEquals("'finish' should have been appended to the document", 
                "Start to finish", filteredDocument.getContentAsString());

        }
    }

    //Running the main method will execute the test methods.
    public static void main(String[] args) throws Exception {
        FilterTestRunner.runTests(FilterTest.class);
    }

}

See also:

top

Funnelback logo
v15.16.0