Skip to main content

Writing your first application router with SailFin

Posted by binod on February 10, 2009 at 6:03 AM PST

When a web server receives an http requests, the server identifies which application need to be run, depending on the context-root of the request. However SIP communication is designed to be between two parties and the message does not contain something like context root to identify the application.

Various programming models based on SIP have some ways to overcome this issue. For example, Microsoft Office Communication Server need you to write MSPL scripts to route the messages appropriately. SipServlet containers like SailFin require application router for this purpose. Take a look at the javadoc for more details.

Application Routers goes beyond electing the application and allow you to create a composition of applications.

Now, lets look at a simple application router, which, based on the domain name of the incoming SIP user, decides which application to be selected. Here is the relevant code snippet.

public class DApplicationRouter implements SipApplicationRouter {
  
    // The properties should be name value pairs. The name should
    // be the domain-name and value should be the appName.
    // The domain name "default" is used to pick any application
    // which use IP address instead of domain name.
    private volatile Map arProps = null;   
    private volatile boolean valid = false;
    private volatile String defaultApp = null;
    private volatile List apps = new ArrayList();
 
   
    public void init(Properties props) {
        this.arProps = new ConcurrentHashMap();
        makePropsCaseInsensitive(props);
        this.defaultApp = this.arProps.get("default");
        this.valid = true;
    }
   
    private void makePropsCaseInsensitive(Properties props) {
        for (Map.Entry entry : props.entrySet()) {
            String key = entry.getKey().toString();
            key = key.trim().toLowerCase();
            if (! key.equals("")) {
                String value = (String) entry.getValue();
                if (value != null) {
                    arProps.put(key, value.trim());
                }
            }
        }
    }


    public void applicationDeployed(List apps) {
        checkValidity();
        this.apps.addAll(apps);
    }

As shown above, the init() method accepts the name value pair of domain name and applications. The last method, applicationDeployed accept the list of applications currently deployed in the container.

Now, lets take a look at the actual selection logic below.

   public SipApplicationRouterInfo 
    getNextApplication(SipServletRequest req, SipApplicationRoutingRegion reg,
    SipApplicationRoutingDirective dir, SipTargetedRequestInfo stri,
    Serializable info) {
        checkValidity();       
        if (req.getLocalAddr() != null) { //Worry about incoming requests only.
            return createRouterInfo(findApp(req), req, reg);
        }
        return createRouterInfo(null, null, null);
    }
   
    private String findApp(SipServletRequest req) {
        Address addr = req.getFrom();
        URI uri = addr.getURI();
        if (uri.isSipURI()) {
            String domain = ((SipURI) uri).getHost();
            if (arProps.containsKey(domain)) {
                String appName = arProps.get(domain);
                if (appName != null && apps.contains(appName)) {
                    return appName;
                }
            }
        }       
        return defaultApp;
    }

When a SIP message reaches server, the above code will get the domain name of the user and then finds its matching configured application.

Please download the full netbeans project here. Once you build the application and produce the jar, it is pretty simple to deploy and configure it..

asadmin deploy --property default=<defaultappname>:<domain1appname>=<appname1> DApplicationRouter.jar

Thats all... SailFin will replace the default alphabetical router with the new router.

Related Topics >>

Comments

restoring the default AR

Hi, is there a way to restore the default application router used by sailfin? I'm planning to deploy my own AR, I would feel more comfortable if I would know how to get sailfin back to normal in case of messing things up .

Just undeploy your custom

Just undeploy your custom router and the default router will be back...

Got it I would like to mention that an AR sample could be found under Sailfin/CVS/JSR289DefaultApplication Router/ Thank you

Those properties would be fed to the init method by the container. The properties in glassfish/sailfin are always name value pairs. In this example, you can configure any number of application-domain combinations. For example, the following configuration would mean that -property default=defaultapp:sun.com=sunapp:ericsson.com=ercissonapp Any request for sun.com domain will be handled by sunapp and ercisson.com will be handled by ericssonapp. All other requests will be handled by the defaultapp. In DAR, the config file location is specified by the system property "javax.servlet.sip.dar.configuration". Now this mechanism is not really cluster friendly [eg: need to specify a NFS path for this file]. So, we thought giving a simple mechanism to specify the properties thru asadmin command would help.

Yes, see "public void init(Properties props)" in the AP implementation In the downloaded project i did not find AVPs (AR conf. file) for I searched JSR 289 C-1 (for DAR) Where to put AR configuration file in AR.jar, Sailfin? Could you explain a bit regards the "--property default" syntax in deployment CLI at the end of the blog? Thank you

Sorry, I did not understand the question. Can you please explain?

nice blog, Deploy the AR with property file? Could you put some light on this issue? Cheers,

thnaks i will try that with D-link DGL-4500 http://www.wireless-reviews.com/routers/dgl-4500-xtreme-n-gaming-router-...