Source Code Cross Referenced for ImapIdleCheckService.java in  » Chat » claros-intouch » org » claros » intouch » webmail » services » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Chat » claros intouch » org.claros.intouch.webmail.services 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.