Skip to content

Filter example: accessing the filtered metadata

Description

This example shows how to access the multimap that is used to store metadata added via the filters and implements a filter that iterates over the metadata and prints out the values to the filter log.

Iterate over the stored metadata

The following example iterates over the metadata multimap containing all the metadata added via filters (using metadata.put() calls).


package com.funnelback.filter;

import com.funnelback.filter.api.*;
import com.funnelback.filter.api.documents.*;
import com.funnelback.filter.api.filters.*;
import com.funnelback.filter.api.mock.*;
import com.google.common.collect.ListMultimap;
import static com.funnelback.filter.api.DocumentType.*;

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


    public PreFilterCheck canFilter(NoContentDocument document, FilterContext context) {
        return PreFilterCheck.ATTEMPT_FILTER;
    }

    @Override
    public FilterResult filterAsStringDocument(StringDocument document, FilterContext context) throws RuntimeException,
        FilterException {

        // Print out the key and set of values as "key" : ["val1","val2","val3"]

        ListMultimap<String, String> metadata = document.getCopyOfMetadata();

        Set<String> keySet = metadata.keySet().sort();
        Iterator keyIterator = keySet.iterator();
        while (keyIterator.hasNext()) {
            def valuestring ="";
            String key = (String) keyIterator.next();
            Collection<String> values = metadata.get(key).sort();

            // Iterate over all the values for the key
            Iterator valueIterator = values.iterator()
            while(valueIterator.hasNext()) {
                // get the next value
                String value = (String) valueIterator.next();

                // eg. add the value to a string
                valuestring += "\""+value+"\""
                if (valueIterator.hasNext()) {
                    valuestring +=","
                }
            }

            log.info("\""+key+"\" : ["+valuestring+"]")
        }

    }
}

See also:

top

Funnelback logo
v15.24.0