KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.claros.chat.ajax;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServlet JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 import org.claros.chat.controllers.TrafficController;
13 import org.claros.chat.threads.ChatListener;
14 import org.jivesoftware.smack.XMPPConnection;
15 import org.jivesoftware.smack.packet.Presence;
16
17 public class Subscriptions extends HttpServlet JavaDoc {
18
19     /**
20      *
21      */

22     private static final long serialVersionUID = 7617721914260091646L;
23
24     /**
25      * Constructor of the object.
26      */

27     public Subscriptions() {
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 JavaDoc request, HttpServletResponse JavaDoc response)
42             throws ServletException JavaDoc, IOException JavaDoc {
43
44         // get the printwriter and prepare the html
45
response.setContentType("text/html");
46         response.setHeader("Expires","-1");
47         response.setHeader("Pragma","no-cache");
48         response.setHeader("Cache-control","no-cache");
49         response.setHeader("Content-Type", "text/html; charset=utf-8");
50
51         PrintWriter JavaDoc out = response.getWriter();
52
53         XMPPConnection conn = (XMPPConnection)request.getSession().getAttribute("conn");
54         if (conn != null) {
55             String JavaDoc xmppUser = conn.getUser();
56             if (xmppUser != null) {
57                 ChatListener listener = TrafficController.getListener(xmppUser);
58
59                 if (listener != null) {
60                     List JavaDoc msgs = listener.getNewSubscriptions();
61
62                     if (msgs != null && msgs.size() > 0) {
63                         Presence prs = null;
64                         String JavaDoc from = null;
65
66                         for (int i=0;i<msgs.size(); i++) {
67                             prs = (Presence)msgs.get(i);
68                             from = prs.getFrom();
69                             out.println("alert('" + from + "');");
70                             
71                             // TODO: subscribe onaylama olay§
72
prs.setType(Presence.Type.SUBSCRIBED);
73                             prs.setFrom(listener.getUser());
74                             prs.setTo(from);
75                             conn.sendPacket(prs);
76                         }
77                     }
78                 }
79             }
80         }
81         out.flush();
82         out.close();
83     }
84
85     /**
86      * The doPost method of the servlet. <br>
87      *
88      * This method is called when a form has its tag value method equals to post.
89      *
90      * @param request the request send by the client to the server
91      * @param response the response send by the server to the client
92      * @throws ServletException if an error occurred
93      * @throws IOException if an error occurred
94      */

95     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
96             throws ServletException JavaDoc, IOException JavaDoc {
97
98         doGet(request, response);
99     }
100 }
101
Popular Tags