vADC Docs

Writing TrafficScript functions in Java

by on ‎03-05-2013 05:39 AM - edited on ‎06-11-2015 05:21 PM by PaulWallace (846 Views)

Stingray's Feature Brief: Java Extensions in Stingray Traffic Manager are useful for managing HTTP traffic, but you can also use the GenericServlet interface to implement 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 and resolve simple misspellings.

 

The TrafficScript code

 

First, the TrafficScript code for our 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 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 Stingray.

 

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( "   '".$word."' sounds like " . soundex( $word ) . "\n" );  

 

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

Screen Shot 2013-03-05 at 13.36.48.png

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.

 

Read more

 

Contributors