Skip to content

Filter example: accessing the filtered metadata


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 static com.funnelback.filter.api.DocumentType.*;

public class DumpFilterMetadata implements StringDocumentFilter {

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

    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);
            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);

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

  "\""+key+"\" : ["+valuestring+"]")


See also:


Funnelback logo