View Javadoc

1   /*
2    * $Id: GreenMailUtilities.java 20321 2010-11-24 15:21:24Z dfeist $
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  
17  import java.net.Socket;
18  import java.util.Properties;
19  
20  import javax.mail.Message;
21  import javax.mail.MessagingException;
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 class GreenMailUtilities
30  {
31      protected static Log logger = LogFactory.getLog(GreenMailUtilities.class);
32  
33      public static void storeEmail(UserManager userManager, String email, String user, String password,
34                                    MimeMessage message)
35              throws Exception
36      {
37          // note that with greenmail 1.1 the Servers object is unreliable
38          // and the approach taken in their examples will not work.
39          // the following does work, but may break in a later version
40          // (there is some confusion in the greenmail code about
41          // whether users are identified by email or name alone)
42          // in which case try retrieving by EMAIL rather than USER
43          userManager.createUser(email, user, password);
44          GreenMailUser gmUser = userManager.getUser(user);
45          assert null != gmUser;
46          gmUser.deliver(message);
47      }
48  
49      public static MimeMessage toMessage(String text, String email, String charset) throws MessagingException
50      {
51          MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
52          if (charset != null) 
53          {
54              message.setContent(text, "text/plain; charset=" + charset);
55          } 
56          else 
57          {
58              message.setContent(text, "text/plain");
59          }
60          message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
61          return message;
62      }
63  
64      public static void waitForStartup(String host, int port, int count, long wait) throws InterruptedException
65      {
66          for (int i = 0; i < count; ++i)
67          {
68              Thread.sleep(wait);
69              try {
70                  Socket socket = new Socket(host, port);
71                  socket.close();
72                  logger.info("Successful connection made to port " + port);
73                  return;
74              }
75              catch (Exception e)
76              {
77                  logger.warn("Could not connect to server on " + host + ":" + port + " - " + e.getMessage());
78              }
79          }
80          throw new RuntimeException("Server failed to start within " + (count * wait) + "ms");
81      }
82  
83      public static void robustStartup(GreenMail servers, String host, int port, int startMax, int testMax, long wait)
84              throws InterruptedException
85      {
86          for (int start = 0; start < startMax; ++start)
87          {
88              try
89              {
90                  servers.start();
91                  waitForStartup(host, port, testMax, wait);
92                  return;
93              }
94              catch (Exception e)
95              {
96                  try
97                  {
98                      servers.stop();
99                  }
100                 catch (Throwable t)
101                 {
102                     // ignore
103                 }
104             }
105             Thread.sleep(wait);
106         }
107     }
108 }