I’m happy to announce that Tropo has finally got a Java based WebApi implementation.

This implementation will allow every Java developer to create Tropo based applications with a very simple and lightweight API that hides all the communication and protocol details the developers making much easier to deploy Tropo applications in a Java environment. This WebApi implmenetation can be used from any Java program from Application Servers like Tomcat, WebSphere or WebLogic to stand-alone applications.

As other WebApi implementations do, the Java WebApi offers a very simple but comprehensive DSL that allows to create Tropo applications with a 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 making easier to developers to create Tropo applications. Previous to this implementation, developers who wanted to use Tropo in the Java platform had to create JSON documents by hand which was unfriendly and error-prone.

The sources of the Java WebApi implementation are available from GitHub as usual. There is also nice documentation over there that will get you introduced to the Java WebApi. You can download the latest version of this library from the distribution folder where you will also find all the required dependencies (not many). Building the Java WebApi requires Apache Maven and is just a one line: mvn clean install.

There is plenty of examples in the project’s page at GitHub on how to use the Java WebApi but I will share with you a couple of them so you can view how easy will be now to create Tropo applications in Java. Let’s start with a simple Java Servlet that is the entry point to our Tropo application. Tropo will send a POST request to our application server each times a person calls a number, sends an SMS to our app, etc. So 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! But let me write another example. Let’s imagine now that you have a Tropo hosted application that sends SMS to a number that is provided as a parameter, and you have a Java standalone application that you want it to send SMS messages. You can simply use the Java WebApi and invoke your hosted application using the application’s token. It would be as simply 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 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!

Originally from Voxeo Blogs