File Source: mailutils.java
/*
P/P * Method: net.sourceforge.pebble.util.MailUtils__static_init
*
* Postconditions:
* ENCODING == &"UTF-8"
* init'ed(log)
* init'ed(pool)
*/
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
/*
P/P * Method: void net.sourceforge.pebble.util.MailUtils()
*/
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 */
/*
P/P * Method: Log access$1()
*
* Preconditions:
* init'ed(log)
*
* Postconditions:
* return_value == log
* init'ed(return_value)
*/
61 private static Log log = LogFactory.getLog(MailUtils.class);
/*
P/P * Method: String access$0()
*
* Preconditions:
* init'ed(ENCODING)
*
* Postconditions:
* return_value == ENCODING
* init'ed(return_value)
*/
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 public static void sendMail(Session session, Blog blog, String to, String subject, String message) {
/*
P/P * Method: void sendMail(Session, Blog, String, String, String)
*
* Preconditions:
* pool != null
*/
76 Collection set = new HashSet();
77 set.add(to);
78 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 public static void sendMail(Session session, Blog blog, Collection to, String subject, String message) {
/*
P/P * Method: void sendMail(Session, Blog, Collection, String, String)
*
* Preconditions:
* pool != null
*/
90 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 public static void sendMail(Session session, Blog blog, Collection to, Collection cc, String subject, String message) {
/*
P/P * Method: void sendMail(Session, Blog, Collection, Collection, String, String)
*
* Preconditions:
* pool != null
*/
103 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 public static void sendMail(Session session, Blog blog, Collection to, Collection cc, Collection bcc, String subject, String message) {
/*
P/P * Method: void sendMail(Session, Blog, Collection, Collection, Collection, String, String)
*
* Preconditions:
* pool != null
*/
117 Runnable r = new SendMailRunnable(session, blog, to, cc, bcc, subject, message);
118 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 */
/*
P/P * Method: void net.sourceforge.pebble.util.MailUtils$SendMailRunnable(Session, Blog, Collection, Collection, Collection, String, String)
*
* Postconditions:
* this.bcc == bcc
* init'ed(this.bcc)
* this.blog == blog
* init'ed(this.blog)
* this.cc == cc
* init'ed(this.cc)
* this.message == message
* init'ed(this.message)
* this.session == session
* init'ed(this.session)
* ...
*/
159 public SendMailRunnable(Session session, Blog blog, Collection to, Collection cc, Collection bcc, String subject, String message) {
160 this.session = session;
161 this.blog = blog;
162 this.to = to;
163 this.cc = cc;
164 this.bcc = bcc;
165 this.subject = subject;
166 this.message = message;
167 }
168
169 /**
170 * Performs the processing associated with this thread.
171 */
172 public void run() {
173 try {
174 // create a message and try to send it
/*
P/P * Method: void run()
*
* Preconditions:
* net/sourceforge/pebble/util/MailUtils.log != null
* (soft) init'ed(net/sourceforge/pebble/util/MailUtils.ENCODING)
* (soft) this.bcc != null
* (soft) this.blog != null
* (soft) this.cc != null
* (soft) init'ed(this.message)
* (soft) init'ed(this.session)
* (soft) init'ed(this.subject)
* (soft) this.to != null
*
* Presumptions:
* java.util.Iterator:next(...)@180 != null
* java.util.Iterator:next(...)@187 != null
* java.util.Iterator:next(...)@194 != null
* init'ed(javax.mail.Message$RecipientType.BCC)
* init'ed(javax.mail.Message$RecipientType.CC)
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@179: {1}, {0}
* java.util.Iterator:hasNext(...)@186: {1}, {0}
* java.util.Iterator:hasNext(...)@193: {1}, {0}
*/
175 Message msg = new MimeMessage(session);
176 msg.setFrom(new InternetAddress(blog.getFirstEmailAddress(), MimeUtility.encodeText(blog.getName(), ENCODING, "B")));
177 Collection internetAddresses = new HashSet();
178 Iterator it = to.iterator();
179 while (it.hasNext()) {
180 internetAddresses.add(new InternetAddress(it.next().toString()));
181 }
182 msg.addRecipients(Message.RecipientType.TO, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{}));
183
184 internetAddresses = new HashSet();
185 it = cc.iterator();
186 while (it.hasNext()) {
187 internetAddresses.add(new InternetAddress(it.next().toString()));
188 }
189 msg.addRecipients(Message.RecipientType.CC, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{}));
190
191 internetAddresses = new HashSet();
192 it = bcc.iterator();
193 while (it.hasNext()) {
194 internetAddresses.add(new InternetAddress(it.next().toString()));
195 }
196 msg.addRecipients(Message.RecipientType.BCC, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{}));
197
198 msg.setSubject(MimeUtility.encodeText(subject, ENCODING, "B"));
199 msg.setSentDate(new Date());
200 msg.setContent(message, "text/html; charset=" + ENCODING);
201
202 log.debug("From : " + blog.getName() + " (" + blog.getFirstEmailAddress() + ")");
203 log.debug("Subject : " + subject);
204 log.debug("Message : " + message);
205
206 Transport.send(msg);
207 } catch (Exception e) {
208 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 public static Session createSession() throws Exception {
/*
P/P * Method: Session createSession()
*
* Preconditions:
* net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
*
* Presumptions:
* net.sourceforge.pebble.Configuration:getSmtpHost(...)@221 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:startsWith(...)@222: {0}, {1}
*/
221 String ref = PebbleContext.getInstance().getConfiguration().getSmtpHost();
222 if (ref.startsWith("java:comp/env")) {
223 // this is a JNDI based mail session
224 Context ctx = new InitialContext();
225 return (Session)ctx.lookup(ref);
226 } else {
227 // this is a simple SMTP hostname based session
228 Properties props = new Properties();
229 props.put("mail.smtp.host", ref);
230 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 public static void validate(String email, ValidationContext context) {
241 if (email != null) {
242 try {
/*
P/P * Method: void validate(String, ValidationContext)
*
* Preconditions:
* (soft) context != null
* (soft) context.errors != null
*
* Test Vectors:
* email: Addr_Set{null}, Inverse{null}
*/
243 InternetAddress ia = new InternetAddress(email, true);
244 ia.validate();
245 } catch (AddressException aex) {
246 context.addError(aex.getMessage() + ": " + email);
247 }
248 }
249 }
250
251 }
SofCheck Inspector Build Version : 2.22510
| mailutils.java |
2010-Jun-25 19:40:32 |
| mailutils.class |
2010-Jul-19 20:23:38 |
| mailutils$sendmailrunnable.class |
2010-Jul-19 20:23:38 |