Tutorial

This project started as a lesson in how to write Postgres Content filters and gradually grew from there. If you are interested in writing a postgres content filter, or enhancing/developing on this project follow this tutorial.

Introduction to Postgres Content filters

Postgres allows filtering/modification of email content, by passing all incoming email content through an open socket (which we can provide). This allows each line of the email to be read or modified, and even to insert new lines into the email. You can read more about Postgres Content filters on the postgres site.

Simplest Filter Ever

Firstly, familiarise yourself with this very very simple filter, it does nothing except pipe content from the incoming port to an outgoing port. It is not as simple as you might imagine as it uses NIO. It contains only two classes NIOProxy and NIOProxyChild. The first class, creates a single thread, which listens for incoming connections, and spawns off an NIOProxyChild for every incoming connection, which lives only as long as the connection remains open. You should be very comfortable with this code as the next example is reasonably more complicated.

View this example in the source code repository.

Basic Content filter

The first example is almost copmletely useless. We would like to read each line of data as it comes through, as lines (not data blocks). To do this we will use a standard way of doing this called a Java Pattern, it adds two new classes, the LineParser and LineResponse.

The LineParser is used to drain all incoming data from a bytebuffer. The LineParser spits out an event every time a complete line is recognised. The LineParser is initialised with a pointer to an object that will be called every time a complete line is recognied.

The LineResponse is an interface, which defines a single function which an object must implement to receive line event messages from the LineParser.

View this example in the source code repository.

Auto Whitelist filter

Now here is where we get to the good stuff. This project builds upon the last example, by adding a generic interface called a PostgresContentFilter. The "Basic content filter" is modified to pipe all input lines through an implementation of the PostgresContentFilter.

We implemented the Auto Whitelist Filter, by implmenting an instance of the PostgresContentFilter. We reccomend that if you would like to write your own Java based PostgresContentFilter, you write your own class which implenets the PostgresContentFilter, and submit it back to this project for other peoples benefit.

Changes and modifications to the base source are welcome, it will be considered if it enables you to use the base system for your own project, encouraging code reuseability.