Filter example - binary document manipulation


Filters allow manipulating documents at a byte level, which is particularly useful for binary documents such as pdf. In this example a filter is written that Base64 decodes all documents. It decodes the given bytes and returns a document with the decoded bytes. As we don’t assume the document to be a String this filter could be given a Base64 encoded pdf and produce pdf bytes which could be filtered in another filter.


This example implements the ByteDocumentFilter. We are required to implement canFilter(), used to check if the filter should be run, as well as filterAsBytesDocument() which contains the logic for the filter.

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

package com.myfilters;

import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.*;
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.*;

public class Base64DecodingFilter implements BytesDocumentFilter {

  public PreFilterCheck canFilter(NoContentDocument document, FilterContext context) {
    // Pre filter checks could be done here, we assume all documents are
    // Base64 encoded.
    return PreFilterCheck.ATTEMPT_FILTER;

  public FilterResult filterAsBytesDocument(BytesDocument document, FilterContext context) {
    // Gets the document contents as raw bytes and decode the bytes
    byte[] decodedBytes = Base64.getDecoder().decode(document.getCopyOfContents());

    log.debug("Decoding resulted in a " + decodedBytes.length + " byte content for: " + document.getURI());

    // Make a clone of the document updating the content to be the decoded bytes.
    // We keep the existing document type and charset.
    BytesDocument newDocument = document.cloneWithContent(
    return FilterResult.of(newDocument);

   * Below are filter test methods.
  public static class FilterTest {
    public void simpleDecodeTest() throws Exception {
      // Create the input document with base 64 encoded bytes.
      BytesDocument inputDoc = MockDocuments.mockEmptyByteDoc()

      // Create and run the filter, getting the filtered result.
      FilterResult result = new Base64DecodingFilter()
        .filter(inputDoc, MockFilterContext.getEmptyContext());

      // Get the resulting document from filtering,
      // there should be exactly one document.
      BytesDocument document = (BytesDocument) result

      // Convert the document content to a string.
      String decodedString = new String(document.getCopyOfContents(), UTF_8);

      // Check that we correctly decoded the document.
      	"The content was not correctly decoded",
      	"Hello Funnelback",

  // Running the main method will execute the test methods.
  public static void main(String[] args) throws Exception {


© 2015- Squiz Pty Ltd