Source Code Cross Referenced for MailReaderSampler.java in  » Testing » jakarta-jmeter » org » apache » jmeter » protocol » mail » sampler » 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 » jakarta jmeter » org.apache.jmeter.protocol.mail.sampler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *   http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         * 
017:         */
018:        package org.apache.jmeter.protocol.mail.sampler;
019:
020:        import java.io.UnsupportedEncodingException;
021:        import java.util.Properties;
022:
023:        import javax.mail.Address;
024:        import javax.mail.BodyPart;
025:        import javax.mail.Flags;
026:        import javax.mail.Folder;
027:        import javax.mail.Message;
028:        import javax.mail.Session;
029:        import javax.mail.Store;
030:        import javax.mail.internet.MimeMultipart;
031:
032:        import org.apache.jmeter.samplers.AbstractSampler;
033:        import org.apache.jmeter.samplers.Entry;
034:        import org.apache.jmeter.samplers.SampleResult;
035:        import org.apache.jmeter.testelement.property.BooleanProperty;
036:        import org.apache.jmeter.testelement.property.IntegerProperty;
037:        import org.apache.jmeter.testelement.property.StringProperty;
038:        import org.apache.jorphan.logging.LoggingManager;
039:        import org.apache.log.Logger;
040:
041:        /**
042:         * @author Thad Smith
043:         */
044:        public class MailReaderSampler extends AbstractSampler {
045:            private static final Logger log = LoggingManager
046:                    .getLoggerForClass();
047:
048:            private final static String SERVER_TYPE = "host_type"; // $NON-NLS-1$
049:            private final static String SERVER = "host"; // $NON-NLS-1$
050:            private final static String USERNAME = "username"; // $NON-NLS-1$
051:            private final static String PASSWORD = "password"; // $NON-NLS-1$
052:            private final static String FOLDER = "folder"; // $NON-NLS-1$
053:            private final static String DELETE = "delete"; // $NON-NLS-1$
054:            private final static String NUM_MESSAGES = "num_messages"; // $NON-NLS-1$
055:            private static final String NEW_LINE = "\n"; // $NON-NLS-1$
056:
057:            // Needed by GUI
058:            public final static String TYPE_POP3 = "pop3"; // $NON-NLS-1$
059:            public final static String TYPE_IMAP = "imap"; // $NON-NLS-1$
060:            public static final int ALL_MESSAGES = -1;
061:
062:            public MailReaderSampler() {
063:                setServerType(TYPE_POP3);
064:                setFolder("INBOX");
065:                setNumMessages(ALL_MESSAGES);
066:                setDeleteMessages(false);
067:            }
068:
069:            /*
070:             * (non-Javadoc) Performs the sample, and returns the result
071:             * 
072:             * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
073:             */
074:            public SampleResult sample(Entry e) {
075:                SampleResult res = new SampleResult();
076:                boolean isOK = false; // Did sample succeed?
077:                boolean deleteMessages = getDeleteMessages();
078:
079:                res.setSampleLabel(getName());
080:                res.setSamplerData(getServerType() + "://" + getUserName()
081:                        + "@" + getServer());
082:                /*
083:                 * Perform the sampling
084:                 */
085:                res.sampleStart(); // Start timing
086:                try {
087:                    // Create empty properties
088:                    Properties props = new Properties();
089:
090:                    // Get session
091:                    Session session = Session.getDefaultInstance(props, null);
092:
093:                    // Get the store
094:                    Store store = session.getStore(getServerType());
095:                    store.connect(getServer(), getUserName(), getPassword());
096:
097:                    // Get folder
098:                    Folder folder = store.getFolder(getFolder());
099:                    if (deleteMessages) {
100:                        folder.open(Folder.READ_WRITE);
101:                    } else {
102:                        folder.open(Folder.READ_ONLY);
103:                    }
104:
105:                    // Get directory
106:                    Message messages[] = folder.getMessages();
107:                    Message message;
108:                    StringBuffer data = new StringBuffer();
109:                    data.append(messages.length);
110:                    data.append(" messages found\n");
111:
112:                    int n = getNumMessages();
113:                    if (n == ALL_MESSAGES || n > messages.length)
114:                        n = messages.length;
115:
116:                    // TODO - create a sample result for each message?
117:                    for (int i = 0; i < n; i++) {
118:                        message = messages[i];
119:
120:                        if (i == 0) { // Assumes all the messaged have the same type ...
121:                            res.setContentType(message.getContentType());
122:                        }
123:
124:                        data.append("Message "); // $NON-NLS-1$
125:                        data.append(message.getMessageNumber());
126:                        data.append(":\n"); // $NON-NLS-1$
127:
128:                        data.append("Date: "); // $NON-NLS-1$
129:                        data.append(message.getSentDate());
130:                        data.append(NEW_LINE);
131:
132:                        data.append("To: "); // $NON-NLS-1$
133:                        Address[] recips = message.getAllRecipients();
134:                        for (int j = 0; j < recips.length; j++) {
135:                            data.append(recips[j].toString());
136:                            if (j < recips.length - 1)
137:                                data.append("; "); // $NON-NLS-1$
138:                        }
139:                        data.append(NEW_LINE);
140:
141:                        data.append("From: "); // $NON-NLS-1$
142:                        Address[] from = message.getFrom();
143:                        for (int j = 0; j < from.length; j++) {
144:                            data.append(from[j].toString());
145:                            if (j < from.length - 1)
146:                                data.append("; "); // $NON-NLS-1$
147:                        }
148:                        data.append(NEW_LINE);
149:
150:                        data.append("Subject: "); // $NON-NLS-1$
151:                        data.append(message.getSubject());
152:                        data.append(NEW_LINE);
153:
154:                        data.append(NEW_LINE);
155:                        Object content = message.getContent();
156:                        if (content instanceof  MimeMultipart) {
157:                            MimeMultipart mmp = (MimeMultipart) content;
158:                            int count = mmp.getCount();
159:                            data.append("Multipart. Count: ");
160:                            data.append(count);
161:                            data.append(NEW_LINE);
162:                            for (int j = 0; j < count; j++) {
163:                                BodyPart bodyPart = mmp.getBodyPart(j);
164:                                data.append("Type: ");
165:                                data.append(bodyPart.getContentType());
166:                                data.append(NEW_LINE);
167:                                try {
168:                                    data.append(bodyPart.getContent());
169:                                } catch (UnsupportedEncodingException ex) {
170:                                    data.append(ex.getLocalizedMessage());
171:                                }
172:                                data.append(NEW_LINE);
173:                            }
174:                        } else {
175:                            data.append(content);
176:                            data.append(NEW_LINE);
177:                        }
178:                        data.append(NEW_LINE);
179:
180:                        if (deleteMessages) {
181:                            message.setFlag(Flags.Flag.DELETED, true);
182:                        }
183:                    }
184:
185:                    // Close connection
186:                    folder.close(true);
187:                    store.close();
188:
189:                    /*
190:                     * Set up the sample result details
191:                     */
192:                    res.setResponseData(data.toString().getBytes());
193:                    res.setDataType(SampleResult.TEXT);
194:
195:                    res.setResponseCodeOK();
196:                    res.setResponseMessage("OK"); // $NON-NLS-1$
197:                    isOK = true;
198:                } catch (NoClassDefFoundError ex) {
199:                    log.debug("", ex);// No need to log normally, as we set the status
200:                    res.setResponseCode("500"); // $NON-NLS-1$
201:                    res.setResponseMessage(ex.toString());
202:                } catch (Exception ex) {
203:                    log.debug("", ex);// No need to log normally, as we set the status
204:                    res.setResponseCode("500"); // $NON-NLS-1$
205:                    res.setResponseMessage(ex.toString());
206:                }
207:
208:                res.sampleEnd();
209:                res.setSuccessful(isOK);
210:                return res;
211:            }
212:
213:            /**
214:             * Sets the type of protocol to use when talking with the remote mail
215:             * server. Either MailReaderSampler.TYPE_IMAP or
216:             * MailReaderSampler.TYPE_POP3. Default is MailReaderSampler.TYPE_POP3.
217:             * 
218:             * @param serverType
219:             */
220:            public void setServerType(String serverType) {
221:                setProperty(SERVER_TYPE, serverType);
222:            }
223:
224:            /**
225:             * Returns the type of the protocol set to use when talking with the remote
226:             * server. Either MailReaderSampler.TYPE_IMAP or
227:             * MailReaderSampler.TYPE_POP3.
228:             * 
229:             * @return Server Type
230:             */
231:            public String getServerType() {
232:                return getProperty(SERVER_TYPE).toString();
233:            }
234:
235:            /**
236:             * @param server -
237:             *            The name or address of the remote server.
238:             */
239:            public void setServer(String server) {
240:                setProperty(SERVER, server);
241:            }
242:
243:            /**
244:             * @return The name or address of the remote server.
245:             */
246:            public String getServer() {
247:                return getProperty(SERVER).toString();
248:            }
249:
250:            /**
251:             * @param username -
252:             *            The username of the mail account.
253:             */
254:            public void setUserName(String username) {
255:                setProperty(USERNAME, username);
256:            }
257:
258:            /**
259:             * @return The username of the mail account.
260:             */
261:            public String getUserName() {
262:                return getProperty(USERNAME).toString();
263:            }
264:
265:            /**
266:             * @param password
267:             */
268:            public void setPassword(String password) {
269:                setProperty(PASSWORD, password);
270:            }
271:
272:            /**
273:             * @return password
274:             */
275:            public String getPassword() {
276:                return getProperty(PASSWORD).toString();
277:            }
278:
279:            /**
280:             * @param folder -
281:             *            Name of the folder to read emails from. "INBOX" is the only
282:             *            acceptable value if the server type is POP3.
283:             */
284:            public void setFolder(String folder) {
285:                setProperty(FOLDER, folder);
286:            }
287:
288:            /**
289:             * @return folder
290:             */
291:            public String getFolder() {
292:                return getProperty(FOLDER).toString();
293:            }
294:
295:            /**
296:             * @param num_messages -
297:             *            The number of messages to retrieve from the mail server. Set
298:             *            this value to -1 to retrieve all messages.
299:             */
300:            public void setNumMessages(int num_messages) {
301:                setProperty(new IntegerProperty(NUM_MESSAGES, num_messages));
302:            }
303:
304:            /**
305:             * @param num_messages -
306:             *            The number of messages to retrieve from the mail server. Set
307:             *            this value to -1 to retrieve all messages.
308:             */
309:            public void setNumMessages(String num_messages) {
310:                setProperty(new StringProperty(NUM_MESSAGES, num_messages));
311:            }
312:
313:            /**
314:             * @return The number of messages to retrieve from the mail server.
315:             *         -1 denotes get all messages.
316:             */
317:            public int getNumMessages() {
318:                return getPropertyAsInt(NUM_MESSAGES);
319:            }
320:
321:            /**
322:             * @return The number of messages to retrieve from the mail server.
323:             *         -1 denotes get all messages.
324:             */
325:            public String getNumMessagesString() {
326:                return getPropertyAsString(NUM_MESSAGES);
327:            }
328:
329:            /**
330:             * @param delete -
331:             *            Whether or not to delete the read messages from the folder.
332:             */
333:            public void setDeleteMessages(boolean delete) {
334:                setProperty(new BooleanProperty(DELETE, delete));
335:            }
336:
337:            /**
338:             * @return Whether or not to delete the read messages from the folder.
339:             */
340:            public boolean getDeleteMessages() {
341:                return getPropertyAsBoolean(DELETE);
342:            }
343:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.