View Javadoc

1   /*
2    * $Id: AbstractGreenMailSupport.java 19873 2010-10-08 23:54:59Z dzapata $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email;
12  
13  import com.icegreen.greenmail.user.GreenMailUser;
14  import com.icegreen.greenmail.user.UserManager;
15  import com.icegreen.greenmail.util.GreenMail;
16  import com.icegreen.greenmail.util.ServerSetup;
17  
18  import java.util.List;
19  import java.util.Properties;
20  
21  import javax.mail.Message;
22  import javax.mail.Session;
23  import javax.mail.internet.InternetAddress;
24  import javax.mail.internet.MimeMessage;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  public abstract class AbstractGreenMailSupport 
30  {
31  
32      public static final String LOCALHOST = "127.0.0.1";
33      public static final String MESSAGE = "Test Email Message";
34      public static final String AT_EXAMPLE_COM = "@example.com";
35      public static final String BOB = "bob";
36      public static final String BOB_EMAIL = BOB + AT_EXAMPLE_COM;
37      public static final String ALICE = "alice";
38      public static final String ALICE_EMAIL = ALICE + AT_EXAMPLE_COM;
39      public static final String PASSWORD = "secret";
40      public static final long STARTUP_PERIOD_MS = 100;
41  
42      protected final Log logger = LogFactory.getLog(this.getClass());
43      private GreenMail servers;
44  
45      protected void createUserAndStoreEmail(String email, String user, String password, Object message) throws Exception
46      {
47          // note that with greenmail 1.1 the Servers object is unreliable
48          // and the approach taken in their examples will not work.
49          // the following does work, but may break in a later version
50          // (there is some confusion in the greenmail code about
51          // whether users are identified by email or name alone)
52          // in which case try retrieving by EMAIL rather than USER
53          logger.debug("Creating mail user " + user + "/" + email + "/" + password);
54          UserManager userManager = servers.getManagers().getUserManager();
55          userManager.createUser(email, user, password);
56          GreenMailUser target = userManager.getUser(user);
57          if (null == target)
58          {
59              throw new IllegalStateException("Failure in greenmail - see comments in test code.");
60          }
61          target.deliver((MimeMessage) message);
62          Thread.sleep(STARTUP_PERIOD_MS);
63      }
64  
65      protected void createBobAndStoreEmail(Object message) throws Exception
66      {
67          createUserAndStoreEmail(BOB_EMAIL, BOB, PASSWORD, message);
68      }
69  
70      protected void createAliceAndStoreEmail(Object message) throws Exception
71      {
72          createUserAndStoreEmail(ALICE_EMAIL, ALICE, PASSWORD, message);
73      }
74  
75      protected void startServers(List<Integer> list) throws Exception
76      {
77          logger.info("Starting mail servers");
78          servers = new GreenMail(getSetups(list));
79          servers.start();
80          Thread.sleep(STARTUP_PERIOD_MS);
81      }
82  
83      protected abstract int nextPort();
84  
85      private ServerSetup[] getSetups(List<Integer> list)
86      {
87          if (list.size() != 6)
88          {
89              throw new IllegalArgumentException("must pass in an array of 6 ports for server setup");
90          }
91          
92          return new ServerSetup[]{
93                  newServerSetup(list.get(0), ServerSetup.PROTOCOL_POP3),
94                  newServerSetup(list.get(1), ServerSetup.PROTOCOL_SMTP),
95                  newServerSetup(list.get(2), ServerSetup.PROTOCOL_SMTPS),
96                  newServerSetup(list.get(3), ServerSetup.PROTOCOL_POP3S),
97                  newServerSetup(list.get(4), ServerSetup.PROTOCOL_IMAP),
98                  newServerSetup(list.get(5), ServerSetup.PROTOCOL_IMAPS)
99          };
100     }
101 
102     private ServerSetup newServerSetup(int port, String protocol)
103     {
104         logger.debug("Server for " + protocol + " will be on port " + port);
105         return new ServerSetup(port, null, protocol);
106     }
107 
108     protected void stopServers() throws Exception
109     {
110         logger.info("Stopping mail servers");
111         if (null != servers)
112         {
113             servers.stop();
114         }
115     }
116 
117     protected GreenMail getServers()
118     {
119         return servers;
120     }
121 
122     public MimeMessage getValidMessage(String to) throws Exception
123     {
124         MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
125         message.setContent(MESSAGE, "text/plain");
126         message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
127         return message;
128     }
129 
130 }
131