KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.claros.chat.ajax;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.http.HttpServlet JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13 import javax.servlet.http.HttpSession JavaDoc;
14
15 import org.claros.chat.models.Contact;
16 import org.claros.chat.utility.RosterComparator;
17 import org.jivesoftware.smack.Roster;
18 import org.jivesoftware.smack.RosterEntry;
19 import org.jivesoftware.smack.XMPPConnection;
20 import org.jivesoftware.smack.packet.Presence;
21 import org.jivesoftware.smack.packet.RosterPacket;
22
23 public class Contacts extends HttpServlet JavaDoc {
24
25     /**
26      *
27      */

28     private static final long serialVersionUID = -3626097157006365935L;
29
30     /**
31      * Constructor of the object.
32      */

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

47     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
48             throws ServletException JavaDoc, IOException JavaDoc {
49         doPost(request, response);
50     }
51
52     /**
53      * The doPost method of the servlet. <br>
54      *
55      * This method is called when a form has its tag value method equals to post.
56      *
57      * @param request the request send by the client to the server
58      * @param response the response send by the server to the client
59      * @throws ServletException if an error occurred
60      * @throws IOException if an error occurred
61      */

62     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
63             throws ServletException JavaDoc, IOException JavaDoc {
64
65         // get the printwriter and prepare the html
66
response.setHeader("Expires","-1");
67         response.setHeader("Pragma","no-cache");
68         response.setHeader("Cache-control","no-cache");
69         response.setHeader("Content-Type", "text/html; charset=utf-8");
70
71         PrintWriter JavaDoc out = response.getWriter();
72         
73         XMPPConnection conn = (XMPPConnection)request.getSession().getAttribute("conn");
74
75         if (conn != null) {
76             Roster roster = conn.getRoster();
77             roster.setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);
78             ArrayList JavaDoc contacts = new ArrayList JavaDoc();
79             RosterEntry en = null;
80             Contact cn = null;
81             for (Iterator JavaDoc i=roster.getEntries(); i.hasNext(); ) {
82                 en = (RosterEntry)i.next();
83                 cn = new Contact();
84                 cn.setName(en.getName());
85                 if (cn.getName() == null) {
86                     cn.setName(en.getUser());
87                     if (cn.getName().indexOf("@") > 0) {
88                         cn.setName(cn.getName().substring(0, cn.getName().indexOf("@")));
89                     }
90                 }
91                 
92                 RosterPacket.ItemStatus st = en.getStatus();
93                 if (st != null && st.equals(RosterPacket.ItemStatus.SUBSCRIPTION_PENDING)) {
94                     cn.setStatus("pending");
95                 }
96                 
97                 Presence pr = roster.getPresence(en.getUser());
98                 if (pr != null) {
99                     Presence.Mode md = pr.getMode();
100                     if (md != null) {
101                         if (md.equals(Presence.Mode.AVAILABLE)) {
102                             cn.setPresence("available");
103                         } else if (md.equals(Presence.Mode.AWAY)) {
104                             cn.setPresence("away");
105                         } else if (md.equals(Presence.Mode.CHAT)) {
106                             cn.setPresence("chat");
107                         } else if (md.equals(Presence.Mode.DO_NOT_DISTURB)) {
108                             cn.setPresence("disturb");
109                         } else if (md.equals(Presence.Mode.EXTENDED_AWAY)) {
110                             cn.setPresence("extended_away");
111                         } else if (md.equals(Presence.Mode.INVISIBLE)) {
112                             cn.setPresence("invisible");
113                         }
114                         if (pr.getStatus() != null && !pr.getStatus().equals("")) {
115                             cn.setMessage(pr.getStatus());
116                         }
117                     }
118                 }
119                 if (cn.getPresence() == null) {
120                     cn.setPresence("offline");
121                 }
122                 cn.setUser(en.getUser());
123                 contacts.add(cn);
124
125             }
126             Collections.sort(contacts, new RosterComparator());
127             request.getSession().setAttribute("contacts", contacts);
128
129             // all contacts are set, now generate the output
130
Contact tmp = null;
131             for (int i=0; i<contacts.size(); i++) {
132                 tmp = (Contact)contacts.get(i);
133 // if (!tmp.getPresence().equals("offline")) {
134
out.println("<div id=\"contact\" onclick=\"openChat(this);\" ondblclick=\"openChat(this);\" onmouseover=\"showInfoWin(this);\" onmouseout=\"unhoverContact(this);\" " +
135                             "user=\"" + tmp.getUser() + "\" username=\"" + tmp.getName() + "\" presence=\"" + tmp.getPresence() + "\" msg=\"" + tmp.getMessage() + "\">");
136                     out.println("<table width=\"134\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" height=\"20\">");
137                     out.println(" <tr>");
138                     String JavaDoc color = "gray";
139                     if (tmp.getPresence().equals("available")) {
140                         color = "green";
141                     } else if (tmp.getPresence().equals("away")) {
142                         color = "orange";
143                     } else if (tmp.getPresence().equals("chat")) {
144                         color = "green";
145                     } else if (tmp.getPresence().equals("disturb")) {
146                         color = "red";
147                     } else if (tmp.getPresence().equals("extended_away")) {
148                         color = "red";
149                     } else if (tmp.getPresence().equals("invisible")) {
150                         color = "orange";
151                     }
152                     out.println(" <td width=\"10\"><img SRC=\"img/indicators/" + color + ".gif\" id=\"indicator\"/></td>");
153                     out.println(" <td width=\"124\">");
154                     out.println(" <div id=\"contactname\">");
155                     out.println(" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"124\" height=\"20\">");
156                     out.println(" <tr><td nowrap=\"nowrap\"><a HREF=\"#\">" + tmp.getName() + "</a></td>");
157                     out.println(" </tr>");
158                     out.println(" </table>");
159                     out.println(" </div>");
160                     out.println(" </td>");
161                     out.println(" </tr>");
162                     out.println("</table>");
163                     out.println("</div>");
164                 }
165 // }
166
} else {
167             out.print("<br /><br /><br /><div align=\"center\"><img SRC=\"img/loading.gif\" width=\"32\" height=\"32\">");
168         }
169             
170         out.flush();
171         out.close();
172     }
173
174     /**
175      * Initialization of the servlet. <br>
176      *
177      * @throws ServletException if an error occure
178      */

179     public void init() throws ServletException JavaDoc {
180         // Put your code here
181
}
182     
183     public static String JavaDoc findNameByUser(HttpSession JavaDoc sess, String JavaDoc user) {
184         ArrayList JavaDoc contacts = (ArrayList JavaDoc)sess.getAttribute("contacts");
185         if (contacts != null) {
186             Contact tmp = null;
187             String JavaDoc adr = null;
188             for (int i=0; i<contacts.size(); i++) {
189                 tmp = (Contact)contacts.get(i);
190                 adr = tmp.getUser().substring(0);
191                 if (adr.indexOf("@") > 0) {
192                     adr = adr.substring(0, adr.indexOf("@"));
193                 }
194                 if (adr.equals(user)) {
195                     if (tmp.getName() != null) {
196                         return tmp.getName();
197                     }
198                 }
199             }
200         }
201         return user;
202     }
203
204 }
205
Popular Tags