optimized error handling
fixed usage of wrong template
This commit is contained in:
@ -42,11 +42,9 @@
|
|||||||
<artifactId>freemarker</artifactId>
|
<artifactId>freemarker</artifactId>
|
||||||
<version>2.3.23</version>
|
<version>2.3.23</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax</groupId>
|
<groupId>javax</groupId>
|
||||||
<artifactId>javaee-api</artifactId>
|
<artifactId>javaee-api</artifactId>
|
||||||
<version>7.0</version>
|
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package de.muehlencord.shared.account.business.account;
|
package de.muehlencord.shared.account.business.account;
|
||||||
|
|
||||||
import de.muehlencord.shared.account.business.ConfigService;
|
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.MailService;
|
||||||
import de.muehlencord.shared.account.business.mail.MailTemplateException;
|
import de.muehlencord.shared.account.business.mail.MailTemplateException;
|
||||||
import de.muehlencord.shared.account.entity.AccountEntity;
|
import de.muehlencord.shared.account.entity.AccountEntity;
|
||||||
@ -172,7 +173,7 @@ public class AccountControl {
|
|||||||
|
|
||||||
em.merge(account);
|
em.merge(account);
|
||||||
return true;
|
return true;
|
||||||
} catch (MessagingException | MailTemplateException | URISyntaxException | IOException | TemplateException ex) {
|
} catch (MailException ex) {
|
||||||
LOGGER.log(Level.ERROR, "Error while sending password reset mail. " + ex.toString());
|
LOGGER.log(Level.ERROR, "Error while sending password reset mail. " + ex.toString());
|
||||||
if (LOGGER.isDebugEnabled()) {
|
if (LOGGER.isDebugEnabled()) {
|
||||||
LOGGER.log(Level.DEBUG, "Error while sending password reset mail.", ex);
|
LOGGER.log(Level.DEBUG, "Error while sending password reset mail.", ex);
|
||||||
|
|||||||
@ -1,15 +1,12 @@
|
|||||||
package de.muehlencord.shared.account.business.mail;
|
package de.muehlencord.shared.account.business.mail;
|
||||||
|
|
||||||
import de.muehlencord.shared.account.entity.AccountEntity;
|
import de.muehlencord.shared.account.entity.AccountEntity;
|
||||||
import freemarker.template.TemplateException;
|
import java.net.MalformedURLException;
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.ejb.EJB;
|
import javax.ejb.EJB;
|
||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
import javax.faces.application.FacesMessage;
|
|
||||||
import javax.faces.context.ExternalContext;
|
import javax.faces.context.ExternalContext;
|
||||||
import javax.faces.context.FacesContext;
|
import javax.faces.context.FacesContext;
|
||||||
import javax.mail.BodyPart;
|
import javax.mail.BodyPart;
|
||||||
@ -40,7 +37,7 @@ public class MailService {
|
|||||||
sendMail(recipient, "Test email", "This is a test email");
|
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();
|
Date now = new Date();
|
||||||
AccountEntity account = new AccountEntity("joern.muehlencord", "joern@muehlencord.de", "Jörn", "Mühlencord", "secret", 0, "NEW", now, "admin", now, "admin");
|
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);
|
MailDatamodel dataModel = new MailDatamodel(account);
|
||||||
@ -57,17 +54,17 @@ public class MailService {
|
|||||||
message.setText(body);
|
message.setText(body);
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
} catch (MessagingException ex) {
|
} 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 {
|
try {
|
||||||
Message message = new MimeMessage(mailSession);
|
Message message = new MimeMessage(mailSession);
|
||||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
|
||||||
message.setSubject(subject);
|
message.setSubject(subject);
|
||||||
|
|
||||||
String body = mailTemplateService.getStringFromTemplate("password_reset_html", dataModel);
|
String body = mailTemplateService.getStringFromTemplate(templateName, dataModel);
|
||||||
|
|
||||||
BodyPart bodyPart = new MimeBodyPart();
|
BodyPart bodyPart = new MimeBodyPart();
|
||||||
bodyPart.setContent(body, "text/html");
|
bodyPart.setContent(body, "text/html");
|
||||||
@ -78,26 +75,30 @@ public class MailService {
|
|||||||
|
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
} catch (MessagingException | MailTemplateException ex) {
|
} catch (MessagingException | MailTemplateException ex) {
|
||||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error while sending email", "Exception: " + ex.toString());
|
throw new MailException("Error while sending email.", ex);
|
||||||
FacesContext context = FacesContext.getCurrentInstance();
|
|
||||||
context.addMessage(null, message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
MailDatamodel model = new MailDatamodel(account);
|
||||||
|
|
||||||
// String absoluteWebPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath();
|
try {
|
||||||
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
|
// String absoluteWebPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath();
|
||||||
String resetPage = "/login.xhtml?token=" + token;
|
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
|
||||||
URL baseUrl = new URL(externalContext.getRequestScheme(),
|
String resetPage = "/login.xhtml?token=" + token;
|
||||||
externalContext.getRequestServerName(),
|
URL baseUrl;
|
||||||
externalContext.getRequestServerPort(),
|
|
||||||
externalContext.getRequestContextPath());
|
|
||||||
|
|
||||||
model.addParameter("url", baseUrl.toString());
|
// TODO move out of this class, this is not mandatorily connected to a form
|
||||||
model.addParameter("resetUrl", baseUrl.toString() + resetPage);
|
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");
|
sendHTMLMail(account.getEmailaddress(), "Reset your password", model, "password_reset");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user