001: package org.claros.chat.ajax;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005:
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServlet;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010: import javax.servlet.http.HttpSession;
011:
012: import org.claros.chat.controllers.TrafficController;
013: import org.claros.chat.threads.ChatListener;
014: import org.claros.chat.threads.ChatSender;
015: import org.jivesoftware.smack.XMPPConnection;
016:
017: public class Logout extends HttpServlet {
018:
019: /**
020: *
021: */
022: private static final long serialVersionUID = 418575745334176421L;
023:
024: /**
025: * Constructor of the object.
026: */
027: public Logout() {
028: super ();
029: }
030:
031: /**
032: * Destruction of the servlet. <br>
033: */
034: public void destroy() {
035: super .destroy();
036: }
037:
038: /**
039: * The doGet method of the servlet. <br>
040: *
041: * This method is called when a form has its tag value method equals to get.
042: *
043: * @param request the request send by the client to the server
044: * @param response the response send by the server to the client
045: * @throws ServletException if an error occurred
046: * @throws IOException if an error occurred
047: */
048: public void doGet(HttpServletRequest request,
049: HttpServletResponse response) throws ServletException,
050: IOException {
051: response.setHeader("Expires", "-1");
052: response.setHeader("Pragma", "no-cache");
053: response.setHeader("Cache-control", "no-cache");
054:
055: response.setContentType("text/html");
056: PrintWriter out = response.getWriter();
057:
058: logout(request.getSession(false));
059: out.print("ok");
060: out.flush();
061: out.close();
062: }
063:
064: /**
065: * The doPost method of the servlet. <br>
066: *
067: * This method is called when a form has its tag value method equals to post.
068: *
069: * @param request the request send by the client to the server
070: * @param response the response send by the server to the client
071: * @throws ServletException if an error occurred
072: * @throws IOException if an error occurred
073: */
074: public void doPost(HttpServletRequest request,
075: HttpServletResponse response) throws ServletException,
076: IOException {
077: doGet(request, response);
078: }
079:
080: /**
081: *
082: * @param request
083: */
084: public static void logout(HttpSession sess) {
085: if (sess != null) {
086: String user = null;
087: try {
088: XMPPConnection conn = (XMPPConnection) sess
089: .getAttribute("conn");
090: if (conn != null) {
091: user = conn.getUser();
092: conn.close();
093: sess.setAttribute("conn", null);
094: }
095: } catch (Throwable e) {
096: }
097:
098: try {
099: ChatListener listener = TrafficController
100: .getListener(user);
101: if (listener != null) {
102: listener.terminate();
103: if (user != null) {
104: TrafficController.removeListener(user);
105: }
106: }
107: } catch (Throwable e) {
108: }
109:
110: try {
111: ChatSender sender = TrafficController.getSender(user);
112: if (sender != null) {
113: sender.terminate();
114: if (user != null) {
115: TrafficController.removeSender(user);
116: }
117: }
118: } catch (Throwable e) {
119: }
120:
121: try {
122: sess.invalidate();
123: } catch (Throwable e) {
124: }
125: }
126: }
127:
128: }
|