The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


SailFin CAFE: Adding communication capabilities to web applications made (very) simple.

Posted by binod on November 4, 2009 at 7:56 PM PST

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.

  • Download and install latest SailFin V2 promoted build.
  • Download and install latest SailFin CAFE build (b12 or later).
  • Package the application using the normal web application packaging format. Use your favorite IDE to do that. For compilation, please use SAILFIN_HOME/lib/communication-api.jar.
  • Start/Restart SailFin and deploy the application (eg: asadmin deploy firstcafewebapp.war).
  • Start two X-Lite phones.
  • Point your browser window/tab to
    http://localhost:8080/firstcafewebapp/CommunicatingServlet?party1=alice@example.com&party2=bob@example.com
    Or use curl
    curl -G -d "party1=alice@example.com&party2=bob@example.com" http://localhost:8080/firstcafewebapp/CommunicatingServlet
    Accept the phone calls as it reaches the X-Lite phones.
  • Watch this space for more information as I explain, (many) other features, spec compliance, etc.


    Related Topics >> Blogs      Glassfish      J2EE      Java Enterprise      Open Source      Servlets      Web Applications      
    Comments
    Comments are listed in date ascending order (oldest first)