001: package org.claros.intouch.profiling.services;
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.claros.commons.auth.models.AuthProfile;
016: import org.claros.commons.mail.models.ConnectionMetaHandler;
017: import org.claros.commons.mail.models.ConnectionProfile;
018: import org.claros.commons.mail.protocols.Protocol;
019: import org.claros.commons.mail.protocols.ProtocolFactory;
020: import org.jivesoftware.smack.XMPPConnection;
021:
022: public class LogoutService extends HttpServlet {
023:
024: /**
025: *
026: */
027: private static final long serialVersionUID = 418575745334176421L;
028:
029: /**
030: * Constructor of the object.
031: */
032: public LogoutService() {
033: super ();
034: }
035:
036: /**
037: * Destruction of the servlet. <br>
038: */
039: public void destroy() {
040: super .destroy();
041: }
042:
043: /**
044: * The doGet method of the servlet. <br>
045: *
046: * This method is called when a form has its tag value method equals to get.
047: *
048: * @param request the request send by the client to the server
049: * @param response the response send by the server to the client
050: * @throws ServletException if an error occurred
051: * @throws IOException if an error occurred
052: */
053: public void doGet(HttpServletRequest request,
054: HttpServletResponse response) throws ServletException,
055: IOException {
056: response.setHeader("Expires", "-1");
057: response.setHeader("Pragma", "no-cache");
058: response.setHeader("Cache-control", "no-cache");
059:
060: response.setContentType("text/html");
061: PrintWriter out = response.getWriter();
062:
063: logoutMail(request.getSession(false));
064: logoutChat(request.getSession(false));
065:
066: out.print("ok");
067: }
068:
069: /**
070: *
071: * @param request
072: */
073: public static void logoutMail(HttpSession sess) {
074: try {
075: ConnectionProfile profile = getConnectionProfile(sess);
076: if (profile != null) {
077: ConnectionMetaHandler handler = getConnectionHandler(sess);
078: AuthProfile auth = getAuthProfile(sess);
079: ProtocolFactory factory = new ProtocolFactory(profile,
080: auth, handler);
081: if (factory != null) {
082: Protocol protocol = factory.getProtocol(null);
083: if (protocol != null) {
084: protocol.disconnect();
085: }
086: }
087: }
088: } catch (Throwable e) {
089: }
090: }
091:
092: /**
093: * The doPost method of the servlet. <br>
094: *
095: * This method is called when a form has its tag value method equals to post.
096: *
097: * @param request the request send by the client to the server
098: * @param response the response send by the server to the client
099: * @throws ServletException if an error occurred
100: * @throws IOException if an error occurred
101: */
102: public void doPost(HttpServletRequest request,
103: HttpServletResponse response) throws ServletException,
104: IOException {
105: doGet(request, response);
106: }
107:
108: /**
109: *
110: * @param request
111: */
112: public static void logoutChat(HttpSession sess) {
113: if (sess != null) {
114: String user = null;
115: try {
116: XMPPConnection conn = (XMPPConnection) sess
117: .getAttribute("conn");
118: if (conn != null) {
119: user = conn.getUser();
120: conn.close();
121: sess.setAttribute("conn", null);
122: }
123: } catch (Throwable e) {
124: }
125:
126: try {
127: ChatListener listener = TrafficController
128: .getListener(user);
129: if (listener != null) {
130: listener.terminate();
131: if (user != null) {
132: TrafficController.removeListener(user);
133: }
134: }
135: } catch (Throwable e) {
136: }
137:
138: try {
139: ChatSender sender = TrafficController.getSender(user);
140: if (sender != null) {
141: sender.terminate();
142: if (user != null) {
143: TrafficController.removeSender(user);
144: }
145: }
146: } catch (Throwable e) {
147: }
148:
149: try {
150: // sess.invalidate();
151: } catch (Throwable e) {
152: }
153: }
154: }
155:
156: /**
157: *
158: * @param request
159: * @return
160: */
161: private static ConnectionProfile getConnectionProfile(
162: HttpSession sess) {
163: return (ConnectionProfile) sess.getAttribute("profile");
164: }
165:
166: /**
167: *
168: * @param request
169: * @return
170: */
171: private static ConnectionMetaHandler getConnectionHandler(
172: HttpSession sess) {
173: return (ConnectionMetaHandler) sess.getAttribute("handler");
174: }
175:
176: /**
177: *
178: * @param request
179: * @return
180: */
181: private static AuthProfile getAuthProfile(HttpSession sess) {
182: return (AuthProfile) sess.getAttribute("auth");
183: }
184: }
|