I just completed the draft implementation of Javascript filters for Summa and I am posting here to hear some comments. If nobody complains the existing implementation will be likely to stay unchanged. Really the implementation supports any old scripting language supported by the ScriptEngineManager of the JVM, but in practice Javascript will probably be the most important one.
The scripting environment will include two “magic” variables: payload and allowPayload. Unsurprisingly the payload variable contains a reference to the Payload object being processed. The allowPayload variable is a boolean value that defaults to true. If allowPayload is set to false the payload will be dropped from the processing pipeline.
Update: The script filters now have a third magic variable called log sporting the methods log.trace|debug|info|warn|error|fatal(string).
The best way to explain this is probably with an example. To write a Javascript filter for Summa create a file called myFilter.js with the following content:
var record = payload.getRecord();
if (!record.getId().startsWith(record.getBase())) {
record.setId(record.getBase() + "_" + record.getId())
}
if (record.getId().endsWith('taboo')) {
allowPayload = false;
}
This script will make sure that all records have their ids prefixed with their base name, and will filter out any records which id ends with “taboo”.
To plug the script into your filter pipeline you need to stick something like the following in your filter chain configuration:
<xproperties>
<entry>
<key>filter.name</key>
<value class="string">FixRecordIdsandDropTaboos</value>
</entry>
<entry>
<key>summa.filter.sequence.filterclass</key>
<value class="string">dk.statsbiblioteket.summa.common.filter.object.ScriptFilter</value>
</entry>
<entry>
<key>filter.script.url</key>
<value class="string">http://example.com/filters/myFilter.js</value>
</entry>
</xproperties>
I am using the ScriptEngine framework which appeared in Java 6 for all of this, and all in all the development experience has been quite nice. Writing this blog post took almost as long as it took me to write that filter
UPDATE: You can find a list of all available scripting engines at scripting.dev.java.net.
April 1, 2009 at 11:03 am
Add a payloadFeedback to give, well, feedback to Summa and make it possible to inline scripts in the configuration. Do that and you’ve created a really big gun that can be aimed at a lot of targets, including feet.
This opens up for so much quick’n'dirty hacking that I am scared, but go ahead anyway!
April 2, 2009 at 11:08 am
Be careful what you wish for Toke…
April 2, 2009 at 11:21 am
[...] for Trouble? You’ve Come to the Right Place! By kamstrup Toke was asking for trouble yesterday. I would assume that he knew me better by now… With my last commit it is now actually [...]