001: package org.claros.intouch.webmail.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.ArrayList;
006: import java.util.HashMap;
007: import java.util.List;
008:
009: import javax.mail.Folder;
010: import javax.mail.FolderClosedException;
011: import javax.mail.Message;
012: import javax.mail.MessagingException;
013: import javax.servlet.ServletException;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019: import org.claros.commons.auth.models.AuthProfile;
020: import org.claros.commons.mail.models.ConnectionProfile;
021: import org.claros.commons.mail.utility.Constants;
022: import org.claros.intouch.common.services.BaseService;
023: import org.claros.intouch.webmail.adapters.IdleMessageCountAdapter;
024: import org.claros.intouch.webmail.adapters.IdleOutputThread;
025: import org.claros.intouch.webmail.controllers.ImapFolderControllerImpl;
026: import org.claros.intouch.webmail.factory.FolderControllerFactory;
027:
028: import com.sun.mail.imap.IMAPFolder;
029:
030: public class ImapIdleCheckService extends BaseService {
031: private static final long serialVersionUID = 6209371120941300627L;
032: private static HashMap listeners = new HashMap();
033: private static Log log = LogFactory
034: .getLog(ImapIdleCheckService.class);
035:
036: /**
037: * The doGet method of the servlet. <br>
038: *
039: * This method is called when a form has its tag value method equals to get.
040: *
041: * @param request the request send by the client to the server
042: * @param response the response send by the server to the client
043: * @throws ServletException if an error occurred
044: * @throws IOException if an error occurred
045: */
046: public void doPost(HttpServletRequest request,
047: HttpServletResponse response) throws ServletException,
048: IOException {
049: response.setContentType("text/html");
050: PrintWriter out = response.getWriter();
051:
052: String f = request.getParameter("folder");
053:
054: ConnectionProfile profile = getConnectionProfile(request);
055: AuthProfile auth = getAuthProfile(request);
056:
057: if (profile.getProtocol().equals(Constants.IMAP)) {
058: FolderControllerFactory foldFact = new FolderControllerFactory(
059: auth, profile, getConnectionHandler(request));
060: ImapFolderControllerImpl imapFold = (ImapFolderControllerImpl) foldFact
061: .getFolderController();
062:
063: /*
064: if (listenerExists(auth.getUsername(), f)) {
065: // listener already exists so no need to do any action.
066: return;
067: }
068: */
069: Folder fold = null;
070: try {
071: log.debug("getting the folder");
072: fold = imapFold.getFolderObj(f, false);
073: log.debug("got folder:" + f);
074: } catch (Exception e) {
075: log.error(e);
076: }
077: if (fold != null) {
078: try {
079: log.debug("monitoring....");
080: addListener(auth.getUsername(), f);
081:
082: IdleMessageCountAdapter adp = new IdleMessageCountAdapter(
083: auth.getUsername());
084:
085: IdleOutputThread ot = new IdleOutputThread(response
086: .getWriter(), auth.getUsername());
087: ot.start();
088:
089: monitorNewMail(auth.getUsername(), f,
090: (IMAPFolder) fold, adp);
091: } catch (Exception e) {
092: log.error(e);
093: }
094: }
095: }
096: out.write("alert('FINITO');");
097: out.flush();
098: }
099:
100: /**
101: *
102: * @param username
103: * @param folder
104: */
105: public static void addListener(String username, String folder) {
106: List userListener = (List) listeners.get(username);
107: if (userListener == null) {
108: userListener = new ArrayList();
109: }
110: if (!userListener.contains(folder)) {
111: userListener.add(folder);
112: }
113: listeners.put(username, userListener);
114: }
115:
116: /**
117: *
118: * @param username
119: * @param folder
120: */
121: public static boolean listenerExists(String username, String folder) {
122: List userListener = (List) listeners.get(username);
123: if (userListener == null) {
124: userListener = new ArrayList();
125: }
126: return userListener.contains(folder);
127: }
128:
129: /**
130: *
131: * @param username
132: * @param folder
133: */
134: public static void removeListener(String username, String folder) {
135: List userListener = (List) listeners.get(username);
136: if (userListener == null) {
137: userListener = new ArrayList();
138: }
139: if (!userListener.contains(folder)) {
140: userListener.remove(folder);
141: }
142: listeners.put(username, userListener);
143: }
144:
145: /**
146: *
147: * @param fold
148: * @param out
149: * @throws Exception
150: */
151: public void monitorNewMail(String username, String folderName,
152: IMAPFolder fold, IdleMessageCountAdapter adapter)
153: throws Exception {
154: // Add messageCountListener to listen for new messages
155: fold.addMessageCountListener(adapter);
156:
157: // Check mail once in "freq" MILLIseconds
158: try {
159: int freq = 5000;
160: boolean supportsIdle = false;
161: try {
162: if (fold instanceof IMAPFolder) {
163: IMAPFolder f = (IMAPFolder) fold;
164: f.idle();
165: supportsIdle = true;
166: }
167: } catch (FolderClosedException fex) {
168: throw fex;
169: } catch (MessagingException mex) {
170: supportsIdle = false;
171: }
172: while (listenerExists(username, folderName)) {
173: if (supportsIdle && fold instanceof IMAPFolder) {
174: IMAPFolder f = (IMAPFolder) fold;
175: f.idle();
176: log.debug("IDLE done");
177: } else {
178: Thread.sleep(freq); // sleep for freq milliseconds
179: // This is to force the IMAP server to send us
180: // EXISTS notifications.
181: fold.getMessageCount();
182: }
183: }
184: } catch (FolderClosedException e) {
185: log.error(e);
186: } catch (MessagingException e) {
187: log.error(e);
188: } catch (InterruptedException e) {
189: log.error(e);
190: }
191: }
192: }
|