View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  package org.apache.james.postage.client;
22  
23  import java.io.IOException;
24  import java.util.Iterator;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.commons.net.pop3.POP3MessageInfo;
29  import org.apache.james.postage.PostageException;
30  import org.apache.james.postage.SamplingException;
31  import org.apache.james.postage.StartupException;
32  import org.apache.james.postage.execution.Sampler;
33  import org.apache.james.postage.result.PostageRunnerResult;
34  import org.apache.james.postage.user.UserList;
35  
36  /***
37   * acts like a MUA using POP3 protocol.<br/>
38   * fetches all mails for one (all) users and initiates adding to results
39   */
40  public class POP3Client implements Sampler {
41  
42      private static Log log = LogFactory.getLog(POP3Client.class);
43  
44      private String m_host;
45      private int m_port;
46      private UserList m_internalUsers;
47      private PostageRunnerResult m_results;
48  
49      public POP3Client(String host, int port, UserList internalUsers, PostageRunnerResult results) {
50          m_host = host;
51          m_port = port;
52          m_internalUsers = internalUsers;
53          m_results = results;
54      }
55  
56      /***
57       * checks, if the configured POP3 services is accessable
58       */
59      public boolean checkAvailability() throws StartupException {
60          try {
61              org.apache.commons.net.pop3.POP3Client pop3Client = openConnection(m_internalUsers.getRandomUsername());
62              closeSession(pop3Client);
63          } catch (PostageException e) {
64              throw new StartupException("error checking availability");
65          }
66          return true;
67      }
68  
69      private void closeSession(org.apache.commons.net.pop3.POP3Client pop3Client) throws PostageException {
70          try {
71              pop3Client.sendCommand("QUIT");
72              pop3Client.disconnect();
73          } catch (IOException e) {
74              throw new PostageException("error closing pop3 session", e);
75          }
76      }
77  
78      private org.apache.commons.net.pop3.POP3Client openConnection(String username) throws PostageException {
79          org.apache.commons.net.pop3.POP3Client pop3Client = new org.apache.commons.net.pop3.POP3Client();
80          try {
81              pop3Client.connect(m_host, m_port);
82              pop3Client.login(username, m_internalUsers.getPassword());
83          } catch (IOException e) {
84              throw new PostageException("POP3 service not available", e);
85          }
86          return pop3Client;
87      }
88  
89      /***
90       * take one POP3 sample for a random user
91       */
92      public void doSample() throws SamplingException {
93          String username = m_internalUsers.getRandomUsername();
94  
95          try {
96              findAllMatchingTestMail(username);
97          } catch (SamplingException e) {
98              log.warn("error sampling mail for user " + username);
99              throw e;
100         }
101     }
102 
103     /***
104      * used after completing with regular test scenario. tries to collect all mails, which are left 
105      * unprocessed by the random access. this is done by iterating over all user accounts, looking for mail
106      */
107     public void doMatchMailForAllUsers() {
108         Iterator usernames = m_internalUsers.getUsernames();
109         while (usernames.hasNext()) {
110             String username = (String)usernames.next();
111             try {
112                 findAllMatchingTestMail(username);
113             } catch (SamplingException e) {
114                 log.warn("error reading mail for user " + username);
115             }
116         }
117     }
118 
119     /***
120      * for the specified user, fetches all mail and invokes the matching process
121      * @param username
122      * @throws SamplingException
123      */
124     private void findAllMatchingTestMail(String username) throws SamplingException {
125         try {
126             org.apache.commons.net.pop3.POP3Client pop3Client = openConnection(username);
127 
128             // retrieve all messages
129             POP3MessageInfo[] entries = null;
130             try {
131                 entries = pop3Client.listMessages();
132             } catch (Exception e) {
133                 String errorMessage = "failed to read pop3 account mail list for " + username;
134                 m_results.addError(500, errorMessage);
135                 log.info(errorMessage);
136                 return;
137             }
138 
139             for (int i = 0; entries != null && i < entries.length; i++) {
140                 POP3MessageInfo entry = entries[i];
141 
142                 try {
143                     new POP3MailAnalyzeStrategy("pop3", m_results, pop3Client, entry.number, i).handle();
144                 } catch (Exception exception) {
145                     log.warn("error processing pop3 mail", exception);
146                 }
147             }
148 
149             closeSession(pop3Client);
150         } catch (PostageException e) {
151             throw new SamplingException("sample failed", e);
152         }
153     }
154 }
155