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