Created
October 2, 2013 15:57
-
-
Save neolitec/6795989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hotelbb.crs.utils.mail; | |
import java.io.UnsupportedEncodingException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import javax.activation.DataHandler; | |
import javax.activation.DataSource; | |
import javax.mail.BodyPart; | |
import javax.mail.MessagingException; | |
import javax.mail.Multipart; | |
import javax.mail.internet.AddressException; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeBodyPart; | |
import javax.mail.internet.MimeMessage; | |
import javax.mail.internet.MimeMessage.RecipientType; | |
import javax.mail.internet.MimeMultipart; | |
import javax.mail.util.ByteArrayDataSource; | |
import org.apache.commons.lang.StringUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.hotelbb.crs.utils.template.TemplateHelper; | |
/** | |
* Classe mettant à disposition les fonctionnalités de mail. Configuration | |
* mail-sms.xml inclus par cxf-servlet.xml. | |
* | |
* @author BrTignac | |
* | |
*/ | |
public class MailHelper { | |
private static String dotSeparator = "."; | |
/** | |
* Expression régulière pour vérifier la syntaxe de email. | |
*/ | |
private String regex; | |
/** | |
* Active ou non le mode de test pour l'envoi de mail. | |
*/ | |
private boolean testMode; | |
/** | |
* Domaine de restriction pour l'envoi de mail. | |
* Seul les destinataires appartenant à ce domaine recevront les mails. | |
*/ | |
private String testDomain; | |
/** | |
* Map de correspondance entre extension et type mime | |
*/ | |
private Map<String, String> mapExtMimeType = new HashMap<String, String>(); | |
/** | |
* Mise en forme des templates. | |
*/ | |
private TemplateHelper templateHelper; | |
/** | |
* Expéditeur sous sa forme mail. | |
*/ | |
private InternetAddress senderIA; | |
/** | |
* Utilitaire d'envoi de mail asynchrone. | |
*/ | |
private AsyncMailSender asyncMailSender; | |
/** | |
* Positionne l'expression régulière. | |
* | |
* @param regexp | |
* expression régulière | |
*/ | |
public final void setRegex(final String regexp) { | |
this.regex = regexp; | |
} | |
public void setTestMode(String testMode) { | |
if(null != testMode && (StringUtils.equals("true", testMode.trim().toLowerCase()) | |
|| StringUtils.equals("1", testMode) | |
|| StringUtils.equals("on", testMode.trim().toLowerCase()))) { | |
this.testMode = true; | |
} else { | |
this.testMode = false; | |
} | |
} | |
public void setTestDomain(String testDomain) { | |
this.testDomain = testDomain; | |
} | |
/** | |
* Le logger. | |
*/ | |
private Logger log = LoggerFactory.getLogger(this.getClass()); | |
/** | |
* Permet l'envoi de mail, en se basant sur le template fourni et en y | |
* injectant les données et les destinataires (un par un). | |
* | |
* @param senderAdress | |
* adresse de l'expéditeur | |
* @param senderName | |
* nom de l'expéditeur | |
* @param subject | |
* sujet du mail | |
* @param templateName | |
* nom du template | |
* @param templateHtml | |
* template au format HTML à alimenter | |
* @param templateText | |
* template au format Texte Brut à alimenter | |
* @param recipients | |
* liste des destinataires | |
* @param datas | |
* données à utiliser | |
* @return null si l'envoi asynchrone n'a pas été invoqué | |
* @throws com.hotelbb.crs.utils.mail.MailException | |
*/ | |
/*public final String send(final String senderAdress, final String senderName, final String subject, final String templateName, | |
final String templateHtml, final String templateText, final List<MailPerson> recipients, final Map<String, Object> datas, | |
final MailCallback mailCallback) throws com.hotelbb.crs.utils.mail.MailException { | |
return send(senderAdress, senderName, subject, templateName, templateHtml, templateText, recipients, datas, null, mailCallback); | |
}*/ | |
/** | |
* Permet l'envoi de mail, en se basant sur le template fourni et en y | |
* injectant les données et les destinataires et les pièces jointes (un par | |
* un). | |
* | |
* @param senderAdress | |
* adresse de l'expéditeur | |
* @param senderName | |
* nom de l'expéditeur | |
* @param subject | |
* sujet du mail | |
* @param templateName | |
* nom du template | |
* @param templateHtml | |
* template au format Html à alimenter | |
* @param templateText | |
* template au format Texte Brut à alimenter | |
* @param recipients | |
* liste des destinataires | |
* @param datas | |
* données à utiliser | |
* @param attachments | |
* pièces jointes | |
* @param mailCallbacks | |
* callbacks d'exécution | |
* @param async | |
* vrai pour envoyer en asynchrone | |
* @return null si l'envoi asynchrone n'a pas été invoqué | |
* @throws com.hotelbb.crs.utils.mail.MailException | |
*/ | |
private final String send(final String senderAdress, final String senderName, final String subject, final String templateName, | |
final String templateHtml, final String templateText, final List<MailPerson> recipients, final Map<String, Object> datas, | |
final List<MailAttachment> attachments, final List<MailCallback> mailCallbacks, final boolean async) | |
throws com.hotelbb.crs.utils.mail.MailException { | |
InternetAddress iaSender = senderIA; | |
// Vérification de l'expéditeur | |
if (senderAdress != null && !"".equals(senderAdress)) { | |
if (!senderAdress.matches(regex)) { | |
log.error("Impossible de créer l'expéditeur : email invalide : " + senderAdress); | |
} else { | |
try { | |
iaSender = new InternetAddress(senderAdress, senderName); | |
} catch (UnsupportedEncodingException ex) { | |
log.error("Impossible de créer l'expéditeur : " + senderAdress, ex); | |
} | |
} | |
} | |
// Vérification du destinataire | |
// Tous les destinataires doivent être OK sinon aucun mail n'est envoyé | |
if(testMode) { | |
List<String> mailErrors = new ArrayList<String>(); | |
for(MailPerson mailPerson : recipients) { | |
if(null != mailPerson.getEmail() && !mailPerson.getEmail().trim().endsWith(testDomain)) { | |
mailErrors.add(mailPerson.getEmail()); | |
} | |
} | |
if(mailErrors.size() > 0) { | |
throw new com.hotelbb.crs.utils.mail.MailException("Mode TEST : restriction par domaine : " + StringUtils.join(mailErrors, ",") + " pas dans le domaine " + testDomain); | |
} | |
} | |
return send(iaSender, subject, templateName, templateHtml, templateText, recipients, datas, attachments, mailCallbacks); | |
} | |
/** | |
* Permet l'envoi de mail, en se basant sur le template fourni et en y | |
* injectant les données et les destinataires et les pièces jointes (un par | |
* un). (Synchrone) | |
* | |
* @param senderAdress | |
* adresse de l'expéditeur | |
* @param senderName | |
* nom de l'expéditeur | |
* @param subject | |
* sujet du mail | |
* @param templateName | |
* nom du template | |
* @param templateHtml | |
* template au format Html à alimenter | |
* @param templateText | |
* template au format Texte Brut à alimenter | |
* @param recipients | |
* liste des destinataires | |
* @param datas | |
* données à utiliser | |
* @param attachments | |
* pièces jointes | |
* @param mailCallbacks | |
* callbacks d'exécution | |
* @param async | |
* vrai pour envoyer en asynchrone | |
* @return null si l'envoi asynchrone n'a pas été invoqué | |
* @throws com.hotelbb.crs.utils.mail.MailException | |
*/ | |
public final String sendSync(final String senderAdress, final String senderName, final String subject, final String templateName, | |
final String templateHtml, final String templateText, final List<MailPerson> recipients, final Map<String, Object> datas, | |
final List<MailAttachment> attachments, final List<MailCallback> mailCallbacks) | |
throws com.hotelbb.crs.utils.mail.MailException { | |
return send(senderAdress, senderName, subject, templateName, templateHtml, templateText, recipients, datas, attachments, mailCallbacks, false); | |
} | |
/** | |
* Permet l'envoi de mail, en se basant sur le template fourni et en y | |
* injectant les données et les destinataires et les pièces jointes (un par | |
* un). (Asynchrone) | |
* | |
* @param senderAdress | |
* adresse de l'expéditeur | |
* @param senderName | |
* nom de l'expéditeur | |
* @param subject | |
* sujet du mail | |
* @param templateName | |
* nom du template | |
* @param templateHtml | |
* template au format Html à alimenter | |
* @param templateText | |
* template au format Texte Brut à alimenter | |
* @param recipients | |
* liste des destinataires | |
* @param datas | |
* données à utiliser | |
* @param attachments | |
* pièces jointes | |
* @param mailCallbacks | |
* callbacks d'exécution | |
* @param async | |
* vrai pour envoyer en asynchrone | |
* @return null si l'envoi asynchrone n'a pas été invoqué | |
* @throws com.hotelbb.crs.utils.mail.MailException | |
*/ | |
public final String send(final String senderAdress, final String senderName, final String subject, final String templateName, | |
final String templateHtml, final String templateText, final List<MailPerson> recipients, final Map<String, Object> datas, | |
final List<MailAttachment> attachments, final List<MailCallback> mailCallbacks) | |
throws com.hotelbb.crs.utils.mail.MailException { | |
return send(senderAdress, senderName, subject, templateName, templateHtml, templateText, recipients, datas, attachments, mailCallbacks, true); | |
} | |
/** | |
* Permet l'envoi de mail, en se basant sur le template fourni et en y | |
* injectant les données et les destinataires (un par un). | |
* | |
* @param sender | |
* adresse de l'expéditeur | |
* @param subject | |
* sujet du mail | |
* @param templateName | |
* nom du template | |
* @param templateHtml | |
* template au format HTML | |
* @param templateText | |
* template au format TextBrut | |
* @param recipients | |
* liste des destinataires | |
* @param datas | |
* données à utiliser | |
* @param attachements | |
* pièces jointes | |
* @param callbacks | |
* liste de callbacks exécutés en cas de succès | |
* @return null si l'envoi n'a pas été fait | |
* @throws com.hotelbb.crs.utils.mail.MailException | |
* @throws | |
*/ | |
private String send(final InternetAddress sender, final String subject, final String templateName, | |
final String templateHtml, final String templateText, final List<MailPerson> recipients, final Map<String, Object> datas, | |
final List<MailAttachment> attachments, final List<MailCallback> mailCallbacks) | |
throws com.hotelbb.crs.utils.mail.MailException { | |
String msgBodyHtml = templateHelper.mergeModelAndTemplateIntoString(templateName, templateHtml, datas); | |
String msgBodyText = templateHelper.mergeModelAndTemplateIntoString(templateName, templateText, datas); | |
// on envoie les mails un par un. | |
for (MailPerson recipient : recipients) { | |
datas.put("recipient", recipient); | |
MimeMessage mail = asyncMailSender.createMimeMessage(); | |
try { | |
mail.setFrom(sender); | |
mail.setRecipient(RecipientType.TO, new InternetAddress(recipient.getEmail())); | |
if (subject != null) { | |
mail.setSubject(templateHelper.mergeModelAndTemplateIntoString(templateName, subject, datas)); | |
} | |
// Création du mail en multipart pour gérer le mail en text/HTML et en text/plain | |
Multipart mp = new MimeMultipart("mixed"); | |
mail.setContent(mp); | |
MimeMultipart nestedRelatedMultipart = new MimeMultipart("related"); | |
MimeBodyPart relatedBodyPart = new MimeBodyPart(); | |
relatedBodyPart.setContent(nestedRelatedMultipart); | |
mp.addBodyPart(relatedBodyPart); | |
MimeMultipart messageBody = new MimeMultipart("alternative"); | |
MimeBodyPart bodyPart = null; | |
for (int i = 0; i < nestedRelatedMultipart.getCount(); i++) { | |
BodyPart bp = nestedRelatedMultipart.getBodyPart(i); | |
if (bp.getFileName() == null) { | |
bodyPart = (MimeBodyPart) bp; | |
} | |
} | |
if (bodyPart == null) { | |
MimeBodyPart mimeBodyPart = new MimeBodyPart(); | |
nestedRelatedMultipart.addBodyPart(mimeBodyPart); | |
bodyPart = mimeBodyPart; | |
} | |
bodyPart.setContent(messageBody, "text/alternative"); | |
MimeBodyPart textPart = new MimeBodyPart(); | |
textPart.setContent(msgBodyText, "text/plain; charset=UTF-8"); | |
messageBody.addBodyPart(textPart); | |
MimeBodyPart htmlPart = new MimeBodyPart(); | |
htmlPart.setContent(msgBodyHtml, "text/html; charset=UTF-8"); | |
messageBody.addBodyPart(htmlPart); | |
if (null != attachments && attachments.size() > 0) { | |
// Pièce jointes | |
for (MailAttachment attachment : attachments) { | |
MimeBodyPart attachFilePart = new MimeBodyPart(); | |
String extension = getFileExtension(attachment.getName()); | |
String mimeType = ""; | |
if (extension == null || (mimeType = mapExtMimeType.get(extension)) == null) { | |
throw new com.hotelbb.crs.utils.mail.MailException("Type de fichier joint inconnu : " | |
+ extension); | |
} | |
DataSource dataSource = new ByteArrayDataSource(attachment.getBytes(), mimeType); | |
attachFilePart.setDataHandler(new DataHandler(dataSource)); | |
attachFilePart.setFileName(attachment.getName()); | |
mp.addBodyPart(attachFilePart); | |
} | |
} | |
// Envoi du message | |
asyncMailSender.send(mail, mailCallbacks); | |
} catch (AddressException ae) { | |
log.error("Impossible de créer l'adresse du destinataire" + recipient.getEmail(), ae); | |
throw new com.hotelbb.crs.utils.mail.MailException("Impossible de créer l'adresse du destinataire" | |
+ recipient.getEmail(), ae); | |
} catch (MessagingException me) { | |
log.error("Erreur de préparation du mail pour " + recipient.getEmail() + ", template " + templateName, | |
me); | |
throw new com.hotelbb.crs.utils.mail.MailException("Erreur de préparation du mail pour " | |
+ recipient.getEmail() + ", template " + templateName, me); | |
} catch (MailException me) { | |
log.error("Erreur d'envoi de mail pour " + recipient.getEmail() + ", template " + templateName, me); | |
throw new com.hotelbb.crs.utils.mail.MailException("Erreur d'envoi de mail pour " | |
+ recipient.getEmail() + ", template " + templateName, me); | |
} | |
} | |
return msgBodyHtml; | |
} | |
/** | |
* This method takes File Object as parameter and returns the extension of | |
* the file. | |
* | |
* @param file : nom du fichier à traiter. | |
* - a file object. | |
* @return -String -the extension of the file. | |
*/ | |
public static String getFileExtension(final String filename) { | |
if (filename == null) { | |
return null; | |
} | |
int extIndex = filename.lastIndexOf(dotSeparator); | |
if (extIndex == -1) { | |
return ""; | |
} else { | |
return filename.substring(extIndex + 1); | |
} | |
} | |
/** | |
* Positionne l'utilitaire de template. | |
* | |
* @param templateHelper | |
* l'utilitaire | |
*/ | |
public final void setTemplateHelper(final TemplateHelper templateHelper) { | |
this.templateHelper = templateHelper; | |
} | |
/** | |
* Positionne l'expéditeur. | |
* | |
* @param sender | |
* expéditeur | |
*/ | |
public final void setSender(final String sender) { | |
try { | |
senderIA = new InternetAddress(sender); | |
} catch (AddressException ae) { | |
log.error("Impossible de créer l'adresse de l'expéditeur" + sender, ae); | |
} | |
} | |
/** | |
* Positionne le mailer asynchrone. | |
* | |
* @param asyncMailSender | |
* le mailer | |
*/ | |
public final void setAsyncMailSender(final AsyncMailSender asyncMailSender) { | |
this.asyncMailSender = asyncMailSender; | |
} | |
/** | |
* @param mapExtMimeType | |
* the mapExtMimeType to set | |
*/ | |
public final void setMapExtMimeType(final Map<String, String> mapExtMimeType) { | |
this.mapExtMimeType = mapExtMimeType; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hotelbb.crs.business.impl; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Map; | |
import org.apache.commons.lang.StringUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import com.hotelbb.crs.business.MailService; | |
import com.hotelbb.crs.dataaccess.log.MailLogRepository; | |
import com.hotelbb.crs.dataaccess.template.MailTemplateRepository; | |
import com.hotelbb.crs.dto.log.MailLogAndContent; | |
import com.hotelbb.crs.model.log.MailLog; | |
import com.hotelbb.crs.model.template.MailTemplate; | |
import com.hotelbb.crs.utils.mail.MailAttachment; | |
import com.hotelbb.crs.utils.mail.MailCallback; | |
import com.hotelbb.crs.utils.mail.MailHelper; | |
import com.hotelbb.crs.utils.mail.MailPerson; | |
import com.hotelbb.crs.utils.monitoring.ExceptionLogHelper; | |
import com.hotelbb.crs.utils.serializer.MailSerializer; | |
import com.hotelbb.dto.enumerations.EnumExceptionStatus; | |
import com.hotelbb.dto.mail.MailConstants; | |
import com.hotelbb.exceptions.BusinessException; | |
/** | |
* Implémentation de l'interface MailService. | |
* | |
* @author StMoallic | |
*/ | |
@Service | |
public class MailServiceImpl implements MailService { | |
/** | |
* Le logger. | |
*/ | |
private Logger logger = LoggerFactory.getLogger(this.getClass()); | |
@Autowired | |
private MailTemplateRepository mailTemplateRepository; | |
@Autowired | |
private MailLogRepository mailLogRepository; | |
@Autowired | |
private MailHelper mailHelper; | |
/** | |
* Callback d'envoi de mails. | |
*/ | |
private class MailCallbackImpl implements MailCallback { | |
private List<MailLogAndContent> mailLogWrappers = null; | |
public MailCallbackImpl(final List<MailLogAndContent> mailLogWrappers) { | |
this.mailLogWrappers = mailLogWrappers; | |
} | |
@Override | |
public void doThis(final Throwable e) { | |
List<MailLog> persistedMailLog = new ArrayList<MailLog>(); | |
for (MailLogAndContent logWrapper : mailLogWrappers) { | |
MailLog log = mailLogRepository.findOne(logWrapper.getMailLog().getId()); | |
persistedMailLog.add(log); | |
log.setEventDate(new Date()); | |
if (null != e) { | |
if (e instanceof BusinessException) { | |
// pas de stacktrace pour les business | |
logger.warn("mail send error = " + e.getMessage()); | |
} else { | |
// stacktrace pour le reste | |
logger.warn("mail send error = " + ExceptionLogHelper.toString(e)); | |
} | |
// Log du contenu de l'email | |
logger.warn(logWrapper.getContent()); | |
log.setStatus(MailConstants.MAIL_STATUS_FAILURE); | |
String errMsg = e.getMessage(); | |
log.setStatusDescription(errMsg.substring(0, Math.min(errMsg.length(), 255))); | |
} else { | |
// log.setContent(null); | |
log.setStatus(MailConstants.MAIL_STATUS_OK); | |
} | |
} | |
for (MailLog log : persistedMailLog) { | |
mailLogRepository.save(log); | |
} | |
} | |
} | |
@Override | |
public final boolean sendMail(final String senderAdress, final String senderName, final String templateName, | |
final String lang, final Map<String, Object> model, final List<MailPerson> recipients) | |
throws BusinessException { | |
return sendMail(senderAdress, senderName, templateName, lang, model, recipients, null, null); | |
} | |
@Override | |
public final boolean sendMail(final String senderAdress, final String senderName, final String templateName, | |
final String lang, final Map<String, Object> model, final List<MailPerson> recipients, | |
final List<MailAttachment> attachments) throws BusinessException { | |
return sendMail(senderAdress, senderName, templateName, lang, model, recipients, attachments, null); | |
} | |
private final boolean sendMail(String senderAdress, String senderName, String templateName, String langue, | |
Map<String, Object> model, List<MailPerson> recipients, List<MailAttachment> attachments, | |
List<MailCallback> callbacks, boolean async) throws BusinessException { | |
String lang = langue; | |
if (StringUtils.isEmpty(lang)) { | |
lang = MailConstants.MAIL_DEFAULT_LANGUAGE; | |
} | |
final List<MailLogAndContent> mailLogWrappers = new ArrayList<MailLogAndContent>(); | |
try { | |
// Récupération du template | |
MailTemplate templateHtml = findMailTemplate(templateName, lang, "html"); | |
MailTemplate templateText = findMailTemplate(templateName, lang, "text"); | |
if (null == templateHtml) { | |
// On force pour récupérer le template en français | |
templateHtml = findMailTemplate(templateName, "fr", "html"); | |
} | |
// Enregistrement en table de logs des mails envoyés | |
for (MailPerson person : recipients) { | |
MailLogAndContent logWrapper = new MailLogAndContent(); | |
MailLog log = new MailLog(); | |
log.setEventDate(new Date()); | |
log.setRecipient(person.getEmail()); | |
log.setSender(senderAdress); | |
log.setStatus(MailConstants.MAIL_STATUS_PENDING); | |
log.setSubject(templateHtml != null ? templateHtml.getSubject() : null); | |
log.setTemplate(templateHtml != null ? templateHtml.getName() : null); | |
log = mailLogRepository.save(log); | |
logWrapper.setMailLog(log); | |
mailLogWrappers.add(logWrapper); | |
} | |
if (null != templateHtml) { | |
String html = templateHtml.getTemplate(); | |
String text = ""; | |
if (null != templateText) { | |
text = templateText.getTemplate(); | |
} | |
// Packaging des callbacks d'envoi | |
List<MailCallback> mailCallbacks = new ArrayList<MailCallback>(); | |
// Callback métiers | |
if (null != callbacks) { | |
mailCallbacks.addAll(callbacks); | |
} | |
// Callback technique (gestion des logs MailLog) | |
MailCallback mailCallback = new MailCallbackImpl(mailLogWrappers); | |
mailCallbacks.add(mailCallback); | |
String mailContent; | |
if(!async) { | |
mailContent = mailHelper.sendSync(senderAdress, senderName, templateHtml.getSubject(), templateName, | |
html, text, recipients, model, attachments, mailCallbacks); | |
} else { | |
mailContent = mailHelper.send(senderAdress, senderName, templateHtml.getSubject(), templateName, | |
html, text, recipients, model, attachments, mailCallbacks); | |
} | |
String contentData = null; | |
// On enregistre les données des mails envoyés | |
for (MailLogAndContent logWrapper : mailLogWrappers) { | |
if (null != logWrapper.getMailLog()) { | |
try { | |
contentData = MailSerializer.serializeData(model); | |
if (null != contentData) { | |
// On supprime les données que l'on ne veut pas stocker dans MailLog | |
if (contentData.contains("I18nLabels")) { | |
int posDeb = contentData.indexOf("\"I18nLabels"); | |
int posFin = contentData.indexOf("}", posDeb); | |
String i18nLabels = contentData.substring(posDeb, posFin + 2); | |
contentData = contentData.replace(i18nLabels, ""); | |
} | |
logWrapper.getMailLog().setContentData(contentData); | |
} | |
} catch (Exception e) { | |
logger.warn(e.getMessage()); | |
} | |
} | |
logWrapper.setContent(mailContent); | |
} | |
return mailContent != null; | |
} else { | |
for (MailLogAndContent logWrapper : mailLogWrappers) { | |
if (null != logWrapper.getMailLog()) { | |
logWrapper.getMailLog().setStatus(MailConstants.MAIL_STATUS_FAILURE); | |
} | |
} | |
logger.error("MAILING: le template \"" + templateName + "\" n'existe pas."); | |
throw new BusinessException(EnumExceptionStatus.Mail_UnknownTemplate, "MAILING: le template \"" | |
+ templateName + "\" n'existe pas."); | |
} | |
} catch (Exception e) { | |
if (e instanceof BusinessException) { | |
// pas de stacktrace pour les business | |
logger.warn("mail send error = " + e.getMessage()); | |
} else { | |
// stacktrace pour le reste | |
logger.warn("mail send error = " + ExceptionLogHelper.toString(e)); | |
} | |
for (MailLogAndContent logWrapper : mailLogWrappers) { | |
if (null != logWrapper.getMailLog()) { | |
logWrapper.getMailLog().setStatus(MailConstants.MAIL_STATUS_FAILURE); | |
logWrapper.getMailLog().setStatusDescription(e.getMessage()); | |
} | |
} | |
throw new BusinessException(EnumExceptionStatus.Mail_SendError, e.getMessage()); | |
} finally { | |
for (MailLogAndContent logWrapper : mailLogWrappers) { | |
if (null != logWrapper.getMailLog()) { | |
mailLogRepository.save(logWrapper.getMailLog()); | |
} | |
} | |
} | |
} | |
@Override | |
public boolean sendMail(String senderAdress, String senderName, String templateName, String langue, | |
Map<String, Object> model, List<MailPerson> recipients, List<MailAttachment> attachments, | |
List<MailCallback> callbacks) throws BusinessException { | |
return sendMail(senderAdress, senderName, templateName, langue, model, recipients, attachments, callbacks, true); | |
} | |
@Override | |
public boolean sendMailSync(String senderAdress, String senderName, String templateName, String langue, | |
Map<String, Object> model, List<MailPerson> recipients, List<MailAttachment> attachments, | |
List<MailCallback> callbacks) throws BusinessException { | |
return sendMail(senderAdress, senderName, templateName, langue, model, recipients, attachments, callbacks, true); | |
} | |
/** | |
* Récupération des templates de mails. | |
* | |
* @param templateName nom du template | |
* @param lang langue | |
* @param type type (HTML ou TEXT) | |
* @return template | |
*/ | |
protected final MailTemplate findMailTemplate(final String templateName, final String lang, final String type) { | |
return mailTemplateRepository.findMailTemplate(templateName, lang, type); | |
} | |
@Override | |
public final void saveMail(final MailTemplate mailTemplate) { | |
// Vérification de l'existence du template de mail | |
Calendar cal = Calendar.getInstance(); | |
MailTemplate verif = mailTemplateRepository.findMailTemplate(mailTemplate.getName(), mailTemplate.getLang(), | |
mailTemplate.getTypeTemplate()); | |
if (null == verif) { | |
MailTemplate tpl = mailTemplate; | |
tpl.setCreateDate(cal.getTime()); | |
tpl.setUpdateDate(cal.getTime()); | |
mailTemplateRepository.save(tpl); | |
} else { | |
verif.setUserName(mailTemplate.getUserName()); | |
verif.setUpdateDate(cal.getTime()); | |
if ((null == verif.getName()) || (!verif.getName().equals(mailTemplate.getName()))) { | |
verif.setName(mailTemplate.getName()); | |
} | |
if ((null == verif.getDescription()) || (!verif.getDescription().equals(mailTemplate.getDescription()))) { | |
verif.setDescription(mailTemplate.getDescription()); | |
} | |
if ((null == verif.getLang()) || (!verif.getLang().equals(mailTemplate.getLang()))) { | |
verif.setLang(mailTemplate.getLang()); | |
} | |
if ((null == verif.getSubject()) || (!verif.getSubject().equals(mailTemplate.getSubject()))) { | |
verif.setSubject(mailTemplate.getSubject()); | |
} | |
if ((null == verif.getTemplate()) || (!verif.getTemplate().equals(mailTemplate.getTemplate()))) { | |
verif.setTemplate(mailTemplate.getTemplate()); | |
} | |
if ((null == verif.getTypeTemplate()) || (!verif.getTypeTemplate().equals(mailTemplate.getTypeTemplate()))) { | |
verif.setTypeTemplate(mailTemplate.getTypeTemplate()); | |
} | |
} | |
} | |
@Override | |
public final MailTemplate getMailTemplate(final String templateName, final String lang, final String typeTemplate) { | |
return mailTemplateRepository.findMailTemplate(templateName, lang, typeTemplate); | |
} | |
@Override | |
public final List<MailTemplate> getMailTemplates() { | |
return mailTemplateRepository.findMailTemplateOrdered(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment