Source Code Cross Referenced for EmailHelper.java in  » Testing » webtest » com » canoo » webtest » plugins » emailtest » 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 » Testing » webtest » com.canoo.webtest.plugins.emailtest 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Released under the Canoo Webtest license.
002:        package com.canoo.webtest.plugins.emailtest;
003:
004:        import java.util.Properties;
005:
006:        import javax.mail.Flags;
007:        import javax.mail.Folder;
008:        import javax.mail.Message;
009:        import javax.mail.MessagingException;
010:        import javax.mail.Session;
011:        import javax.mail.Store;
012:        import javax.mail.NoSuchProviderException;
013:
014:        import org.apache.log4j.Logger;
015:
016:        import com.canoo.webtest.util.ConversionUtil;
017:
018:        /**
019:         * Helper class for processing email messages.
020:         *
021:         * @author Paul King
022:         * @author Luca Scheuring
023:         * @author lofi@mountproc.org
024:         */
025:        public class EmailHelper {
026:            private static final Logger LOG = Logger
027:                    .getLogger(EmailHelper.class);
028:
029:            /**
030:             * log into Email server and fetch mails (only headers)
031:             *
032:             * @return message array
033:             */
034:            Message[] getMessages(final Folder folder)
035:                    throws MessagingException {
036:                // Get the message wrappers
037:                final Message[] msgs = folder.getMessages();
038:                LOG.debug("Login to Email server successful, " + msgs.length
039:                        + " message(s) on server");
040:                return msgs;
041:            }
042:
043:            Message getMessage(final int id, final Folder folder)
044:                    throws MessagingException {
045:                // Get the message wrappers
046:                final Message[] msgs = getMessages(folder);
047:                for (int i = 0; i < msgs.length; i++) {
048:                    if (msgs[i].getMessageNumber() == id) {
049:                        return msgs[i];
050:                    }
051:                }
052:                return null;
053:            }
054:
055:            /**
056:             * Mark a message for deletion (message gets deleted when closing inbox)
057:             *
058:             * @param m message to be deleted
059:             */
060:            void markForDelete(final Message m) throws MessagingException {
061:                m.setFlag(Flags.Flag.DELETED, true);
062:                LOG.debug("Message " + m.getMessageNumber()
063:                        + " marked for delete");
064:            }
065:
066:            Folder getInboxFolder(final EmailConfigInfo info)
067:                    throws MessagingException {
068:                int port = -1;
069:                String server = info.getServer();
070:                final int colonPosn = server.indexOf(":");
071:                if (colonPosn != -1) {
072:                    port = extractPort(server, colonPosn);
073:                    server = server.substring(0, colonPosn);
074:                }
075:                processDelayIfNeeded(info.getDelay());
076:
077:                // Get the default session
078:                final Session session = getSessionInstance(System
079:                        .getProperties());
080:
081:                // Get an email message store, and connect to it
082:                final Store store = getStore(session, info.getType());
083:                store.connect(server, port, info.getUsername(), info
084:                        .getPassword());
085:
086:                // Try to get the default folder
087:                Folder folder = store.getDefaultFolder();
088:                if (folder == null) {
089:                    throw new MessagingException("No default folder");
090:                }
091:
092:                // ...and its INBOX
093:                folder = folder.getFolder("INBOX");
094:                if (folder == null) {
095:                    throw new MessagingException("No INBOX");
096:                }
097:
098:                // Open the folder with r/w-access
099:                folder.open(Folder.READ_WRITE);
100:                return folder;
101:            }
102:
103:            private static int extractPort(final String server,
104:                    final int colonPosn) throws MessagingException {
105:                final String portStr = server.substring(colonPosn + 1);
106:                try {
107:                    return Integer.parseInt(portStr);
108:                } catch (NumberFormatException e) {
109:                    throw new MessagingException(
110:                            "Port must be numeric, illegal value: " + portStr);
111:                }
112:            }
113:
114:            private static void processDelayIfNeeded(final String delayStr)
115:                    throws MessagingException {
116:                if (delayStr != null) {
117:                    try {
118:                        final int delay = ConversionUtil.convertToInt(delayStr,
119:                                0);
120:                        Thread.sleep(delay * 1000);
121:                    } catch (NumberFormatException nfe) {
122:                        throw new MessagingException(
123:                                "Delay must be numeric, illegal value: "
124:                                        + delayStr);
125:                    } catch (InterruptedException ie) {/* ignore */
126:                    }
127:                }
128:            }
129:
130:            protected Store getStore(final Session session, final String type)
131:                    throws NoSuchProviderException {
132:                return session.getStore(type);
133:            }
134:
135:            protected Session getSessionInstance(final Properties properties) {
136:                return Session.getInstance(properties);
137:            }
138:
139:            /**
140:             * log out of email server. expunge if deleteOnServer is true
141:             */
142:            void logout(final Folder folder, final boolean deleteOnServer) {
143:                try {
144:                    if (folder != null) {
145:                        LOG.debug("closing INBOX...");
146:                        folder.close(deleteOnServer);
147:                    }
148:                } catch (MessagingException e) {
149:                    LOG.warn("Failed to close INBOX folder.", e);
150:                }
151:            }
152:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.