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
081: .setSubscriptionMode(Roster.SubscriptionMode.accept_all);
082: ArrayList contacts = new ArrayList();
083: RosterEntry en = null;
084: Contact cn = null;
085: for (Iterator i = roster.getEntries().iterator(); i
086: .hasNext();) {
087: try {
088: en = (RosterEntry) i.next();
089: cn = new Contact();
090: cn.setName(en.getName());
091:
092: if (en.getUser().indexOf("msn") == 0) {
093: System.out.println("ok");
094: }
095:
096: if (cn.getName() == null || cn.getName().equals("")) {
097: cn.setName(en.getUser());
098: if (cn.getName().indexOf("@") > 0) {
099: cn.setName(cn.getName().substring(0,
100: cn.getName().indexOf("@")));
101: }
102: }
103:
104: if (cn.getName().indexOf("%") > 0) {
105: cn.setName(cn.getName().substring(0,
106: cn.getName().indexOf("%")));
107: }
108:
109: RosterPacket.ItemStatus st = en.getStatus();
110: if (st != null
111: && st
112: .equals(RosterPacket.ItemStatus.SUBSCRIPTION_PENDING)) {
113: cn.setStatus("pending");
114: }
115:
116: Presence pr = roster.getPresence(en.getUser());
117: if (pr != null) {
118: Presence.Type tp = pr.getType();
119: if (tp.equals(Presence.Type.available)) {
120: cn.setPresence("available");
121:
122: Presence.Mode md = pr.getMode();
123: if (md != null) {
124: if (md.equals(Presence.Mode.available)) {
125: cn.setPresence("available");
126: } else if (md
127: .equals(Presence.Mode.away)) {
128: cn.setPresence("away");
129: } else if (md
130: .equals(Presence.Mode.chat)) {
131: cn.setPresence("chat");
132: } else if (md.equals(Presence.Mode.dnd)) {
133: cn.setPresence("disturb");
134: } else if (md.equals(Presence.Mode.xa)) {
135: cn.setPresence("extended_away");
136: }
137: if (pr.getStatus() != null
138: && !pr.getStatus().equals("")) {
139: cn.setMessage(pr.getStatus());
140: }
141: }
142: }
143: }
144: if (cn.getPresence() == null) {
145: cn.setPresence("offline");
146: }
147: cn.setUser(en.getUser());
148: contacts.add(cn);
149: } catch (Exception e) {
150: e.printStackTrace();
151: }
152: }
153:
154: Collections.sort(contacts, new RosterComparator());
155: request.getSession().setAttribute("contacts", contacts);
156:
157: // all contacts are set, now generate the output
158: Contact tmp = null;
159: out.print("<!-- contacts -->");
160: for (int i = 0; i < contacts.size(); i++) {
161: try {
162: tmp = (Contact) contacts.get(i);
163: if (tmp.getPresence().equals("offline")) {
164: out
165: .println("<div id=\"contact\" onclick=\"openChat(this);\" ondblclick=\"openChat(this);\" onmouseover=\"showInfoWin(this);\" onmouseout=\"unhoverContact(this);\" "
166: + "user=\""
167: + tmp.getUser()
168: + "\" username=\""
169: + tmp.getName()
170: + "\" presence=\""
171: + tmp.getPresence()
172: + "\" msg=\""
173: + tmp.getMessage()
174: + "\" style=\"display:none;\">");
175: } else {
176: out
177: .println("<div id=\"contact\" onclick=\"openChat(this);\" ondblclick=\"openChat(this);\" onmouseover=\"showInfoWin(this);\" onmouseout=\"unhoverContact(this);\" "
178: + "user=\""
179: + tmp.getUser()
180: + "\" username=\""
181: + tmp.getName()
182: + "\" presence=\""
183: + tmp.getPresence()
184: + "\" msg=\""
185: + tmp.getMessage() + "\">");
186: }
187:
188: out
189: .println("<table width=\"134\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" height=\"20\">");
190: out.println(" <tr>");
191: String color = "gray";
192: if (tmp.getPresence().equals("available")) {
193: color = "green";
194: } else if (tmp.getPresence().equals("away")) {
195: color = "orange";
196: } else if (tmp.getPresence().equals("chat")) {
197: color = "green";
198: } else if (tmp.getPresence().equals("disturb")) {
199: color = "red";
200: } else if (tmp.getPresence()
201: .equals("extended_away")) {
202: color = "red";
203: } else if (tmp.getPresence().equals("invisible")) {
204: color = "orange";
205: }
206: out
207: .println(" <td width=\"10\"><img src=\"images/chat/indicators/"
208: + color
209: + ".gif\" id=\"indicator\"/></td>");
210: out.println(" <td width=\"124\">");
211: out.println(" <div id=\"contactname\">");
212: out
213: .println(" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"124\" height=\"20\">");
214: out
215: .println(" <tr><td nowrap=\"nowrap\"><a href=\"#\">"
216: + tmp.getName() + "</a></td>");
217: out.println(" </tr>");
218: out.println(" </table>");
219: out.println(" </div>");
220: out.println(" </td>");
221: out.println(" </tr>");
222: out.println("</table>");
223: out.println("</div>");
224: } catch (Exception e) {
225: e.printStackTrace();
226: }
227: }
228: } else {
229: out
230: .print("<br /><br /><br /><div align=\"center\"><img src=\"images/chat/loading.gif\" width=\"32\" height=\"32\">");
231: }
232: }
233:
234: /**
235: * Initialization of the servlet. <br>
236: *
237: * @throws ServletException if an error occure
238: */
239: public void init() throws ServletException {
240: // Put your code here
241: }
242:
243: public static String findNameByUser(HttpSession sess, String user) {
244: ArrayList contacts = (ArrayList) sess.getAttribute("contacts");
245: if (contacts != null) {
246: Contact tmp = null;
247: String adr = null;
248: for (int i = 0; i < contacts.size(); i++) {
249: tmp = (Contact) contacts.get(i);
250: adr = tmp.getUser().substring(0);
251: if (adr.indexOf("@") > 0) {
252: adr = adr.substring(0, adr.indexOf("@"));
253: }
254: if (adr.equals(user)) {
255: if (tmp.getName() != null) {
256: return tmp.getName();
257: }
258: }
259: }
260: }
261: return user;
262: }
263:
264: }
|