Skip to content

Commit 90b5676

Browse files
committed
handle 409 status code
1 parent 2abdf14 commit 90b5676

5 files changed

Lines changed: 57 additions & 39 deletions

File tree

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,17 @@ function register(){
6363
if(data.message == "success"){
6464
window.location.href = "<c:url value="/successRegister.html"></c:url>";
6565
}
66-
else if(data.message == "email"){
67-
$("#emailError").show().html(data.error);
68-
}
69-
else{
70-
71-
var errors = $.parseJSON(data.message);
72-
$.each( errors, function( index,item ){
73-
$("#"+item.field+"Error").show().html(item.defaultMessage);
74-
});
75-
errors = $.parseJSON(data.error);
76-
$.each( errors, function( index,item ){
77-
$("#globalError").show().append(item.defaultMessage+"<br>");
78-
});
79-
}
66+
67+
8068
})
8169
.fail(function(data) {
8270
if(data.responseJSON.error.indexOf("MailError") > -1)
8371
{
8472
window.location.href = "<c:url value="/emailError.html"></c:url>";
8573
}
74+
else if(data.responseJSON.error == "UserAlreadyExist"){
75+
$("#emailError").show().html(data.responseJSON.message);
76+
}
8677
else if(data.responseJSON.error.indexOf("InternalError") > -1){
8778
window.location.href = "<c:url value="/login.html"></c:url>" + "?message=" + data.responseJSON.message;
8879
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.baeldung.persistence.service.UserDto;
1515
import org.baeldung.registration.OnRegistrationCompleteEvent;
1616
import org.baeldung.validation.EmailExistsException;
17+
import org.baeldung.web.error.UserAlreadyExistException;
1718
import org.baeldung.web.error.UserNotFoundException;
1819
import org.baeldung.web.util.GenericResponse;
1920
import org.slf4j.Logger;
@@ -71,7 +72,7 @@ public GenericResponse registerUserAccount(@Valid final UserDto accountDto, fina
7172

7273
final User registered = createUserAccount(accountDto);
7374
if (registered == null) {
74-
return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale()));
75+
throw new UserAlreadyExistException();
7576
}
7677
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
7778
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));

‎spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public ResponseEntity<Object> handleUserNotFound(final RuntimeException ex, fina
5252
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
5353
}
5454

55+
// 409
56+
@ExceptionHandler({ UserAlreadyExistException.class })
57+
public ResponseEntity<Object> handleUserAlreadyExist(final RuntimeException ex, final WebRequest request) {
58+
logger.error("409 Status Code", ex);
59+
final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("message.regError", null, request.getLocale()), "UserAlreadyExist");
60+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
61+
}
62+
5563
// 500
5664
@ExceptionHandler({ MailAuthenticationException.class })
5765
public ResponseEntity<Object> handleMail(final RuntimeException ex, final WebRequest request) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.web.error;
2+
3+
public final class UserAlreadyExistException extends RuntimeException {
4+
5+
private static final long serialVersionUID = 5861310537366287163L;
6+
7+
public UserAlreadyExistException() {
8+
super();
9+
}
10+
11+
public UserAlreadyExistException(final String message, final Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public UserAlreadyExistException(final String message) {
16+
super(message);
17+
}
18+
19+
public UserAlreadyExistException(final Throwable cause) {
20+
super(cause);
21+
}
22+
23+
}

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<html>
77
<head>
88
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
9+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
910
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
1011
<title><spring:message code="label.form.title"></spring:message></title>
1112
</head>
@@ -16,74 +17,68 @@
1617
<spring:message code="label.form.title"></spring:message>
1718
</h1>
1819
<br>
19-
<form action="/" method="POST" enctype="utf8">
20+
<form action="/" method="POST" enctype="utf8" onsubmit="register()">
2021
<div class="form-group row" >
2122
<label class="col-sm-3"><spring:message code="label.user.firstName"></spring:message></label>
22-
<span class="col-sm-5"><input class="form-control" name="firstName" value="" /></span>
23+
<span class="col-sm-5"><input class="form-control" name="firstName" value="" required/></span>
2324
<span id="firstNameError" class="alert alert-danger col-sm-4" style="display:none"></span>
2425

2526
</div>
2627
<div class="form-group row">
2728
<label class="col-sm-3"><spring:message code="label.user.lastName"></spring:message></label>
28-
<span class="col-sm-5"><input class="form-control" name="lastName" value="" /></span>
29+
<span class="col-sm-5"><input class="form-control" name="lastName" value="" required/></span>
2930
<span id="lastNameError" class="alert alert-danger col-sm-4" style="display:none"></span>
3031

3132
</div>
3233
<div class="form-group row">
3334
<label class="col-sm-3"><spring:message code="label.user.email"></spring:message></label>
34-
<span class="col-sm-5"><input class="form-control" name="email" value="" /></span>
35+
<span class="col-sm-5"><input type="email" class="form-control" name="email" value="" required/></span>
3536
<span id="emailError" class="alert alert-danger col-sm-4" style="display:none"></span>
3637

3738
</div>
3839
<div class="form-group row">
3940
<label class="col-sm-3"><spring:message code="label.user.password"></spring:message></label>
40-
<span class="col-sm-5"><input class="form-control" name="password" value="" type="password" /></span>
41+
<span class="col-sm-5"><input id="password" class="form-control" name="password" value="" type="password" required/></span>
4142
<span id="passwordError" class="alert alert-danger col-sm-4" style="display:none"></span>
4243
</div>
4344
<div class="form-group row">
4445
<label class="col-sm-3"><spring:message code="label.user.confirmPass"></spring:message></label>
45-
<span class="col-sm-5"><input class="form-control" name="matchingPassword" value="" type="password" /></span>
46+
<span class="col-sm-5"><input id="matchPassword" class="form-control" name="matchingPassword" value="" type="password" required/></span>
4647
<span id="globalError" class="alert alert-danger col-sm-4" style="display:none"></span>
4748
</div>
4849
<br>
49-
<a href="#" class="btn btn-primary" onclick="register()">
50+
<button type="submit" class="btn btn-primary">
5051
<spring:message code="label.form.submit"></spring:message>
51-
</a>
52+
</button>
5253
</form>
5354
<br>
5455
<a href="<c:url value="login.html" />"><spring:message code="label.form.loginLink"></spring:message></a>
5556
</div>
5657
</div>
5758

58-
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
5959
<script type="text/javascript">
6060
function register(){
61+
event.preventDefault();
6162
$(".alert").html("").hide();
63+
if($("#password").val() != $("#matchPassword").val()){
64+
$("#globalError").show().html("Password mismatch");
65+
return;
66+
}
6267
var formData= $('form').serialize();
63-
$.post("/spring-security-login-and-registration/user/registration",formData ,function(data){
68+
$.post("<c:url value="/user/registration"/>",formData ,function(data){
6469
if(data.message == "success"){
6570
window.location.href = "<c:url value="/successRegister.html"></c:url>";
6671
}
67-
else if(data.message == "email"){
68-
$("#emailError").show().html(data.error);
69-
}
70-
else{
71-
72-
var errors = $.parseJSON(data.message);
73-
$.each( errors, function( index,item ){
74-
$("#"+item.field+"Error").show().html(item.defaultMessage);
75-
});
76-
errors = $.parseJSON(data.error);
77-
$.each( errors, function( index,item ){
78-
$("#globalError").show().append(item.defaultMessage+"<br>");
79-
});
80-
}
72+
8173
})
8274
.fail(function(data) {
8375
if(data.responseJSON.error.indexOf("MailError") > -1)
8476
{
8577
window.location.href = "<c:url value="/emailError.html"></c:url>";
8678
}
79+
else if(data.responseJSON.error == "UserAlreadyExist"){
80+
$("#emailError").show().html(data.responseJSON.message);
81+
}
8782
else if(data.responseJSON.error.indexOf("InternalError") > -1){
8883
window.location.href = "<c:url value="/login.html"></c:url>" + "?message=" + data.responseJSON.message;
8984
}

0 commit comments

Comments
 (0)