SailFin CAFE: Adding communication capabilities to web applications made (very) simple.
So far I have described how create server applications that handle call, conference and IM using SailFin CAFE. In this edition lets take a look at how to add communication capabilities to web applications in a (very) simple way.
I like to start with the code. So, here is some code that implements making a phone call between two parties from the web application.
package my.test;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.glassfish.cafe.api.*;
public class CommunicatingServlet extends HttpServlet{
@Context CommunicationSession session;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, java.io.IOException {
java.io.PrintWriter out = response.getWriter();
try {
out.println("");
String party1 = request.getParameter("party1");
String party2 = request.getParameter("party2");
Conversation conv = session.createConversation(party1);
conv.addParticipant(party2);
out.println("Call started between " + party1 + " and " + party2);
} finally {
out.println("");
out.close();
}
}
}
As you can see in the code above, an instance of CommunicationSession object is injected into the HTTP servlet. The session can then be used to create a conversation object specifying the parties in the call. All the further SIP protocol handling will be done by CAFE framework as per RFC 3725, unless the application wishes to modify any behavior (which I will explain another day).
CommunicationSession can be used to create any communication artifacts like conference, conversation, IMConversation etc as per the need of the application. It also share the session between communication bean and HTTP servlet. For example, for creating a conference between three people, the code would be something like the following.
Conference conf = session.createConference();
conf.addParticipant(party1);
conf.addParticipant(party2);
conf.addParticipant(party3);
Similarly, for sending a message, the code would be.
IMConversation conv = session.createIMConversation(party1);
conv.addParticipant(party2);
conv.createTextMessage("Hi There").send();
To run the application, execute the following steps.
http://localhost:8080/firstcafewebapp/CommunicatingServlet?party1=alice@example.com&party2=bob@example.comOr use curl
curl -G -d "party1=alice@example.com&party2=bob@example.com" http://localhost:8080/firstcafewebapp/CommunicatingServletAccept the phone calls as it reaches the X-Lite phones.
- Login or register to post comments
- Printer-friendly version
- binod's blog
- 2333 reads






Comments
Error code 488
by rpisters - 2010-10-14 03:44
When running this example with two X-Lite 4 softphones, I cannot accept the call, and see "488 Not Acceptable Here" in the server log. Works fine with X-Lite 3 though. Thanks Binod.