01: package org.claros.chat.ajax;
02:
03: import java.io.IOException;
04: import java.io.PrintWriter;
05: import java.util.Iterator;
06:
07: import javax.servlet.ServletException;
08: import javax.servlet.http.HttpServlet;
09: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11:
12: import org.jivesoftware.smack.RosterEntry;
13: import org.jivesoftware.smack.XMPPConnection;
14: import org.jivesoftware.smack.XMPPException;
15: import org.jivesoftware.smack.packet.Presence;
16:
17: public class Roaster extends HttpServlet {
18:
19: /**
20: *
21: */
22: private static final long serialVersionUID = -7926350754674872925L;
23:
24: /**
25: * Constructor of the object.
26: */
27: public Roaster() {
28: super ();
29: }
30:
31: /**
32: * The doGet method of the servlet. <br>
33: *
34: * This method is called when a form has its tag value method equals to get.
35: *
36: * @param request the request send by the client to the server
37: * @param response the response send by the server to the client
38: * @throws ServletException if an error occurred
39: * @throws IOException if an error occurred
40: */
41: public void doGet(HttpServletRequest request,
42: HttpServletResponse response) throws ServletException,
43: IOException {
44: doPost(request, response);
45: }
46:
47: /**
48: * The doPost method of the servlet. <br>
49: *
50: * This method is called when a form has its tag value method equals to post.
51: *
52: * @param request the request send by the client to the server
53: * @param response the response send by the server to the client
54: * @throws ServletException if an error occurred
55: * @throws IOException if an error occurred
56: */
57: public void doPost(HttpServletRequest request,
58: HttpServletResponse response) throws ServletException,
59: IOException {
60: response.setHeader("Expires", "-1");
61: response.setHeader("Pragma", "no-cache");
62: response.setHeader("Cache-control", "no-cache");
63:
64: response.setContentType("text/html");
65: PrintWriter out = response.getWriter();
66:
67: XMPPConnection conn = (XMPPConnection) request.getSession()
68: .getAttribute("conn");
69:
70: String act = request.getParameter("act");
71: if (act.equals("save")) {
72: String buddy = request.getParameter("newBuddy");
73: Presence pr = new Presence(Presence.Type.SUBSCRIBE);
74: pr.setTo(buddy);
75: conn.sendPacket(pr);
76: } else if (act.equals("remove")) {
77: String buddy = request.getParameter("removeBuddy");
78: RosterEntry en = null;
79: for (Iterator i = conn.getRoster().getEntries(); i
80: .hasNext();) {
81: en = (RosterEntry) i.next();
82: if (en.getUser().equals(buddy)) {
83: try {
84: conn.getRoster().removeEntry(en);
85: } catch (XMPPException e) {
86: e.printStackTrace();
87: }
88: break;
89: }
90: }
91: }
92: out.println("ok");
93: out.flush();
94: out.close();
95: }
96: }
|