001: package org.claros.chat.ajax;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.List;
006:
007: import javax.servlet.ServletException;
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import org.claros.chat.controllers.TrafficController;
013: import org.claros.chat.threads.ChatListener;
014: import org.jivesoftware.smack.XMPPConnection;
015: import org.jivesoftware.smack.packet.Presence;
016:
017: public class Subscriptions extends HttpServlet {
018:
019: /**
020: *
021: */
022: private static final long serialVersionUID = 7617721914260091646L;
023:
024: /**
025: * Constructor of the object.
026: */
027: public Subscriptions() {
028: super ();
029: }
030:
031: /**
032: * The doGet method of the servlet. <br>
033: *
034: * This method is called when a form has its tag value method equals to get.
035: *
036: * @param request the request send by the client to the server
037: * @param response the response send by the server to the client
038: * @throws ServletException if an error occurred
039: * @throws IOException if an error occurred
040: */
041: public void doGet(HttpServletRequest request,
042: HttpServletResponse response) throws ServletException,
043: IOException {
044:
045: // get the printwriter and prepare the html
046: response.setContentType("text/html");
047: response.setHeader("Expires", "-1");
048: response.setHeader("Pragma", "no-cache");
049: response.setHeader("Cache-control", "no-cache");
050: response.setHeader("Content-Type", "text/html; charset=utf-8");
051:
052: PrintWriter out = response.getWriter();
053:
054: XMPPConnection conn = (XMPPConnection) request.getSession()
055: .getAttribute("conn");
056: if (conn != null) {
057: String xmppUser = conn.getUser();
058: if (xmppUser != null) {
059: ChatListener listener = TrafficController
060: .getListener(xmppUser);
061:
062: if (listener != null) {
063: List msgs = listener.getNewSubscriptions();
064:
065: if (msgs != null && msgs.size() > 0) {
066: Presence prs = null;
067: String from = null;
068:
069: for (int i = 0; i < msgs.size(); i++) {
070: prs = (Presence) msgs.get(i);
071: from = prs.getFrom();
072: out.println("alert('" + from + "');");
073:
074: // TODO: subscribe onaylama olayõ
075: prs.setType(Presence.Type.SUBSCRIBED);
076: prs.setFrom(listener.getUser());
077: prs.setTo(from);
078: conn.sendPacket(prs);
079: }
080: }
081: }
082: }
083: }
084: out.flush();
085: out.close();
086: }
087:
088: /**
089: * The doPost method of the servlet. <br>
090: *
091: * This method is called when a form has its tag value method equals to post.
092: *
093: * @param request the request send by the client to the server
094: * @param response the response send by the server to the client
095: * @throws ServletException if an error occurred
096: * @throws IOException if an error occurred
097: */
098: public void doPost(HttpServletRequest request,
099: HttpServletResponse response) throws ServletException,
100: IOException {
101:
102: doGet(request, response);
103: }
104: }
|