001: package org.claros.chat.ajax;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.List;
006:
007: import javax.servlet.ServletException;
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import org.claros.chat.controllers.TrafficController;
013: import org.claros.chat.models.Queue;
014: import org.claros.chat.threads.ChatListener;
015: import org.claros.chat.utility.Utility;
016: import org.jivesoftware.smack.XMPPConnection;
017:
018: public class Listener extends HttpServlet {
019:
020: /**
021: *
022: */
023: private static final long serialVersionUID = -800529893679431406L;
024:
025: /**
026: * Constructor of the object.
027: */
028: public Listener() {
029: super ();
030: }
031:
032: /**
033: * The doGet method of the servlet. <br>
034: *
035: * This method is called when a form has its tag value method equals to get.
036: *
037: * @param request the request send by the client to the server
038: * @param response the response send by the server to the client
039: * @throws ServletException if an error occurred
040: * @throws IOException if an error occurred
041: */
042: public void doGet(HttpServletRequest request,
043: HttpServletResponse response) throws ServletException,
044: IOException {
045:
046: // get the printwriter and prepare the html
047: response.setContentType("text/html");
048: response.setHeader("Expires", "-1");
049: response.setHeader("Pragma", "no-cache");
050: response.setHeader("Cache-control", "no-cache");
051: response.setHeader("Content-Type", "text/html; charset=utf-8");
052:
053: PrintWriter out = response.getWriter();
054:
055: XMPPConnection conn = (XMPPConnection) request.getSession()
056: .getAttribute("conn");
057: if (conn != null) {
058: String user = conn.getUser();
059: if (user != null) {
060: ChatListener listener = TrafficController
061: .getListener(user);
062: if (listener != null) {
063: List msgs = listener.getUnreadMessages();
064:
065: if (msgs != null && msgs.size() > 0) {
066: Queue msg = null;
067: String from = null;
068: String body = null;
069: for (int i = 0; i < msgs.size(); i++) {
070: msg = (Queue) msgs.get(i);
071: from = msg.getMsgFrom();
072:
073: String userOnly = from;
074: if (userOnly.indexOf("@") > 0) {
075: userOnly = from.substring(0, from
076: .indexOf("@"));
077: }
078:
079: body = msg.getMsgBody();
080: String myMsg = tidyMsg(body);
081: String output = "openChat(null, \""
082: + from
083: + "\", \""
084: + Contacts.findNameByUser(request
085: .getSession(), userOnly)
086: + "\", \"" + myMsg + "\", \"in\");";
087: out.print(output);
088: }
089: }
090: }
091: }
092: }
093: out.flush();
094: out.close();
095: }
096:
097: /**
098: *
099: * @param msg
100: * @return
101: */
102: private String tidyMsg(String msg) {
103: msg = Utility.replaceAllOccurances(msg, "\"", "\\\"");
104: msg = Utility.replaceAllOccurances(msg, "\n",
105: "<clarosbr><clarosbr>");
106: if (msg.endsWith("<clarosbr><clarosbr>")) {
107: msg += "<clarosbr><clarosbr>";
108: }
109:
110: /*
111: msg = Utility.replaceAllOccurances(msg, ":)", "<img src='img/emotions/regular_smile.gif' />");
112: msg = Utility.replaceAllOccurances(msg, ":-)", "<img src='img/emotions/regular_smile.gif' />");
113: msg = Utility.replaceAllOccurances(msg, ":-O", "<img src='img/emotions/omg_smile.gif' />");
114: msg = Utility.replaceAllOccurances(msg, ":-o", "<img src='img/emotions/omg_smile.gif' />");
115: msg = Utility.replaceAllOccurances(msg, ":O", "<img src='img/emotions/omg_smile.gif' />");
116: msg = Utility.replaceAllOccurances(msg, ":o", "<img src='img/emotions/omg_smile.gif' />");
117: msg = Utility.replaceAllOccurances(msg, ";-)", "<img src='img/emotions/wink_smile.gif' />");
118: msg = Utility.replaceAllOccurances(msg, ";)", "<img src='img/emotions/wink_smile.gif' />");
119: msg = Utility.replaceAllOccurances(msg, ":S", "<img src='img/emotions/confused_smile.gif' />");
120: msg = Utility.replaceAllOccurances(msg, ":s", "<img src='img/emotions/confused_smile.gif' />");
121: msg = Utility.replaceAllOccurances(msg, ":'(", "<img src='img/emotions/cry_smile.gif' />");
122: msg = Utility.replaceAllOccurances(msg, ":D", "<img src='img/emotions/teeth_smile.gif' />");
123: msg = Utility.replaceAllOccurances(msg, ":d", "<img src='img/emotions/teeth_smile.gif' />");
124: msg = Utility.replaceAllOccurances(msg, ":-P", "<img src='img/emotions/tongue_smile.gif' />");
125: msg = Utility.replaceAllOccurances(msg, ":p", "<img src='img/emotions/tongue_smile.gif' />");
126: msg = Utility.replaceAllOccurances(msg, ":(", "<img src='img/emotions/sad_smile.gif' />");
127: msg = Utility.replaceAllOccurances(msg, ":|", "<img src='img/emotions/what_smile.gif' />");
128: msg = Utility.replaceAllOccurances(msg, ":-@", "<img src='img/emotions/angry_smile.gif' />");
129: msg = Utility.replaceAllOccurances(msg, ":@", "<img src='img/emotions/angry_smile.gif' />");
130: */
131: return msg;
132: }
133:
134: /**
135: * The doPost method of the servlet. <br>
136: *
137: * This method is called when a form has its tag value method equals to post.
138: *
139: * @param request the request send by the client to the server
140: * @param response the response send by the server to the client
141: * @throws ServletException if an error occurred
142: * @throws IOException if an error occurred
143: */
144: public void doPost(HttpServletRequest request,
145: HttpServletResponse response) throws ServletException,
146: IOException {
147: doGet(request, response);
148: }
149:
150: }
|