|
||||||||||
Chat Sending SampleThis document provides a sample that creates an HTTP servlet (Struts Action) and a SIP servlet that receives a message form the HTTP servlet. Configuration and ProcessingFigure 1 shows the configuration and the basic process of the sample application. As you can see, this application (message sending) consists of the same components as the Click-to-dial functionality. The user interface and startup of the SIP servlet is handled on the HTTP side, and the SIP sequence after the startup is handled on the SIP side.
The sequence of the chat process used by OKISoftphone is described in the tutorial. See the links below: This sample develops the application based on the code in the tutorial. Struts Action ClassList 1 shows the code on the HTTP side. This code is an inherited class of Action in Struts. It describes logic where a Web browser posts a message typed in a form. It takes required information from the received HTTP request, sets it in SipRequestParams, and starts the SIP servlet (messenger) using SipRequestDispatcher.invoke. This application defines the following unique startup parameters for the SIP servlet:
List 1: MessageAction Class public class MessageAction extends BasicUserAction implements DemoConstants, SipConstants { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { // Gets form information. String snd = (String) PropertyUtils.getSimpleProperty(form, "sender"); String rcv = (String) PropertyUtils.getSimpleProperty(form, "receiver"); String msg = (String) PropertyUtils.getSimpleProperty(form, "message"); // Starts the SIP servlet HttpSession session = req.getSession(); ServletContext context = session.getServletContext(); SipRequestDispatcher dispatcher = SipRequestDispatcher.getInstance(this, context, "messenger"); SipRequestParams params = new SipRequestParams(); params.setParam("msg.snd", snd); params.setParam("msg.rcv", rcv); params.setParam("msg.msg", msg); String chatID = (String) dispatcher.invoke("sendMessage", params); return mapping.findForward("success"); } } SIP ServletList 2 shows the implementation of the SIP servlet. sendMessage is a method that is invoked from the HTTP side. This method extracts an API-defined argument from the request attribute, initiates message sending process, and registers the return value and exception information with the request attribute under the given name. This servlet only sends messages requested from the HTTP side. You can extend this servlet to create an application that functions as B2BUA. In the list shown below, exception handling is omitted and the logic is simplified. List 2: MessengerServlet Class public class MessengerServlet extends BasicSipServlet { // Information to be sent after 302 is received. private Map chatMap = new HashMap(); private class Message { public String body; public String snd; public String rcv; Message(String body, String snd, String rcv) { this.body = body; this.snd = rcv; this.rcv = snd; } } public void init() throws ServletException { super.init(); } public String sendMessage(SipRequestParams params) thorws Exception { // Generates an object that stores a message body. String msg = (String) params.getParam("msg.msg"); String snd = (String) params.getParam("msg.snd"); String rcv = (String) params.getParam("msg.rcv"); String body = snd + "\r\n" + msg + ":"; Message message = new Message(body, snd, rcv); // Generates a message ID. String chatID = createChatID(); chatMap.put(chatID, message); sendMessageRequest(chatID, true); // Sets a return value upon successful completion. return chatID; } private void sendMessageRequest(String chatID, boolean isInitiate) throws Exception { Message message = (Message) chatMap.get(chatID); SipApplicationSession appSession = factory.createApplicationSession(); SipServletRequest req = initiateRequest(appSession, "MESSAGE", message.snd, message.rcv); // Sets a message header and body. req.setHeader("X-Chat-ID", chatID); if(! isInitiate) req.setContent(message.body, "text/plain; charset=UTF-8"); // Gets and sets RequestURI from the location server. SipURI rcvURI = (SipURI) factory.createURI(message.rcv); List contactList = getLocationEditor().getContactList(rcvURI, null); URI reqURI = ((Address) contactList.get(0)).getURI(); req.setRequestURI(reqURI); // Sends a message. req.send(); } protected void doRedirectResponse(SipServletResponse res) throws IOException, ServletException { if(res.getStatus() == 302) { // If a 302 response is received, sends the message body. String chatID = res.getHeader("X-Chat-ID"); sendMessageRequest(chatID, false); } } private String createMessageID() { // Method that generates and returns an appropriate ChatID. } } Last Modified:Wed Jan 12 19:41:00 JST 2005 |
||||||||||
|