| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.util; |
| 33 | |
|
| 34 | |
import net.sourceforge.pebble.domain.Blog; |
| 35 | |
import net.sourceforge.pebble.domain.State; |
| 36 | |
import org.apache.commons.logging.Log; |
| 37 | |
import org.apache.commons.logging.LogFactory; |
| 38 | |
import net.sourceforge.pebble.web.validation.ValidationContext; |
| 39 | |
import net.sourceforge.pebble.PebbleContext; |
| 40 | |
|
| 41 | |
import javax.mail.Message; |
| 42 | |
import javax.mail.Session; |
| 43 | |
import javax.mail.Transport; |
| 44 | |
import javax.mail.internet.AddressException; |
| 45 | |
import javax.mail.internet.InternetAddress; |
| 46 | |
import javax.mail.internet.MimeMessage; |
| 47 | |
import javax.mail.internet.MimeUtility; |
| 48 | |
import javax.naming.Context; |
| 49 | |
import javax.naming.InitialContext; |
| 50 | |
import java.util.*; |
| 51 | |
import java.util.concurrent.ExecutorService; |
| 52 | |
import java.util.concurrent.Executors; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 0 | public class MailUtils { |
| 60 | |
|
| 61 | |
|
| 62 | 4 | private static Log log = LogFactory.getLog(MailUtils.class); |
| 63 | 4 | private static String ENCODING = "UTF-8"; |
| 64 | |
|
| 65 | |
|
| 66 | 4 | private static ExecutorService pool = Executors.newFixedThreadPool(1); |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public static String getBlogEntryPrefix(Blog blog) { |
| 75 | 28 | return getPrefix(blog, "notification.prefix.blog.entry", "blogentry.blogentry"); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public static String getBlogEntryPrefix(Blog blog, State state) { |
| 86 | 0 | return getPrefix(blog, "notification.prefix.blog.entry", "blogentry.blogentry", state); |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public static String getCommentPrefix(Blog blog) { |
| 96 | 0 | return getPrefix(blog, "notification.prefix.comment", "blogentry.comment"); |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public static String getCommentPrefix(Blog blog, State state) { |
| 107 | 0 | return getPrefix(blog, "notification.prefix.comment", "blogentry.comment", state); |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public static String getTrackbackPrefix(Blog blog, State state) { |
| 118 | 0 | return getPrefix(blog, "notification.prefix.trackback", "blogentry.trackback", state); |
| 119 | |
} |
| 120 | |
|
| 121 | |
private static String getPrefix(Blog blog, String key, String defaultValue, State state) { |
| 122 | 0 | String value = blog.getProperty(key); |
| 123 | 0 | if (value == null) { |
| 124 | 0 | value = I18n.getMessage(blog, defaultValue); |
| 125 | |
} |
| 126 | 0 | return "[" + value + " - " + I18n.getMessage(blog, "admin." + state.getName()) + "]"; |
| 127 | |
} |
| 128 | |
|
| 129 | |
private static String getPrefix(Blog blog, String key, String defaultValue) { |
| 130 | 28 | String value = blog.getProperty(key); |
| 131 | 28 | if (value == null) { |
| 132 | 28 | value = I18n.getMessage(blog, defaultValue); |
| 133 | |
} |
| 134 | 28 | return "[" + value + "]"; |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public static void sendMail(Session session, Blog blog, String to, String subject, String message) { |
| 146 | 0 | Collection set = new HashSet(); |
| 147 | 0 | set.add(to); |
| 148 | 0 | sendMail(session, blog, set, new HashSet(), new HashSet(), subject, message); |
| 149 | 0 | } |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
public static void sendMail(Session session, Blog blog, Collection to, String subject, String message) { |
| 160 | 0 | sendMail(session, blog, to, new HashSet(), new HashSet(), subject, message); |
| 161 | 0 | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public static void sendMail(Session session, Blog blog, Collection to, Collection cc, String subject, String message) { |
| 173 | 0 | sendMail(session, blog, to, cc, new HashSet(), subject, message); |
| 174 | 0 | } |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public static void sendMail(Session session, Blog blog, Collection to, Collection cc, Collection bcc, String subject, String message) { |
| 187 | 0 | Runnable r = new SendMailRunnable(session, blog, to, cc, bcc, subject, message); |
| 188 | 0 | pool.execute(r); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | 0 | static class SendMailRunnable implements Runnable { |
| 196 | |
|
| 197 | |
|
| 198 | |
private Session session; |
| 199 | |
|
| 200 | |
|
| 201 | |
private Blog blog; |
| 202 | |
|
| 203 | |
|
| 204 | |
private Collection to; |
| 205 | |
|
| 206 | |
|
| 207 | |
private Collection cc; |
| 208 | |
|
| 209 | |
|
| 210 | |
private Collection bcc; |
| 211 | |
|
| 212 | |
|
| 213 | |
private String subject; |
| 214 | |
|
| 215 | |
|
| 216 | |
private String message; |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | 0 | public SendMailRunnable(Session session, Blog blog, Collection to, Collection cc, Collection bcc, String subject, String message) { |
| 230 | 0 | this.session = session; |
| 231 | 0 | this.blog = blog; |
| 232 | 0 | this.to = to; |
| 233 | 0 | this.cc = cc; |
| 234 | 0 | this.bcc = bcc; |
| 235 | 0 | this.subject = subject; |
| 236 | 0 | this.message = message; |
| 237 | 0 | } |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
public void run() { |
| 243 | |
try { |
| 244 | |
|
| 245 | 0 | Message msg = new MimeMessage(session); |
| 246 | 0 | msg.setFrom(new InternetAddress(blog.getFirstEmailAddress(), MimeUtility.encodeText(blog.getName(), ENCODING, "B"))); |
| 247 | 0 | Collection internetAddresses = new HashSet(); |
| 248 | 0 | Iterator it = to.iterator(); |
| 249 | 0 | while (it.hasNext()) { |
| 250 | 0 | internetAddresses.add(new InternetAddress(it.next().toString())); |
| 251 | |
} |
| 252 | 0 | msg.addRecipients(Message.RecipientType.TO, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{})); |
| 253 | |
|
| 254 | 0 | internetAddresses = new HashSet(); |
| 255 | 0 | it = cc.iterator(); |
| 256 | 0 | while (it.hasNext()) { |
| 257 | 0 | internetAddresses.add(new InternetAddress(it.next().toString())); |
| 258 | |
} |
| 259 | 0 | msg.addRecipients(Message.RecipientType.CC, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{})); |
| 260 | |
|
| 261 | 0 | internetAddresses = new HashSet(); |
| 262 | 0 | it = bcc.iterator(); |
| 263 | 0 | while (it.hasNext()) { |
| 264 | 0 | internetAddresses.add(new InternetAddress(it.next().toString())); |
| 265 | |
} |
| 266 | 0 | msg.addRecipients(Message.RecipientType.BCC, (InternetAddress[])internetAddresses.toArray(new InternetAddress[]{})); |
| 267 | |
|
| 268 | 0 | msg.setSubject(MimeUtility.encodeText(subject, ENCODING, "B")); |
| 269 | 0 | msg.setSentDate(new Date()); |
| 270 | 0 | msg.setContent(message, "text/html; charset=" + ENCODING); |
| 271 | |
|
| 272 | 0 | log.debug("From : " + blog.getName() + " (" + blog.getFirstEmailAddress() + ")"); |
| 273 | 0 | log.debug("Subject : " + subject); |
| 274 | 0 | log.debug("Message : " + message); |
| 275 | |
|
| 276 | 0 | Transport.send(msg); |
| 277 | 0 | } catch (Exception e) { |
| 278 | 0 | log.error("Notification e-mail could not be sent", e); |
| 279 | 0 | } |
| 280 | 0 | } |
| 281 | |
|
| 282 | |
} |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
public static Session createSession() throws Exception { |
| 291 | 28 | String ref = PebbleContext.getInstance().getConfiguration().getSmtpHost(); |
| 292 | 28 | if (ref.startsWith("java:comp/env")) { |
| 293 | |
|
| 294 | 28 | Context ctx = new InitialContext(); |
| 295 | 28 | return (Session)ctx.lookup(ref); |
| 296 | |
} else { |
| 297 | |
|
| 298 | 0 | Properties props = new Properties(); |
| 299 | 0 | props.put("mail.smtp.host", ref); |
| 300 | 0 | return Session.getDefaultInstance(props, null); |
| 301 | |
} |
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
public static void validate(String email, ValidationContext context) { |
| 311 | 80 | if (email != null) { |
| 312 | |
try { |
| 313 | 64 | InternetAddress ia = new InternetAddress(email, true); |
| 314 | 44 | ia.validate(); |
| 315 | 20 | } catch (AddressException aex) { |
| 316 | 20 | context.addError(aex.getMessage() + ": " + email); |
| 317 | 44 | } |
| 318 | |
} |
| 319 | 80 | } |
| 320 | |
|
| 321 | |
} |