Brekeke SIP Server Wiki

4. Sample Code: sample.java

package  plugin ;

import java.util.* ;


public class sample
{

  // Parameters:
  //   arg  - arguments
  //   sippacket  - SIP packet
  //   pr  - properties



  // say hello
  // 
  //   Syntax:
  //     $sample.hello
  // 
  //   Returns:
  //     "hello"
  static public String hello( String[] arg, com.brekeke.net.sip.SIPpacket sippacket, Properties pr ) throws Exception
  {
    // return the string.
    return ( "hello" ) ;
  }



  // reverse string
  // 
  //   Syntax:
  //     $sample.reverse( string )
  // 
  //   Returns:
  //     reversed string
  static public String reverse( String[] arg, com.brekeke.net.sip.SIPpacket sippacket, Properties pr ) throws Exception
  {
    if ( ( arg != null ) && ( arg[0] != null) ) {

      String  str = arg[0] ;
      StringBuffer  buf = new StringBuffer() ;
      int  m = str.length() - 1;
      int  n ; 

      for ( n = m; n >= 0; --n ) {
        buf.append( str.charAt( n ) ) ;
      }

      return ( buf.toString() ) ;
    }

    return ( null ) ;
  }



  // get new address based on the username
  // 
  //   Syntax:
  //     $sample.alias( username )
  // 
  //   Returns:
  //     aliased SIP-URI
  static public String alias( String[] arg, com.brekeke.net.sip.SIPpacket sippacket, Properties pr ) throws Exception
  {
    if ( ( arg != null ) && ( arg[0] != null) ) {
      
      String  username = arg[0] ;


      // return original SIP uri based on the username
      if ( username.equals( "user1" ) ) {
        return ( "sip:test1@domain1.com" ) ;
      }

      if ( username.equals( "user2" ) ) {
        return ( "sip:test2@domain2.com" ) ;
      }

      if ( username.equals( "user3" ) ) {
        return ( "sip:test3@domain3.com" ) ;
      }      

    }

    return ( null ) ;
  }


  // get admin's SIP-URI
  // 
  //   Syntax:
  //     $sample.adminuri
  // 
  //   Returns:
  //     admin's SIP-URI
  static public String adminuri( String[] arg, com.brekeke.net.sip.SIPpacket sippacket, Properties pr ) throws Exception
  {

    return ( pr.getProperty( "sv.admin.sip" ) ) ;

  }


  // compare strings
  // 
  //   Syntax:
  //     $sample.equals( string1, string2, [string3] )
  //
  //   Returns:
  //     "true" or "false"
  static public boolean equals( String[] arg, com.brekeke.net.sip.SIPpacket sippacket, Properties pr ) throws Exception
  {

    if ( ( arg == null ) || ( arg.length < 2 ) ) {
      return ( true ) ;
    }


    int  n ;
    if ( arg[0] == null ) {

      for ( n = 1; n < arg.length - 1; ++n ) {
        if ( arg[n] != null ) {
          return ( false ) ;
        }
      }

      return ( true ) ;
    }


    for ( n = 0; n < arg.length - 1; ++n ) {
      if ( arg[n].equals( arg[n + 1] ) == false ) {
        return ( false ) ;
      }
    }

    return ( true ) ;
  }


  // get uri from the web
  // 
  //   Syntax:
  //     $sample.webget
  // 
  //   Returns:
  //     new SIP-URI
  static public String webget( String[] arg, com.brekeke.net.sip.SIPpacket sippacket, Properties pr ) throws Exception
  {

    // For example, if To header's SIP uri is "sip:username@brekeke.com",
    // the new SIP uri is obtained from "http://brekeke.com/username.txt".


    try {
      // get SIP uri from To header
      String  sipuri = sippacket.getUri( sippacket.getValue( "To" ) ) ;

      int  i = sipuri.indexOf( "@" ) ;
      if ( i > 0 ) {

        // get user name
        String  username = sipuri.substring( 4, i ) ;

        // get domain name
        String  domainname = sippacket.getDomainFromUri( sipuri ) ;

        // compose the web connection
        java.net.URL  weburl = new java.net.URL( "http://" + domainname + "/" + username + ".txt" ) ;
        java.io.BufferedReader in = new java.io.BufferedReader( new java.io.InputStreamReader( weburl.openStream() ) ) ;

        // get new SIP uri
        String  newsipuri = in.readLine().trim() ;

        in.close();


       if ( newsipuri.startsWith( "sip:" ) ) {
          return ( newsipuri ) ;
        }
      }

    }
    catch ( Exception ex ) {
    }


    return ( null ) ;
  }



}
Yes No
Suggest Edit