I’m happy to announce that Tropo finally has a Java based WebAPI implementation.
This implementation will allow every Java developer to create Tropo based applications with a very simple and lightweight API. This API hides all the communication and protocol details, making it much easier to deploy Tropo applications in a Java environment. It can be used with any Java program, including Application Servers like Tomcat, WebSphere or WebLogic as well as stand-alone applications.
As the other WebAPI implementations do, the Java WebAPI library offers a very simple but comprehensive DSL that can be used to create Tropo applications with syntax very close to natural language. However, in contrast to other implementations, the Java WebAPI is statically typed, which means we can catch errors at compile time. This makes it even easier for developers to create Tropo applications. Previous to this implementation, developers who wanted to use Tropo with Java had to create JSON documents by hand, inherently unfriendly and error-prone.
The sources of the Java WebAPI implementation are available from GitHub as usual, including documentation to help get you started. You can download the latest version of this library from the distribution folder, where you will also find all the required dependencies (of which there are not many). Building the Java WebAPI requires Apache Maven and is just a one line: mvn clean install.
There are plenty of examples on the project’s page at GitHub; I’ll share a couple of them now so you can see how easy it is to create Tropo applications in Java. Let’s start with a simple Java Servlet that will be the entry point to our Tropo application. Tropo sends a POST request to our application server each time a person calls a number, sends an SMS to our app, etc. – we just have to implement the doPost method from our Servlet.
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Tropo tropo = new Tropo();
tropo.say("Hello from Tropo. This is our first application.");
tropo.render(response);
}
How cool is that? No JSON, no response handling, just plain and easy Java code! Let’s look at another example – imagine you have a Tropo hosted application, which sends SMS to a number; this number is passed to Tropo as a parameter. You have a Java standalone application that you want to hook into this hosted app, in order to send an SMS based on a particular criteria (like when a file download is finished). You can simply use this Java implementation to launch your hosted application using the application’s token and pass the phone number at the same time. It would be as simple as this:
public void sendSMS(String number) {
String token = ...
Tropo tropo = new Tropo();
Map params = new HashMap();
params.put("number", number);
tropo.launchSession(token, params);
}
Finally, the Java WebAPI also offers classes to deal with all results and incoming information from Tropo. For example, checking Tropo’s session object is very easy:
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Tropo tropo = new Tropo();
TropoSession session = tropo.session(request);
System.out.println("Call id: " + session.getCallId());
}
As you can see, writing Tropo applications in Java has never been that easy. We hope you like this new WebAPI implementation!