Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart2.png 45% of files have more coverage
52   251   15   6,5
10   110   0,29   4
8     1,88  
2    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  MailUtils       Line # 58 19 0% 9 15 48,3% 0.4827586
  MailUtils.SendMailRunnable       Line # 125 33 0% 6 41 0% 0.0
 
  (26)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.util;
33   
34    import net.sourceforge.pebble.domain.Blog;
35    import org.apache.commons.logging.Log;
36    import org.apache.commons.logging.LogFactory;
37    import net.sourceforge.pebble.web.validation.ValidationContext;
38    import net.sourceforge.pebble.PebbleContext;
39   
40    import javax.mail.Message;
41    import javax.mail.Session;
42    import javax.mail.Transport;
43    import javax.mail.internet.AddressException;
44    import javax.mail.internet.InternetAddress;
45    import javax.mail.internet.MimeMessage;
46    import javax.mail.internet.MimeUtility;
47    import javax.naming.Context;
48    import javax.naming.InitialContext;
49    import java.util.*;
50    import java.util.concurrent.ExecutorService;
51    import java.util.concurrent.Executors;
52   
53    /**
54    * Utilities for e-mail related functions.
55    *
56    * @author Simon Brown
57    */
 
58    public class MailUtils {
59   
60    /** the log used by this class */
61    private static Log log = LogFactory.getLog(MailUtils.class);
62    private static String ENCODING = "UTF-8";
63   
64    /** thread pool used to send e-mail */
65    private static ExecutorService pool = Executors.newFixedThreadPool(1);
66   
67    /**
68    * Sends an e-mail.
69    *
70    * @param blog the notifying blog
71    * @param to the e-mail addresses of the recipients in the TO field
72    * @param subject the subject of the e-mail
73    * @param message the body of the e-mail
74    */
 
75  0 toggle public static void sendMail(Session session, Blog blog, String to, String subject, String message) {
76  0 Collection set = new HashSet();
77  0 set.add(to);
78  0 sendMail(session, blog, set, new HashSet(), new HashSet(), subject, message);
79    }
80   
81    /**
82    * Sends an e-mail.
83    *
84    * @param blog the notifying blog
85    * @param to the e-mail addresses of the recipients in the TO field
86    * @param subject the subject of the e-mail
87    * @param message the body of the e-mail
88    */
 
89  0 toggle public static void sendMail(Session session, Blog blog, Collection to, String subject, String message) {
90  0 sendMail(session, blog, to, new HashSet(), new HashSet(), subject, message);
91    }
92   
93    /**
94    * Sends an e-mail.
95    *
96    * @param blog the notifying blog
97    * @param to the e-mail addresses of the recipients in the TO field
98    * @param cc the e-mail addresses of the recipients in the CC field
99    * @param subject the subject of the e-mail
100    * @param message the body of the e-mail
101    */
 
102  0 toggle public static void sendMail(Session session, Blog blog, Collection to, Collection cc, String subject, String message) {
103  0 sendMail(session, blog, to, cc, new HashSet(), subject, message);
104    }
105   
106    /**
107    * Sends an e-mail.
108    *
109    * @param blog the notifying blog
110    * @param to the e-mail addresses of the recipients in the TO field
111    * @param cc the e-mail addresses of the recipients in the CC field
112    * @param bcc the e-mail addresses of the recipients in the BCC field
113    * @param subject the subject of the e-mail
114    * @param message the body of the e-mail
115    */
 
116  0 toggle public static void sendMail(Session session, Blog blog, Collection to, Collection cc, Collection bcc, String subject, String message) {
117  0 Runnable r = new SendMailRunnable(session, blog, to, cc, bcc, subject, message);
118  0 pool.execute(r);
119    }
120   
121    /**
122    * A thread allowing the e-mail to be sent asynchronously, so the requesting
123    * thread (and therefore the user) isn't held up.
124    */
 
125    static class SendMailRunnable implements Runnable {
126   
127    /** the JavaMail session */
128    private Session session;
129   
130    /** the notifying blog */
131    private Blog blog;
132   
133    /** the e-mail addresses of the recipients in the TO field */
134    private Collection to;
135   
136    /** the e-mail addresses of the recipients in the CC field */
137    private Collection cc;
138   
139    /** the e-mail addresses of the recipients in the BCC field */
140    private Collection bcc;
141   
142    /** the subject of the e-mail */
143    private String subject;
144   
145    /** the body of the e-mail */
146    private String message;
147   
148    /**
149    * Creates a new thread to send a new e-mail.
150    *
151    * @param session a JavaMail Session instance
152    * @param blog the notifying blog
153    * @param to the e-mail addresses of the recipients in the TO field
154    * @param cc the e-mail addresses of the recipients in the CC field
155    * @param bcc the e-mail addresses of the recipients in the BCC field
156    * @param subject the subject of the e-mail
157    * @param message the body of the e-mail
158    */
 
159  0 toggle public SendMailRunnable(Session session, Blog blog, Collection to, Collection cc, Collection bcc, String subject, String message) {
160  0 this.session = session;
161  0 this.blog = blog;
162  0 this.to = to;
163  0 this.cc = cc;
164  0 this.bcc = bcc;
165  0 this.subject = subject;
166  0 this.message = message;
167    }
168   
169    /**
170    * Performs the processing associated with this thread.
171    */
 
172  0 toggle public void run() {
173  0 try {
174    // create a message and try to send it
175  0 Message msg = new MimeMessage(session);
176  0 msg.setFrom(new InternetAddress(blog.getFirstEmailAddress(), MimeUtility.encodeText(blog.getName(), ENCODING, "B")));
177  0 Collection internetAddresses = new HashSet();
178  0 Iterator it = to.iterator();
179  0 while (it.hasNext()) {
180  0 internetAddresses.add(new InternetAddress(it.next().toString()));
181    }
182  0 msg.addRecipients(Message.RecipientType.TO, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{}));
183   
184  0 internetAddresses = new HashSet();
185  0 it = cc.iterator();
186  0 while (it.hasNext()) {
187  0 internetAddresses.add(new InternetAddress(it.next().toString()));
188    }
189  0 msg.addRecipients(Message.RecipientType.CC, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{}));
190   
191  0 internetAddresses = new HashSet();
192  0 it = bcc.iterator();
193  0 while (it.hasNext()) {
194  0 internetAddresses.add(new InternetAddress(it.next().toString()));
195    }
196  0 msg.addRecipients(Message.RecipientType.BCC, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{}));
197   
198  0 msg.setSubject(MimeUtility.encodeText(subject, ENCODING, "B"));
199  0 msg.setSentDate(new Date());
200  0 msg.setContent(message, "text/html; charset=" + ENCODING);
201   
202  0 log.debug("From : " + blog.getName() + " (" + blog.getFirstEmailAddress() + ")");
203  0 log.debug("Subject : " + subject);
204  0 log.debug("Message : " + message);
205   
206  0 Transport.send(msg);
207    } catch (Exception e) {
208  0 log.error("Notification e-mail could not be sent", e);
209    }
210    }
211   
212    }
213   
214    /**
215    * Creates a reference to a JavaMail Session.
216    *
217    * @return a Session instance
218    * @throws Exception if something goes wrong creating a session
219    */
 
220  14 toggle public static Session createSession() throws Exception {
221  14 String ref = PebbleContext.getInstance().getConfiguration().getSmtpHost();
222  14 if (ref.startsWith("java:comp/env")) {
223    // this is a JNDI based mail session
224  14 Context ctx = new InitialContext();
225  14 return (Session)ctx.lookup(ref);
226    } else {
227    // this is a simple SMTP hostname based session
228  0 Properties props = new Properties();
229  0 props.put("mail.smtp.host", ref);
230  0 return Session.getDefaultInstance(props, null);
231    }
232    }
233   
234    /**
235    * Validates the given comment.
236    *
237    * @param email the Comment instance to validate
238    * @param context the context in which to perform validation
239    */
 
240  40 toggle public static void validate(String email, ValidationContext context) {
241  40 if (email != null) {
242  32 try {
243  32 InternetAddress ia = new InternetAddress(email, true);
244  22 ia.validate();
245    } catch (AddressException aex) {
246  10 context.addError(aex.getMessage() + ": " + email);
247    }
248    }
249    }
250   
251    }