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

Search

Online Books:
java.net on MarkMail:


Binod

Binod P.G is a senior staff engineer in the Java Web Services division at Sun Microsystems. He is an architect in the Application Server development team and is working on Project SailFin. He is also a co-specification lead of the Java EE Connector Architecture 1.6 Expert Group and a member of JDBC 4.0 expert group. In the past, he was involved in the development of many areas of the glassfish applicaton server, including Java EE Service Engine, Server Startup, Connector 1.5, JDBC, Connection Pool and JMS provider integration. He is also one of the owners of Generic Resource Adapter for JMS project. Prior to joining Sun in 2000, he has worked on a number of server side software technologies including IMS PL/1 programs in IBM Mainframes and internet projects in Microsoft IIS.

 

Binod's blog

Typing Detection in SailFin CAFE

Posted by binod on November 16, 2009 at 8:22 AM PST


RFC 3994 standardized how SIP applications implement "Indication of Messages" or "Typing detection" at protocol level. SailFin CAFE provides a simple way to detect typing using a Communication Bean. Also a web application can send a "Message Indication" to a SIP client.

Here is the code that implements receiving Instant Messages and Message Indication from a SIP client.

@CommunicationBean
public class SimpleCommsBean {

    @Context CommunicationContext ctx;

    @CommunicationEvent(type=CommunicationEvent.Type.MESSAGEINDICATION)
    void handleMessageIndication() {
        IMConversation conv = (IMConversation) ctx.getCommunication();
        MessageIndication msg = (MessageIndication) ctx.getMessage();
        System.out.println("Message State : " + msg.getState());
        System.out.println("Next Message Type : " + msg.getNextMessageType());
    }

    @CommunicationEvent(type=CommunicationEvent.Type.MESSAGEARRIVED)
    void handleMessage() {
        IMConversation conv = (IMConversation) ctx.getCommunication();
        TextMessage msg = (TextMessage) ctx.getMessage();
        System.out.println("Received Message" + msg.getText());
    }
}

In the code above, the first event gives an indication about the message (eg: whether the user is actively typing, what is the type of next message etc). In the next event, the application receives the actual message. Both these , on a typical SIP Servlet application , would be received as the content of a SIP message and application would need to interpret the content. Message Indication is an xml document, which would otherwise need to be parsed by the SIP Servlet. Here the CAFE framework handles the parsing the message content and provides it as an appropriate POJO to the application.

Similarly, a servlet or JSP would send a Message Indication or Text Message to SIP client by creating an IMConversation. Here is the sample servlet code that send IM message.

   @Context CommunicationSession session;

    @Override
    protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    java.io.IOException {
    
        java.io.PrintWriter out = response.getWriter();
        String party1 = request.getParameter("party1");
        String party2 = request.getParameter("party2");
        try {
            out.println("");
            IMConversation conv  = session.createIMConversation(party1);
            conv.addParticipant(party2);
            conv.createMessageIndication().send();
            conv.createTextMessage("Hi There").send();
            out.println("IMConversation started between " + party1 + "and" + party2);
        } catch (Exception e) {
            out.println(e);
        }
For more information take a look at the javadoc for IMConversation, TextMessage and MessageIndication.




Related Topics >> Blogs      
Comments
Comments are listed in date ascending order (oldest first)
Syndicate content