Writing TrafficScript functions in Java

ZXTM's Java Extensions are useful for managing application traffic, but you can also use the GenericServlet interface to write new TrafficScript functions using a Java Extension.

The following code sample illustrates how to create a TrafficScript subroutine called soundex(); the Soundex algorithm calculates a phonetic representation of a word, and can be used to determine if two words sound similar. It's most usefully used to compare surnames, to detect related names or simple misspellings.

The TrafficScript code

First, the TrafficScript code for the new soundex() subroutine:

sub soundex( $word ) {
java.run( "Soundex", $word );
return connection.data.get( "soundex" );
}

This code declares a TrafficScript subroutine called soundex that takes one argument, then calls a Java Extension and passes it the value of that argument.

Note that, as per the Java Servlet API, Java Extensions cannot return values directly. The easiest way to return a value is to set connection local data in the Extension, then look the value up in TrafficScript using connection.data.get().

The Java Extension

The Java Extension uses the GenericServlet API (more details) and implements a new service() method:

import java.io.IOException;
import javax.servlet.*;
import com.zeus.ZXTMServlet.ZXTMServletRequest;
public class Soundex extends GenericServlet {
private static final long serialVersionUID = 1L;
public void service( ServletRequest req, ServletResponse res )
throws IOException
{
String[] args = (String[])req.getAttribute( "args" );
String result = doSoundex( args[0] );
((ZXTMServletRequest)req).setConnectionData( "soundex", result );
}
static String soundex = "01230120022455012623010202";
String doSoundex( String s ) {
s = s.toUpperCase();
StringBuilder r = new StringBuilder();
char last = '0';
if( s.length() > 0 ) last = s.charAt( 0 );
r.append( last );
for( int i = 1; i < s.length(); i++ ) {
int j = s.charAt( i )-'A';
char next = ( j >= 0 && j < soundex.length() ) ? soundex.charAt( j ) : '0';
if( next != last && next != '0' ) { r.append( next ); last = next; }
if( r.length() >= 4 ) break;
}
while( r.length() < 4 ) r.append( '0' );
return r.toString();
}
}

Compile and upload the extension class file to your ZXTM.

Using the new TrafficScript function

Here's a simple TrafficScript request rule that you can assign to a Generic Client-First virtual server (ensure that it's set to 'Run Every', not 'Run once'):

sub soundex( $word ) {
java.run( "Soundex", $word );
return connection.data.get( "soundex" );
}
$word = string.trim( request.getLine() );
request.sendResponse( "That sounds like " . soundex( $word ) . "\n" );

Connect to the virtual server using telnet, and type in a few words:

This is just a toy example, and you could probably, with a little more work, implement a soundex algorithm directly in TrafficScript, but this example serves to illustrate how you can create new TrafficScript subroutines using implementations in Java.

Owen Garrett [Zeus Dev Team] 15 October 2008 Bookmark with del.icio.us Post this article to Digg Post this article to reddit Post this article to Facebook Tweet this article  
Leave a comment ...
Your email address will not be displayed.
Your URL will be displayed.
This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)

Recently...

Other Resources