KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > claros > chat > ajax > Listener


1 package org.claros.chat.ajax;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServlet JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 import org.claros.chat.controllers.TrafficController;
13 import org.claros.chat.models.Queue;
14 import org.claros.chat.threads.ChatListener;
15 import org.claros.chat.utility.Utility;
16 import org.jivesoftware.smack.XMPPConnection;
17
18 public class Listener extends HttpServlet JavaDoc {
19
20     /**
21      *
22      */

23     private static final long serialVersionUID = -800529893679431406L;
24
25     /**
26      * Constructor of the object.
27      */

28     public Listener() {
29         super();
30     }
31
32     /**
33      * The doGet method of the servlet. <br>
34      *
35      * This method is called when a form has its tag value method equals to get.
36      *
37      * @param request the request send by the client to the server
38      * @param response the response send by the server to the client
39      * @throws ServletException if an error occurred
40      * @throws IOException if an error occurred
41      */

42     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
43             throws ServletException JavaDoc, IOException JavaDoc {
44
45         // get the printwriter and prepare the html
46
response.setContentType("text/html");
47         response.setHeader("Expires","-1");
48         response.setHeader("Pragma","no-cache");
49         response.setHeader("Cache-control","no-cache");
50         response.setHeader("Content-Type", "text/html; charset=utf-8");
51
52         PrintWriter JavaDoc out = response.getWriter();
53
54         XMPPConnection conn = (XMPPConnection)request.getSession().getAttribute("conn");
55         if (conn != null) {
56             String JavaDoc user = conn.getUser();
57             if (user != null) {
58                 ChatListener listener = TrafficController.getListener(user);
59                 if (listener != null) {
60                     List JavaDoc msgs = listener.getUnreadMessages();
61                     
62                     if (msgs != null && msgs.size() > 0) {
63                         Queue msg = null;
64                         String JavaDoc from = null;
65                         String JavaDoc body = null;
66                         for (int i=0;i<msgs.size(); i++) {
67                             msg = (Queue)msgs.get(i);
68                             from = msg.getMsgFrom();
69
70                             String JavaDoc userOnly = from;
71                             if (userOnly.indexOf("@") > 0) {
72                                 userOnly = from.substring(0, from.indexOf("@"));
73                             }
74                             
75                             body = msg.getMsgBody();
76                             String JavaDoc myMsg = tidyMsg(body);
77                             String JavaDoc output = "openChat(null, \"" + from + "\", \"" + Contacts.findNameByUser(request.getSession(), userOnly) + "\", \"" + myMsg + "\", \"in\");";
78                             out.print(output);
79                         }
80                     }
81                 }
82             }
83         }
84         out.flush();
85         out.close();
86     }
87
88     /**
89      *
90      * @param msg
91      * @return
92      */

93     private String JavaDoc tidyMsg(String JavaDoc msg) {
94         msg = Utility.replaceAllOccurances(msg, "\"", "\\\"");
95         msg = Utility.replaceAllOccurances(msg, "\n", "<clarosbr><clarosbr>");
96         if (msg.endsWith("<clarosbr><clarosbr>")) {
97             msg += "<clarosbr><clarosbr>";
98         }
99         
100         /*
101         msg = Utility.replaceAllOccurances(msg, ":)", "<img SRC='img/emotions/regular_smile.gif' />");
102         msg = Utility.replaceAllOccurances(msg, ":-)", "<img SRC='img/emotions/regular_smile.gif' />");
103         msg = Utility.replaceAllOccurances(msg, ":-O", "<img SRC='img/emotions/omg_smile.gif' />");
104         msg = Utility.replaceAllOccurances(msg, ":-o", "<img SRC='img/emotions/omg_smile.gif' />");
105         msg = Utility.replaceAllOccurances(msg, ":O", "<img SRC='img/emotions/omg_smile.gif' />");
106         msg = Utility.replaceAllOccurances(msg, ":o", "<img SRC='img/emotions/omg_smile.gif' />");
107         msg = Utility.replaceAllOccurances(msg, ";-)", "<img SRC='img/emotions/wink_smile.gif' />");
108         msg = Utility.replaceAllOccurances(msg, ";)", "<img SRC='img/emotions/wink_smile.gif' />");
109         msg = Utility.replaceAllOccurances(msg, ":S", "<img SRC='img/emotions/confused_smile.gif' />");
110         msg = Utility.replaceAllOccurances(msg, ":s", "<img SRC='img/emotions/confused_smile.gif' />");
111         msg = Utility.replaceAllOccurances(msg, ":'(", "<img SRC='img/emotions/cry_smile.gif' />");
112         msg = Utility.replaceAllOccurances(msg, ":D", "<img SRC='img/emotions/teeth_smile.gif' />");
113         msg = Utility.replaceAllOccurances(msg, ":d", "<img SRC='img/emotions/teeth_smile.gif' />");
114         msg = Utility.replaceAllOccurances(msg, ":-P", "<img SRC='img/emotions/tongue_smile.gif' />");
115         msg = Utility.replaceAllOccurances(msg, ":p", "<img SRC='img/emotions/tongue_smile.gif' />");
116         msg = Utility.replaceAllOccurances(msg, ":(", "<img SRC='img/emotions/sad_smile.gif' />");
117         msg = Utility.replaceAllOccurances(msg, ":|", "<img SRC='img/emotions/what_smile.gif' />");
118         msg = Utility.replaceAllOccurances(msg, ":-@", "<img SRC='img/emotions/angry_smile.gif' />");
119         msg = Utility.replaceAllOccurances(msg, ":@", "<img SRC='img/emotions/angry_smile.gif' />");
120         */

121         return msg;
122     }
123     
124     /**
125      * The doPost method of the servlet. <br>
126      *
127      * This method is called when a form has its tag value method equals to post.
128      *
129      * @param request the request send by the client to the server
130      * @param response the response send by the server to the client
131      * @throws ServletException if an error occurred
132      * @throws IOException if an error occurred
133      */

134     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
135             throws ServletException JavaDoc, IOException JavaDoc {
136         doGet(request, response);
137     }
138
139 }
140
Popular Tags