Java VoIP developers 101
Part 1: Java example on sending SMS, making VoIP calls
Java is a general-purpose, class-based programming language that is specifically designed to have as few implementation dependencies as possible. The Ozeki Phone System provides a Java application, so you can use your good old programming language with your brand new phone system. If you have any questions concerning making/receiving calls or sending/receiving text messages, read this guide, because it contains the most important and relevant information.
- Download example project:ozeki-voip-java-example-source-basic.zip
- Reference manual: HTTP API Online Manual
Part 2: Java example on recording VoIP calls, call routing
1. What you need
- Java IDE (Netbeans, Eclipse ect.)
- Ozeki Phone System installed on your PC (Download now)
- A connected softphone
- A connected SMS service
- Java Development Kit
2. Get started
- Add an API Extension to your PBX
- Make a JAVA project
Installing Netbeans and JDK
First, download and install Java Developer Kit (JDK), you can download its latest version, from here. After installing JDK, do the same, with Netbeans (Figure 1). You can download it from the previous link, too. Meanwhile install Netbeans, give the destination folder.
You must select the folder of the previously installed JDK. After the installation of Netbeans, you can start a new project by selecting File from the menu, New Project item.
3. Receive incoming voice calls
First, select which port receives the incoming calls, using http API at Productivity menu, HTTP API menu item. Here, you need to add a new API extension notification, then you can set the address of incoming calls, using the URL field of Incoming call. In this example, this is the 7778 port of localhost. Now, make a java application which checks this port and gives a http response if a call arrives.
public static Initialize() { ServerSocket listener = new ServerSocket(7778)); while (true){ Socket socket = listener.accept(); String responde = "HTTP/1.1 200 OKrn" + "Content-Type: text/xmlrn" + "Connection: Keep-Alivern" + "Keep-Alive: timeout=15, max=100rnrn" + "<?xml version=1.0" encoding="UTF-8"?>rn" + "<Response>rn" + "<Delay>1</Delay>rn" + "<Speak>Congratulations, this is your first OZML Response command.</Speak>rn" + "<Delay>1</Delay>rn" + "<Speak>For more information on OZML syntax, visit www.ozekiphone.com </Speak>rn" + "</Response>"; OutputStream outputStream = socket.getOutputStream(); outputStream.write(responde.getBytes()); outputStream.flush(); socket.close(); } }
Code example 1 - Java code to receive calls
First of all, make a ServerSocket object and add the port number in its constuctor. You can accept an incoming request by calling the accept() method, after the invitation, you will get back a Socket object. Call the getOutputStream method of socket, in order to give a response back. This leads to an OutputStream. You can send back the appropriate response through this, by calling the write method of outputstream. It waits for a byte array. As a result the String typed response message needs to be converted to a byte typed array. You can manage to do it if you use the getBytes method(). Finally, with the help of the outputStream flush() method, you can send the answer.
4. Make outgoing voice calls
To make an outgoing call, make a sample by a URL class. Then connect to the URL, which was given in the construstor of the URL object. To do this, invite the openConnection() method of the object which sends back a URLConnection typed object. Then convert it to HttpURLConnection format. In this file, set the method of the request and send it, using the connect() method.
private static void Initialize { URL url = new URL("http://ozekixepbx.ip:7780/?command=Call&" + "Dialed=100&" + "ApiExtension=9997&" + "CallerId=1000&" + "CallerDisplayName=John+Smith&" + "Url=http://yoursite.com/callconnected.php&" + "ErrorUrl=http://yoursite.com/error.php"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); }
Code example 2 - Java code to start calls
5. Receive SMS message
Like receiving calls, name a port between HTTP API Extension notifications, which receives the incoming messages. You can do this, if you fill the URL field of API extension notification with Incoming message. You need to watch this port at your Java application.
public static void Initialize() { ServerSocket listener = new ServerSocket(7778) while (true) { Socket socket = listener.accept(); InputStream inputStream = socket.getInputStream(); byte[] b = new byte[inputStream.available()]; inputStream.read(b); System.out.println(new String(b)); } }
Code example 3 - Java code to receiving messages
First of all, make a ServerSocket object, with the help of it, you can check the port, which was previously given. You can accept an incoming request by inviting the accept() method of this object. It gives back a Socket typed object. You can query the incoming http response through this object. Call the getInputStream() method of the socket, which returns with an InputStream typed object. You can make a query through another byte array. You can inform the size of this byte array by the inputstream available() method. Then, you can read the response by the read() method, which parameters has to be that byte array, the already read bytes go to that array. To display the response, make a string typed example, with the parameters of the byte array, so you can get back the same array as the string.
6. Send SMS message
You can send a message - like you make an outgoing call - using a http request. First, you need to make a URL class an example. After, calling the openConnection method of this sample, create a HttpURLConnection type object. In this object, set the method of the request, using setRequestMethod. At the end, send the request with the connection method of this object.
private static void Initialize { URL url = new URL("http://ozekixepbx.ip:7780/?command=SendSms&" + "ApiExtension=9997&" + "Sender=%2b061554669&" + "Recipient=%2b061554670&" + "Recipient=%2b061554670&" + "Message=Hello+World&" + "DeliveryReportURL=http://yoursite.com/smsdelivered.php"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); }
Code example 4 - Java code to send messages
Part 2: Java example on recording VoIP calls, call routing
If you have any questions or need assistance, please contact us at info@ozekiphone.com
More information
- Part1: ASP.NET example on sending SMS, making VoIP calls
- Part1: Java example on sending SMS, making VoIP calls
- Part1: PHP example on sending SMS, making VoIP calls
- Part1: Ruby example on sending SMS, making VoIP calls
- Part1: Perl example on sending SMS, making VoIP calls