001: package org.claros.chat.ajax;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.ArrayList;
006: import java.util.Collections;
007: import java.util.Iterator;
008:
009: import javax.servlet.ServletException;
010: import javax.servlet.http.HttpServlet;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013: import javax.servlet.http.HttpSession;
014:
015: import org.claros.chat.models.Contact;
016: import org.claros.chat.utility.RosterComparator;
017: import org.jivesoftware.smack.Roster;
018: import org.jivesoftware.smack.RosterEntry;
019: import org.jivesoftware.smack.XMPPConnection;
020: import org.jivesoftware.smack.packet.Presence;
021: import org.jivesoftware.smack.packet.RosterPacket;
022:
023: public class Contacts extends HttpServlet {
024:
025: /**
026: *
027: */
028: private static final long serialVersionUID = -3626097157006365935L;
029:
030: /**
031: * Constructor of the object.
032: */
033: public Contacts() {
034: super ();
035: }
036:
037: /**
038: * The doGet method of the servlet. <br>
039: *
040: * This method is called when a form has its tag value method equals to get.
041: *
042: * @param request the request send by the client to the server
043: * @param response the response send by the server to the client
044: * @throws ServletException if an error occurred
045: * @throws IOException if an error occurred
046: */
047: public void doGet(HttpServletRequest request,
048: HttpServletResponse response) throws ServletException,
049: IOException {
050: doPost(request, response);
051: }
052:
053: /**
054: * The doPost method of the servlet. <br>
055: *
056: * This method is called when a form has its tag value method equals to post.
057: *
058: * @param request the request send by the client to the server
059: * @param response the response send by the server to the client
060: * @throws ServletException if an error occurred
061: * @throws IOException if an error occurred
062: */
063: public void doPost(HttpServletRequest request,
064: HttpServletResponse response) throws ServletException,
065: IOException {
066:
067: // get the printwriter and prepare the html
068: response.setHeader("Expires", "-1");
069: response.setHeader("Pragma", "no-cache");
070: response.setHeader("Cache-control", "no-cache");
071: response.setHeader("Content-Type", "text/html; charset=utf-8");
072:
073: PrintWriter out = response.getWriter();
074:
075: XMPPConnection conn = (XMPPConnection) request.getSession()
076: .getAttribute("conn");
077:
078: if (conn != null) {
079: Roster roster = conn.getRoster();
080: roster.setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);
081: ArrayList contacts = new ArrayList();
082: RosterEntry en = null;
083: Contact cn = null;
084: for (Iterator i = roster.getEntries(); i.hasNext();) {
085: en = (RosterEntry) i.next();
086: cn = new Contact();
087: cn.setName(en.getName());
088: if (cn.getName() == null) {
089: cn.setName(en.getUser());
090: if (cn.getName().indexOf("@") > 0) {
091: cn.setName(cn.getName().substring(0,
092: cn.getName().indexOf("@")));
093: }
094: }
095:
096: RosterPacket.ItemStatus st = en.getStatus();
097: if (st != null
098: && st
099: .equals(RosterPacket.ItemStatus.SUBSCRIPTION_PENDING)) {
100: cn.setStatus("pending");
101: }
102:
103: Presence pr = roster.getPresence(en.getUser());
104: if (pr != null) {
105: Presence.Mode md = pr.getMode();
106: if (md != null) {
107: if (md.equals(Presence.Mode.AVAILABLE)) {
108: cn.setPresence("available");
109: } else if (md.equals(Presence.Mode.AWAY)) {
110: cn.setPresence("away");
111: } else if (md.equals(Presence.Mode.CHAT)) {
112: cn.setPresence("chat");
113: } else if (md
114: .equals(Presence.Mode.DO_NOT_DISTURB)) {
115: cn.setPresence("disturb");
116: } else if (md
117: .equals(Presence.Mode.EXTENDED_AWAY)) {
118: cn.setPresence("extended_away");
119: } else if (md.equals(Presence.Mode.INVISIBLE)) {
120: cn.setPresence("invisible");
121: }
122: if (pr.getStatus() != null
123: && !pr.getStatus().equals("")) {
124: cn.setMessage(pr.getStatus());
125: }
126: }
127: }
128: if (cn.getPresence() == null) {
129: cn.setPresence("offline");
130: }
131: cn.setUser(en.getUser());
132: contacts.add(cn);
133:
134: }
135: Collections.sort(contacts, new RosterComparator());
136: request.getSession().setAttribute("contacts", contacts);
137:
138: // all contacts are set, now generate the output
139: Contact tmp = null;
140: for (int i = 0; i < contacts.size(); i++) {
141: tmp = (Contact) contacts.get(i);
142: // if (!tmp.getPresence().equals("offline")) {
143: out
144: .println("<div id=\"contact\" onclick=\"openChat(this);\" ondblclick=\"openChat(this);\" onmouseover=\"showInfoWin(this);\" onmouseout=\"unhoverContact(this);\" "
145: + "user=\""
146: + tmp.getUser()
147: + "\" username=\""
148: + tmp.getName()
149: + "\" presence=\""
150: + tmp.getPresence()
151: + "\" msg=\""
152: + tmp.getMessage()
153: + "\">");
154: out
155: .println("<table width=\"134\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" height=\"20\">");
156: out.println(" <tr>");
157: String color = "gray";
158: if (tmp.getPresence().equals("available")) {
159: color = "green";
160: } else if (tmp.getPresence().equals("away")) {
161: color = "orange";
162: } else if (tmp.getPresence().equals("chat")) {
163: color = "green";
164: } else if (tmp.getPresence().equals("disturb")) {
165: color = "red";
166: } else if (tmp.getPresence().equals("extended_away")) {
167: color = "red";
168: } else if (tmp.getPresence().equals("invisible")) {
169: color = "orange";
170: }
171: out
172: .println(" <td width=\"10\"><img src=\"img/indicators/"
173: + color
174: + ".gif\" id=\"indicator\"/></td>");
175: out.println(" <td width=\"124\">");
176: out.println(" <div id=\"contactname\">");
177: out
178: .println(" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"124\" height=\"20\">");
179: out
180: .println(" <tr><td nowrap=\"nowrap\"><a href=\"#\">"
181: + tmp.getName() + "</a></td>");
182: out.println(" </tr>");
183: out.println(" </table>");
184: out.println(" </div>");
185: out.println(" </td>");
186: out.println(" </tr>");
187: out.println("</table>");
188: out.println("</div>");
189: }
190: // }
191: } else {
192: out
193: .print("<br /><br /><br /><div align=\"center\"><img src=\"img/loading.gif\" width=\"32\" height=\"32\">");
194: }
195:
196: out.flush();
197: out.close();
198: }
199:
200: /**
201: * Initialization of the servlet. <br>
202: *
203: * @throws ServletException if an error occure
204: */
205: public void init() throws ServletException {
206: // Put your code here
207: }
208:
209: public static String findNameByUser(HttpSession sess, String user) {
210: ArrayList contacts = (ArrayList) sess.getAttribute("contacts");
211: if (contacts != null) {
212: Contact tmp = null;
213: String adr = null;
214: for (int i = 0; i < contacts.size(); i++) {
215: tmp = (Contact) contacts.get(i);
216: adr = tmp.getUser().substring(0);
217: if (adr.indexOf("@") > 0) {
218: adr = adr.substring(0, adr.indexOf("@"));
219: }
220: if (adr.equals(user)) {
221: if (tmp.getName() != null) {
222: return tmp.getName();
223: }
224: }
225: }
226: }
227: return user;
228: }
229:
230: }
|