Skip to content

Commit 03e182d

Browse files
committed
add exception handler
1 parent 9917043 commit 03e182d

3 files changed

Lines changed: 65 additions & 26 deletions

File tree

‎spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.beans.factory.annotation.Autowired;
2020
import org.springframework.context.ApplicationEventPublisher;
2121
import org.springframework.context.MessageSource;
22+
import org.springframework.http.MediaType;
2223
import org.springframework.mail.MailAuthenticationException;
2324
import org.springframework.mail.SimpleMailMessage;
2425
import org.springframework.mail.javamail.JavaMailSender;
@@ -35,6 +36,7 @@
3536
import org.springframework.web.bind.annotation.RequestMapping;
3637
import org.springframework.web.bind.annotation.RequestMethod;
3738
import org.springframework.web.bind.annotation.RequestParam;
39+
import org.springframework.web.bind.annotation.ResponseBody;
3840
import org.springframework.web.servlet.ModelAndView;
3941

4042
@Controller
@@ -121,7 +123,6 @@ public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid final Use
121123
public String resendRegistrationToken(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) {
122124
final Locale locale = request.getLocale();
123125
final VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
124-
125126
final User user = userService.getUser(newToken.getToken());
126127
try {
127128
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
@@ -135,32 +136,19 @@ public String resendRegistrationToken(final HttpServletRequest request, final Mo
135136
model.addAttribute("message", e.getLocalizedMessage());
136137
return "redirect:/login.html?lang=" + locale.getLanguage();
137138
}
138-
139139
model.addAttribute("message", messages.getMessage("message.resendToken", null, locale));
140140
return "redirect:/login.html?lang=" + locale.getLanguage();
141141
}
142142

143-
@RequestMapping(value = "/user/resendRegistrationToken2", method = RequestMethod.GET)
144-
public String resendRegistrationToken2(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) {
145-
final Locale locale = request.getLocale();
143+
@RequestMapping(value = "/user/resendRegistrationToken2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
144+
public @ResponseBody String resendRegistrationToken2(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) {
146145
final VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
147-
148146
final User user = userService.getUser(newToken.getToken());
149-
try {
150-
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
151-
final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);
152-
mailSender.send(email);
153-
} catch (final MailAuthenticationException e) {
154-
LOGGER.debug("MailAuthenticationException", e);
155-
return "redirect:/emailError.html?lang=" + locale.getLanguage();
156-
} catch (final Exception e) {
157-
LOGGER.debug(e.getLocalizedMessage(), e);
158-
model.addAttribute("message", e.getLocalizedMessage());
159-
return "redirect:/login.html?lang=" + locale.getLanguage();
160-
}
161-
162-
model.addAttribute("message", messages.getMessage("message.resendToken", null, locale));
163-
return "redirect:/login.html?lang=" + locale.getLanguage();
147+
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
148+
final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);
149+
System.out.println(email.getText());
150+
mailSender.send(email);
151+
return messages.getMessage("message.resendToken", null, request.getLocale());
164152
}
165153

166154
@RequestMapping(value = "/user/resetPassword", method = RequestMethod.POST)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.baeldung.web.error;
2+
3+
import org.springframework.mail.MailAuthenticationException;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.ResponseBody;
7+
import org.springframework.web.context.request.WebRequest;
8+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
9+
10+
import com.fasterxml.jackson.core.JsonProcessingException;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
13+
@ControllerAdvice
14+
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
15+
16+
public RestResponseEntityExceptionHandler() {
17+
super();
18+
}
19+
20+
// API
21+
22+
// 500
23+
@ExceptionHandler({ MailAuthenticationException.class })
24+
public @ResponseBody String handleMail(final RuntimeException ex, final WebRequest request) throws JsonProcessingException {
25+
logger.error("500 Status Code", ex);
26+
return new ObjectMapper().writeValueAsString(ex.getClass().toString());
27+
}
28+
29+
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
30+
public @ResponseBody String handleInternal(final RuntimeException ex, final WebRequest request) throws JsonProcessingException {
31+
logger.error("500 Status Code", ex);
32+
return new ObjectMapper().writeValueAsString(ex.getClass().toString());
33+
}
34+
35+
}

‎spring-security-login-and-registration/src/main/webapp/WEB-INF/view/badUser.jsp‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,28 @@ code="label.form.loginSignUp"></spring:message></a>
2323
<c:if test="${param.expired}">
2424
<br>
2525
<h1>${label.form.resendRegistrationToken}</h1>
26-
<a href="<c:url value="/user/resendRegistrationToken">
27-
<c:param name="token" value="${param.token}"/>
28-
</c:url>">
26+
<button onclick="resendToken()">
2927
<spring:message code="label.form.resendRegistrationToken"></spring:message>
30-
</a>
31-
</c:if>
28+
</button>
29+
3230

31+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
32+
<script type="text/javascript">
33+
function resendToken(){
34+
$.get("<c:url value="/user/resendRegistrationToken2"><c:param name="token" value="${param.token}"/></c:url>", function(data){
35+
if(data.indexOf("MailAuthenticationException") > -1)
36+
{
37+
window.location.href = "<c:url value="/emailError.html"></c:url>";
38+
}
39+
else if(data.indexOf("Exception") > -1){
40+
window.location.href = "<c:url value="/login"><c:param name="message" value="Error"/></c:url>";
41+
}
42+
else{
43+
window.location.href = "<c:url value="/login"></c:url>" + "message=" + data;
44+
}
45+
});
46+
}
47+
</script>
48+
</c:if>
3349
</body>
3450
</html>

0 commit comments

Comments
 (0)