add exception handler
This commit is contained in:
parent
99170431c0
commit
03e182d6c7
|
@ -19,6 +19,7 @@ import org.slf4j.LoggerFactory;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mail.MailAuthenticationException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
|
@ -35,6 +36,7 @@ import org.springframework.web.bind.annotation.ModelAttribute;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Controller
|
||||
|
@ -121,7 +123,6 @@ public class RegistrationController {
|
|||
public String resendRegistrationToken(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) {
|
||||
final Locale locale = request.getLocale();
|
||||
final VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
|
||||
|
||||
final User user = userService.getUser(newToken.getToken());
|
||||
try {
|
||||
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
|
@ -135,32 +136,19 @@ public class RegistrationController {
|
|||
model.addAttribute("message", e.getLocalizedMessage());
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
model.addAttribute("message", messages.getMessage("message.resendToken", null, locale));
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user/resendRegistrationToken2", method = RequestMethod.GET)
|
||||
public String resendRegistrationToken2(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) {
|
||||
final Locale locale = request.getLocale();
|
||||
@RequestMapping(value = "/user/resendRegistrationToken2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public @ResponseBody String resendRegistrationToken2(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) {
|
||||
final VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
|
||||
|
||||
final User user = userService.getUser(newToken.getToken());
|
||||
try {
|
||||
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);
|
||||
mailSender.send(email);
|
||||
} catch (final MailAuthenticationException e) {
|
||||
LOGGER.debug("MailAuthenticationException", e);
|
||||
return "redirect:/emailError.html?lang=" + locale.getLanguage();
|
||||
} catch (final Exception e) {
|
||||
LOGGER.debug(e.getLocalizedMessage(), e);
|
||||
model.addAttribute("message", e.getLocalizedMessage());
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
model.addAttribute("message", messages.getMessage("message.resendToken", null, locale));
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);
|
||||
System.out.println(email.getText());
|
||||
mailSender.send(email);
|
||||
return messages.getMessage("message.resendToken", null, request.getLocale());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user/resetPassword", method = RequestMethod.POST)
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package org.baeldung.web.error;
|
||||
|
||||
import org.springframework.mail.MailAuthenticationException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@ControllerAdvice
|
||||
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
public RestResponseEntityExceptionHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
// 500
|
||||
@ExceptionHandler({ MailAuthenticationException.class })
|
||||
public @ResponseBody String handleMail(final RuntimeException ex, final WebRequest request) throws JsonProcessingException {
|
||||
logger.error("500 Status Code", ex);
|
||||
return new ObjectMapper().writeValueAsString(ex.getClass().toString());
|
||||
}
|
||||
|
||||
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
|
||||
public @ResponseBody String handleInternal(final RuntimeException ex, final WebRequest request) throws JsonProcessingException {
|
||||
logger.error("500 Status Code", ex);
|
||||
return new ObjectMapper().writeValueAsString(ex.getClass().toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -23,12 +23,28 @@ code="label.form.loginSignUp"></spring:message></a>
|
|||
<c:if test="${param.expired}">
|
||||
<br>
|
||||
<h1>${label.form.resendRegistrationToken}</h1>
|
||||
<a href="<c:url value="/user/resendRegistrationToken">
|
||||
<c:param name="token" value="${param.token}"/>
|
||||
</c:url>">
|
||||
<button onclick="resendToken()">
|
||||
<spring:message code="label.form.resendRegistrationToken"></spring:message>
|
||||
</a>
|
||||
</c:if>
|
||||
</button>
|
||||
|
||||
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
function resendToken(){
|
||||
$.get("<c:url value="/user/resendRegistrationToken2"><c:param name="token" value="${param.token}"/></c:url>", function(data){
|
||||
if(data.indexOf("MailAuthenticationException") > -1)
|
||||
{
|
||||
window.location.href = "<c:url value="/emailError.html"></c:url>";
|
||||
}
|
||||
else if(data.indexOf("Exception") > -1){
|
||||
window.location.href = "<c:url value="/login"><c:param name="message" value="Error"/></c:url>";
|
||||
}
|
||||
else{
|
||||
window.location.href = "<c:url value="/login"></c:url>" + "message=" + data;
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</c:if>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue