optimized error handling

fixed usage of wrong template
This commit is contained in:
jomu
2015-12-08 09:05:37 +00:00
parent abc7254266
commit 70a7b6841e
3 changed files with 26 additions and 26 deletions

View File

@ -1,6 +1,7 @@
package de.muehlencord.shared.account.business.account;
import de.muehlencord.shared.account.business.ConfigService;
import de.muehlencord.shared.account.business.mail.MailException;
import de.muehlencord.shared.account.business.mail.MailService;
import de.muehlencord.shared.account.business.mail.MailTemplateException;
import de.muehlencord.shared.account.entity.AccountEntity;
@ -172,7 +173,7 @@ public class AccountControl {
em.merge(account);
return true;
} catch (MessagingException | MailTemplateException | URISyntaxException | IOException | TemplateException ex) {
} catch (MailException ex) {
LOGGER.log(Level.ERROR, "Error while sending password reset mail. " + ex.toString());
if (LOGGER.isDebugEnabled()) {
LOGGER.log(Level.DEBUG, "Error while sending password reset mail.", ex);

View File

@ -1,15 +1,12 @@
package de.muehlencord.shared.account.business.mail;
import de.muehlencord.shared.account.entity.AccountEntity;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.mail.BodyPart;
@ -35,12 +32,12 @@ public class MailService {
@Resource(lookup = "java:jboss/mail/ssgMail")
private Session mailSession;
public void sendTestEmail(String recipient) throws MailException {
sendMail(recipient, "Test email", "This is a test email");
}
public void sendTestHtmlEmail(String recipient) {
public void sendTestHtmlEmail(String recipient) throws MailException {
Date now = new Date();
AccountEntity account = new AccountEntity("joern.muehlencord", "joern@muehlencord.de", "Jörn", "Mühlencord", "secret", 0, "NEW", now, "admin", now, "admin");
MailDatamodel dataModel = new MailDatamodel(account);
@ -57,17 +54,17 @@ public class MailService {
message.setText(body);
Transport.send(message);
} catch (MessagingException ex) {
throw new MailException ("Error while sending email.", ex);
throw new MailException("Error while sending email.", ex);
}
}
public void sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String templateName) {
public void sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String templateName) throws MailException {
try {
Message message = new MimeMessage(mailSession);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
message.setSubject(subject);
String body = mailTemplateService.getStringFromTemplate("password_reset_html", dataModel);
String body = mailTemplateService.getStringFromTemplate(templateName, dataModel);
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(body, "text/html");
@ -78,26 +75,30 @@ public class MailService {
Transport.send(message);
} catch (MessagingException | MailTemplateException ex) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error while sending email", "Exception: " + ex.toString());
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, message);
throw new MailException("Error while sending email.", ex);
}
}
public void sendPasswortResetStartEmail(AccountEntity account, String token) throws MessagingException, URISyntaxException, IOException, TemplateException, MailTemplateException {
public void sendPasswortResetStartEmail(AccountEntity account, String token) throws MailException {
MailDatamodel model = new MailDatamodel(account);
// String absoluteWebPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath();
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
String resetPage = "/login.xhtml?token=" + token;
URL baseUrl = new URL(externalContext.getRequestScheme(),
externalContext.getRequestServerName(),
externalContext.getRequestServerPort(),
externalContext.getRequestContextPath());
try {
// String absoluteWebPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath();
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
String resetPage = "/login.xhtml?token=" + token;
URL baseUrl;
model.addParameter("url", baseUrl.toString());
model.addParameter("resetUrl", baseUrl.toString() + resetPage);
// TODO move out of this class, this is not mandatorily connected to a form
baseUrl = new URL(externalContext.getRequestScheme(),
externalContext.getRequestServerName(),
externalContext.getRequestServerPort(),
externalContext.getRequestContextPath());
model.addParameter("url", baseUrl.toString());
model.addParameter("resetUrl", baseUrl.toString() + resetPage);
} catch (MalformedURLException ex) {
throw new MailException("Error while sending email.", ex);
}
sendHTMLMail(account.getEmailaddress(), "Reset your password", model, "password_reset");
}