Java program for connecting to the FreeSWITCH XML-RPC

I would like to show simple Java program which use XML-RPC interface to do some freeswitch commands. We should on freeswitch console load mod_xml_rpc: Worth checking out if it works, in web browser type:
http://fshost8080/webapi/help
If you see "FreeSWITCH help" it works.

Next we download Apache XML-RPC library.

Now we create Java program (using Apache XML-RPC):
package fstest1;

import java.net.URL;
import java.util.Scanner;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class FsTest1 {

  public static void main(String[] args) {
    System.out.println("------ hello freeswitch -------\n");    
    XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
    XmlRpcClient client = new XmlRpcClient();
    try {
      config.setServerURL(new URL("http://fshost:8080/RPC2"));
      config.setBasicUserName("freeswitch");
      config.setBasicPassword("works");

      client.setConfig(config);
      
      System.out.println("--- call 1000 1001 (press Enter) ---");
      new Scanner(System.in).nextLine();
      String resp = (String)client.execute("freeswitch.api", new Object[]{"originate", "user/1000 1001 xml default"});
      System.out.println(resp);
            
      System.out.println("--- show channels (press Enter) ---");
      new Scanner(System.in).nextLine();
      resp = (String)client.execute("freeswitch.api", new Object[]{"show", "channels"});
      System.out.println(resp);
      int idx = resp.indexOf("\n");
      resp = resp.substring(idx);
      idx = resp.indexOf(",");
      String uuid = resp.substring(1, idx);
      
      System.out.println("--- uuid_transfer " + uuid + " 5000 (press Enter) ---");
      new Scanner(System.in).nextLine();
      resp = (String)client.execute("freeswitch.api", new Object[]{"uuid_transfer", uuid + " 5000"});
      System.out.println(resp);
      
      System.out.println("--- show channels (press Enter) ---");
      new Scanner(System.in).nextLine();
      resp = (String)client.execute("freeswitch.api", new Object[]{"show", "channels"});
      System.out.println(resp);
      
      System.out.println("--- uuid_kill " + uuid + " (press Enter) ---");
      new Scanner(System.in).nextLine();
      resp = (String)client.execute("freeswitch.api", new Object[]{"uuid_kill", uuid});
      System.out.println(resp);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
    System.out.println("--- bye freeswitch ---");
  }
}

After running this simple Java program we can see on Java console:
------ hello freeswitch -------

--- call 1000 1001 (press Enter) ---

+OK 0e913ce9-6c86-4f28-a6ed-7356084873bc

--- show channels (press Enter) ---

uuid,direction,(...),sent_callee_num
0e913ce9-6c86-4f28-a6ed-7356084873bc,outbound,(...),1001
cb6a78bf-ce83-4269-9af2-c100991adff3,outbound,(...),1000

2 total.

--- uuid_transfer 0e913ce9-6c86-4f28-a6ed-7356084873bc 5000 (press Enter) ---

+OK

--- show channels (press Enter) ---

uuid,direction,(...),cid_num,(...), dest,(...)
0e913ce9-6c86-4f28-a6ed-7356084873bc,outbound,(...)1000,(...),5000,(...)

1 total.

--- uuid_kill 0e913ce9-6c86-4f28-a6ed-7356084873bc (press Enter) ---

+OK

--- bye freeswitch ---
A lot of fun :)

Komentarze

Popularne posty z tego bloga

AngularJS example MotoAds more advanced than the tutorial on angularjs.org

Java ESL program for connecting to the FreeSWITCH