Mercurial > hg > LGDataverses
comparison src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java @ 10:a50cf11e5178
Rewrite LGDataverse completely upgrading to dataverse4.0
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 08 Sep 2015 17:00:21 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:5926d6419569 | 10:a50cf11e5178 |
|---|---|
| 1 /* | |
| 2 * To change this license header, choose License Headers in Project Properties. | |
| 3 * To change this template file, choose Tools | Templates | |
| 4 * and open the template in the editor. | |
| 5 */ | |
| 6 package edu.harvard.iq.dataverse; | |
| 7 | |
| 8 import com.sun.mail.smtp.SMTPSendFailedException; | |
| 9 import com.sun.mail.smtp.SMTPSenderFailedException; | |
| 10 import edu.harvard.iq.dataverse.settings.SettingsServiceBean; | |
| 11 import edu.harvard.iq.dataverse.settings.SettingsServiceBean.Key; | |
| 12 import edu.harvard.iq.dataverse.util.BundleUtil; | |
| 13 import edu.harvard.iq.dataverse.util.SystemConfig; | |
| 14 import java.sql.Timestamp; | |
| 15 import java.text.MessageFormat; | |
| 16 import java.util.Arrays; | |
| 17 import java.util.Date; | |
| 18 import java.util.Properties; | |
| 19 import java.util.Map; | |
| 20 import java.util.HashMap; | |
| 21 import java.util.List; | |
| 22 import java.util.ResourceBundle; | |
| 23 import java.util.logging.Logger; | |
| 24 import javax.annotation.Resource; | |
| 25 import javax.ejb.EJB; | |
| 26 import javax.ejb.Stateless; | |
| 27 import javax.mail.Message; | |
| 28 import javax.mail.MessagingException; | |
| 29 import javax.mail.Session; | |
| 30 import javax.mail.Transport; | |
| 31 import javax.mail.internet.AddressException; | |
| 32 import javax.mail.internet.InternetAddress; | |
| 33 import javax.mail.internet.MimeMessage; | |
| 34 | |
| 35 /** | |
| 36 * | |
| 37 * original author: roberttreacy | |
| 38 */ | |
| 39 @Stateless | |
| 40 public class MailServiceBean implements java.io.Serializable { | |
| 41 | |
| 42 @EJB | |
| 43 UserNotificationServiceBean userNotificationService; | |
| 44 @EJB | |
| 45 DataverseServiceBean dataverseService; | |
| 46 @EJB | |
| 47 DatasetServiceBean datasetService; | |
| 48 @EJB | |
| 49 DatasetVersionServiceBean versionService; | |
| 50 @EJB | |
| 51 SystemConfig systemConfig; | |
| 52 @EJB | |
| 53 SettingsServiceBean settingsService; | |
| 54 | |
| 55 private static final Logger logger = Logger.getLogger(MailServiceBean.class.getCanonicalName()); | |
| 56 | |
| 57 private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" | |
| 58 + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
| 59 | |
| 60 /** | |
| 61 * Creates a new instance of MailServiceBean | |
| 62 */ | |
| 63 public MailServiceBean() { | |
| 64 } | |
| 65 | |
| 66 public void sendMail(String host, String from, String to, String subject, String messageText) { | |
| 67 Properties props = System.getProperties(); | |
| 68 props.put("mail.smtp.host", host); | |
| 69 Session session = Session.getDefaultInstance(props, null); | |
| 70 | |
| 71 try { | |
| 72 Message msg = new MimeMessage(session); | |
| 73 msg.setFrom(new InternetAddress(from)); | |
| 74 msg.setRecipients(Message.RecipientType.TO, | |
| 75 InternetAddress.parse(to, false)); | |
| 76 msg.setSubject(subject); | |
| 77 msg.setText(messageText); | |
| 78 Transport.send(msg); | |
| 79 } catch (AddressException ae) { | |
| 80 ae.printStackTrace(System.out); | |
| 81 } catch (MessagingException me) { | |
| 82 me.printStackTrace(System.out); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 @Resource(name = "mail/notifyMailSession") | |
| 87 private Session session; | |
| 88 | |
| 89 public boolean sendSystemEmail(String to, String subject, String messageText) { | |
| 90 boolean sent = false; | |
| 91 try { | |
| 92 Message msg = new MimeMessage(session); | |
| 93 | |
| 94 InternetAddress systemAddress = getSystemAddress(); | |
| 95 if (systemAddress != null) { | |
| 96 msg.setFrom(systemAddress); | |
| 97 msg.setSentDate(new Date()); | |
| 98 msg.setRecipients(Message.RecipientType.TO, | |
| 99 InternetAddress.parse(to, false)); | |
| 100 msg.setSubject(subject); | |
| 101 msg.setText(messageText + ResourceBundle.getBundle("Bundle").getString("notification.email.closing")); | |
| 102 try { | |
| 103 Transport.send(msg); | |
| 104 sent = true; | |
| 105 } catch (SMTPSendFailedException ssfe) { | |
| 106 logger.warning("Failed to send mail to " + to + " (SMTPSendFailedException)"); | |
| 107 } | |
| 108 } else { | |
| 109 // commenting out the warning so as not to clutter the log of installations that haven't set up mail | |
| 110 // logger.warning("Skipping sending mail to " + to + ", because the \"no-reply\" address not set."); | |
| 111 } | |
| 112 } catch (AddressException ae) { | |
| 113 logger.warning("Failed to send mail to " + to); | |
| 114 ae.printStackTrace(System.out); | |
| 115 } catch (MessagingException me) { | |
| 116 logger.warning("Failed to send mail to " + to); | |
| 117 me.printStackTrace(System.out); | |
| 118 } | |
| 119 return sent; | |
| 120 } | |
| 121 | |
| 122 private InternetAddress getSystemAddress() { | |
| 123 String systemEmail = settingsService.getValueForKey(Key.SystemEmail); | |
| 124 | |
| 125 if (systemEmail!=null) { | |
| 126 try { | |
| 127 return new InternetAddress(systemEmail); | |
| 128 } catch(AddressException e) { | |
| 129 return null; | |
| 130 } | |
| 131 } | |
| 132 return null; | |
| 133 | |
| 134 } | |
| 135 | |
| 136 //@Resource(name="mail/notifyMailSession") | |
| 137 public void sendMail(String from, String to, String subject, String messageText) { | |
| 138 sendMail(from, to, subject, messageText, new HashMap()); | |
| 139 } | |
| 140 | |
| 141 public void sendMail(String from, String to, String subject, String messageText, Map extraHeaders) { | |
| 142 try { | |
| 143 Message msg = new MimeMessage(session); | |
| 144 if (from.matches(EMAIL_PATTERN)) { | |
| 145 msg.setFrom(new InternetAddress(from)); | |
| 146 } else { | |
| 147 // set fake from address; instead, add it as part of the message | |
| 148 //msg.setFrom(new InternetAddress("invalid.email.address@mailinator.com")); | |
| 149 msg.setFrom(getSystemAddress()); | |
| 150 messageText = "From: " + from + "\n\n" + messageText; | |
| 151 } | |
| 152 msg.setSentDate(new Date()); | |
| 153 msg.setRecipients(Message.RecipientType.TO, | |
| 154 InternetAddress.parse(to, false)); | |
| 155 msg.setSubject(subject); | |
| 156 msg.setText(messageText); | |
| 157 | |
| 158 if (extraHeaders != null) { | |
| 159 for (Object key : extraHeaders.keySet()) { | |
| 160 String headerName = key.toString(); | |
| 161 String headerValue = extraHeaders.get(key).toString(); | |
| 162 | |
| 163 msg.addHeader(headerName, headerValue); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 Transport.send(msg); | |
| 168 } catch (AddressException ae) { | |
| 169 ae.printStackTrace(System.out); | |
| 170 } catch (MessagingException me) { | |
| 171 me.printStackTrace(System.out); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 public Boolean sendNotificationEmail(UserNotification notification){ | |
| 176 boolean retval = false; | |
| 177 String emailAddress = getUserEmailAddress(notification); | |
| 178 if (emailAddress != null){ | |
| 179 Object objectOfNotification = getObjectOfNotification(notification); | |
| 180 if (objectOfNotification != null){ | |
| 181 String messageText = getMessageTextBasedOnNotification(notification, objectOfNotification); | |
| 182 String subjectText = getSubjectTextBasedOnNotification(notification); | |
| 183 if (!(messageText.isEmpty() || subjectText.isEmpty())){ | |
| 184 retval = sendSystemEmail(emailAddress, subjectText, messageText); | |
| 185 } else { | |
| 186 logger.warning("Skipping " + notification.getType() + " notification, because couldn't get valid message"); | |
| 187 } | |
| 188 } else { | |
| 189 logger.warning("Skipping " + notification.getType() + " notification, because no valid Object was found"); | |
| 190 } | |
| 191 } else { | |
| 192 logger.warning("Skipping " + notification.getType() + " notification, because email address is null"); | |
| 193 } | |
| 194 return retval; | |
| 195 } | |
| 196 | |
| 197 private String getSubjectTextBasedOnNotification(UserNotification userNotification) { | |
| 198 switch (userNotification.getType()) { | |
| 199 case CREATEDV: | |
| 200 return ResourceBundle.getBundle("Bundle").getString("notification.email.create.dataverse.subject"); | |
| 201 case REQUESTFILEACCESS: | |
| 202 return ResourceBundle.getBundle("Bundle").getString("notification.email.request.file.access.subject"); | |
| 203 case GRANTFILEACCESS: | |
| 204 return ResourceBundle.getBundle("Bundle").getString("notification.email.grant.file.access.subject"); | |
| 205 case REJECTFILEACCESS: | |
| 206 return ResourceBundle.getBundle("Bundle").getString("notification.email.rejected.file.access.subject"); | |
| 207 case MAPLAYERUPDATED: | |
| 208 return ResourceBundle.getBundle("Bundle").getString("notification.email.update.maplayer"); | |
| 209 case CREATEDS: | |
| 210 return ResourceBundle.getBundle("Bundle").getString("notification.email.create.dataset.subject"); | |
| 211 case SUBMITTEDDS: | |
| 212 return ResourceBundle.getBundle("Bundle").getString("notification.email.submit.dataset.subject"); | |
| 213 case PUBLISHEDDS: | |
| 214 return ResourceBundle.getBundle("Bundle").getString("notification.email.publish.dataset.subject"); | |
| 215 case RETURNEDDS: | |
| 216 return ResourceBundle.getBundle("Bundle").getString("notification.email.returned.dataset.subject"); | |
| 217 case CREATEACC: | |
| 218 return ResourceBundle.getBundle("Bundle").getString("notification.email.create.account.subject"); | |
| 219 } | |
| 220 return ""; | |
| 221 } | |
| 222 | |
| 223 private String getDatasetManagePermissionsLink(Dataset dataset){ | |
| 224 return systemConfig.getDataverseSiteUrl() + "/permissions-manage.xhtml?id=" + dataset.getId(); | |
| 225 } | |
| 226 | |
| 227 private String getDatasetLink(Dataset dataset){ | |
| 228 return systemConfig.getDataverseSiteUrl() + "/dataset.xhtml?persistentId=" + dataset.getGlobalId(); | |
| 229 } | |
| 230 | |
| 231 private String getDataverseLink(Dataverse dataverse){ | |
| 232 return systemConfig.getDataverseSiteUrl() + "/dataverse/" + dataverse.getAlias(); | |
| 233 } | |
| 234 | |
| 235 private String getMessageTextBasedOnNotification(UserNotification userNotification, Object targetObject){ | |
| 236 | |
| 237 String messageText = ResourceBundle.getBundle("Bundle").getString("notification.email.greeting"); | |
| 238 DatasetVersion version = null; | |
| 239 Dataset dataset = null; | |
| 240 String pattern =""; | |
| 241 | |
| 242 switch (userNotification.getType()) { | |
| 243 case CREATEDV: | |
| 244 Dataverse dataverse = (Dataverse) targetObject; | |
| 245 Dataverse parentDataverse = dataverse.getOwner(); | |
| 246 // initialize to empty string in the rare case that there is no parent dataverse (i.e. root dataverse just created) | |
| 247 String parentDataverseDisplayName = ""; | |
| 248 String parentDataverseUrl = ""; | |
| 249 if (parentDataverse != null) { | |
| 250 parentDataverseDisplayName = parentDataverse.getDisplayName(); | |
| 251 parentDataverseUrl = getDataverseLink(parentDataverse); | |
| 252 } | |
| 253 String dataverseCreatedMessage = BundleUtil.getStringFromBundle("notification.email.createDataverse", Arrays.asList( | |
| 254 dataverse.getDisplayName(), | |
| 255 getDataverseLink(dataverse), | |
| 256 parentDataverseDisplayName, | |
| 257 parentDataverseUrl, | |
| 258 systemConfig.getGuidesBaseUrl(), | |
| 259 systemConfig.getVersion())); | |
| 260 logger.fine(dataverseCreatedMessage); | |
| 261 return messageText += dataverseCreatedMessage; | |
| 262 case REQUESTFILEACCESS: | |
| 263 dataset = (Dataset) targetObject; | |
| 264 pattern = ResourceBundle.getBundle("Bundle").getString("notification.email.requestFileAccess"); | |
| 265 String[] paramArrayRequestFileAccess = {dataset.getDisplayName(), getDatasetManagePermissionsLink(dataset)}; | |
| 266 messageText += MessageFormat.format(pattern, paramArrayRequestFileAccess); | |
| 267 return messageText; | |
| 268 case GRANTFILEACCESS: | |
| 269 dataset = (Dataset) targetObject; | |
| 270 pattern = ResourceBundle.getBundle("Bundle").getString("notification.email.grantFileAccess"); | |
| 271 String[] paramArrayGrantFileAccess = {dataset.getDisplayName(), getDatasetLink(dataset)}; | |
| 272 messageText += MessageFormat.format(pattern, paramArrayGrantFileAccess); | |
| 273 return messageText; | |
| 274 case REJECTFILEACCESS: | |
| 275 dataset = (Dataset) targetObject; | |
| 276 pattern = ResourceBundle.getBundle("Bundle").getString("notification.email.rejectFileAccess"); | |
| 277 String[] paramArrayRejectFileAccess = {dataset.getDisplayName(), getDatasetLink(dataset)}; | |
| 278 messageText += MessageFormat.format(pattern, paramArrayRejectFileAccess); | |
| 279 return messageText; | |
| 280 case CREATEDS: | |
| 281 version = (DatasetVersion) targetObject; | |
| 282 String datasetCreatedMessage = BundleUtil.getStringFromBundle("notification.email.createDataset", Arrays.asList( | |
| 283 version.getDataset().getDisplayName(), | |
| 284 getDatasetLink(version.getDataset()), | |
| 285 version.getDataset().getOwner().getDisplayName(), | |
| 286 getDataverseLink(version.getDataset().getOwner()), | |
| 287 systemConfig.getGuidesBaseUrl(), | |
| 288 systemConfig.getVersion() | |
| 289 )); | |
| 290 logger.fine(datasetCreatedMessage); | |
| 291 return messageText += datasetCreatedMessage; | |
| 292 case MAPLAYERUPDATED: | |
| 293 version = (DatasetVersion) targetObject; | |
| 294 pattern = ResourceBundle.getBundle("Bundle").getString("notification.email.worldMap.added"); | |
| 295 String[] paramArrayMapLayer = {version.getDataset().getDisplayName(), getDatasetLink(version.getDataset())}; | |
| 296 messageText += MessageFormat.format(pattern, paramArrayMapLayer); | |
| 297 return messageText; | |
| 298 case SUBMITTEDDS: | |
| 299 version = (DatasetVersion) targetObject; | |
| 300 pattern = ResourceBundle.getBundle("Bundle").getString("notification.email.wasSubmittedForReview"); | |
| 301 String[] paramArraySubmittedDataset = {version.getDataset().getDisplayName(), getDatasetLink(version.getDataset()), | |
| 302 version.getDataset().getOwner().getDisplayName(), getDataverseLink(version.getDataset().getOwner())}; | |
| 303 messageText += MessageFormat.format(pattern, paramArraySubmittedDataset); | |
| 304 return messageText; | |
| 305 case PUBLISHEDDS: | |
| 306 version = (DatasetVersion) targetObject; | |
| 307 pattern = ResourceBundle.getBundle("Bundle").getString("notification.email.wasPublished"); | |
| 308 String[] paramArrayPublishedDataset = {version.getDataset().getDisplayName(), getDatasetLink(version.getDataset()), | |
| 309 version.getDataset().getOwner().getDisplayName(), getDataverseLink(version.getDataset().getOwner())}; | |
| 310 messageText += MessageFormat.format(pattern, paramArrayPublishedDataset); | |
| 311 return messageText; | |
| 312 case RETURNEDDS: | |
| 313 version = (DatasetVersion) targetObject; | |
| 314 pattern = ResourceBundle.getBundle("Bundle").getString("notification.email.wasReturnedByReviewer"); | |
| 315 String[] paramArrayReturnedDataset = {version.getDataset().getDisplayName(), getDatasetLink(version.getDataset()), | |
| 316 version.getDataset().getOwner().getDisplayName(), getDataverseLink(version.getDataset().getOwner())}; | |
| 317 messageText += MessageFormat.format(pattern, paramArrayReturnedDataset); | |
| 318 return messageText; | |
| 319 case CREATEACC: | |
| 320 messageText += ResourceBundle.getBundle("Bundle").getString("notification.email.welcome"); | |
| 321 return messageText; | |
| 322 } | |
| 323 | |
| 324 return ""; | |
| 325 } | |
| 326 | |
| 327 private Object getObjectOfNotification (UserNotification userNotification){ | |
| 328 switch (userNotification.getType()) { | |
| 329 case CREATEDV: | |
| 330 return dataverseService.find(userNotification.getObjectId()); | |
| 331 case REQUESTFILEACCESS: | |
| 332 case GRANTFILEACCESS: | |
| 333 case REJECTFILEACCESS: | |
| 334 return datasetService.find(userNotification.getObjectId()); | |
| 335 case MAPLAYERUPDATED: | |
| 336 case CREATEDS: | |
| 337 case SUBMITTEDDS: | |
| 338 case PUBLISHEDDS: | |
| 339 case RETURNEDDS: | |
| 340 return versionService.find(userNotification.getObjectId()); | |
| 341 case CREATEACC: | |
| 342 return userNotification.getUser(); | |
| 343 } | |
| 344 return null; | |
| 345 } | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 private String getUserEmailAddress(UserNotification notification) { | |
| 351 if (notification != null) { | |
| 352 if (notification.getUser() != null) { | |
| 353 if (notification.getUser().getDisplayInfo() != null) { | |
| 354 if (notification.getUser().getDisplayInfo().getEmailAddress() != null) { | |
| 355 logger.fine("Email address: "+notification.getUser().getDisplayInfo().getEmailAddress()); | |
| 356 return notification.getUser().getDisplayInfo().getEmailAddress(); | |
| 357 } | |
| 358 } | |
| 359 } | |
| 360 } | |
| 361 | |
| 362 logger.fine("no email address"); | |
| 363 return null; | |
| 364 } | |
| 365 | |
| 366 } |
