Merge pull request #174 from Doha2012/master

handle 409 status code
This commit is contained in:
Eugen 2015-03-25 23:41:03 +02:00
commit d6f24fc384
5 changed files with 57 additions and 39 deletions

View File

@ -63,26 +63,17 @@ function register(){
if(data.message == "success"){
window.location.href = "<c:url value="/successRegister.html"></c:url>";
}
else if(data.message == "email"){
$("#emailError").show().html(data.error);
}
else{
var errors = $.parseJSON(data.message);
$.each( errors, function( index,item ){
$("#"+item.field+"Error").show().html(item.defaultMessage);
});
errors = $.parseJSON(data.error);
$.each( errors, function( index,item ){
$("#globalError").show().append(item.defaultMessage+"<br>");
});
}
})
.fail(function(data) {
if(data.responseJSON.error.indexOf("MailError") > -1)
{
window.location.href = "<c:url value="/emailError.html"></c:url>";
}
else if(data.responseJSON.error == "UserAlreadyExist"){
$("#emailError").show().html(data.responseJSON.message);
}
else if(data.responseJSON.error.indexOf("InternalError") > -1){
window.location.href = "<c:url value="/login.html"></c:url>" + "?message=" + data.responseJSON.message;
}

View File

@ -14,6 +14,7 @@ import org.baeldung.persistence.service.IUserService;
import org.baeldung.persistence.service.UserDto;
import org.baeldung.registration.OnRegistrationCompleteEvent;
import org.baeldung.validation.EmailExistsException;
import org.baeldung.web.error.UserAlreadyExistException;
import org.baeldung.web.error.UserNotFoundException;
import org.baeldung.web.util.GenericResponse;
import org.slf4j.Logger;
@ -71,7 +72,7 @@ public class RegistrationController {
final User registered = createUserAccount(accountDto);
if (registered == null) {
return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale()));
throw new UserAlreadyExistException();
}
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));

View File

@ -52,6 +52,14 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
}
// 409
@ExceptionHandler({ UserAlreadyExistException.class })
public ResponseEntity<Object> handleUserAlreadyExist(final RuntimeException ex, final WebRequest request) {
logger.error("409 Status Code", ex);
final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("message.regError", null, request.getLocale()), "UserAlreadyExist");
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
}
// 500
@ExceptionHandler({ MailAuthenticationException.class })
public ResponseEntity<Object> handleMail(final RuntimeException ex, final WebRequest request) {

View File

@ -0,0 +1,23 @@
package org.baeldung.web.error;
public final class UserAlreadyExistException extends RuntimeException {
private static final long serialVersionUID = 5861310537366287163L;
public UserAlreadyExistException() {
super();
}
public UserAlreadyExistException(final String message, final Throwable cause) {
super(message, cause);
}
public UserAlreadyExistException(final String message) {
super(message);
}
public UserAlreadyExistException(final Throwable cause) {
super(cause);
}
}

View File

@ -6,6 +6,7 @@
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title><spring:message code="label.form.title"></spring:message></title>
</head>
@ -16,74 +17,68 @@
<spring:message code="label.form.title"></spring:message>
</h1>
<br>
<form action="/" method="POST" enctype="utf8">
<form action="/" method="POST" enctype="utf8" onsubmit="register()">
<div class="form-group row" >
<label class="col-sm-3"><spring:message code="label.user.firstName"></spring:message></label>
<span class="col-sm-5"><input class="form-control" name="firstName" value="" /></span>
<span class="col-sm-5"><input class="form-control" name="firstName" value="" required/></span>
<span id="firstNameError" class="alert alert-danger col-sm-4" style="display:none"></span>
</div>
<div class="form-group row">
<label class="col-sm-3"><spring:message code="label.user.lastName"></spring:message></label>
<span class="col-sm-5"><input class="form-control" name="lastName" value="" /></span>
<span class="col-sm-5"><input class="form-control" name="lastName" value="" required/></span>
<span id="lastNameError" class="alert alert-danger col-sm-4" style="display:none"></span>
</div>
<div class="form-group row">
<label class="col-sm-3"><spring:message code="label.user.email"></spring:message></label>
<span class="col-sm-5"><input class="form-control" name="email" value="" /></span>
<span class="col-sm-5"><input type="email" class="form-control" name="email" value="" required/></span>
<span id="emailError" class="alert alert-danger col-sm-4" style="display:none"></span>
</div>
<div class="form-group row">
<label class="col-sm-3"><spring:message code="label.user.password"></spring:message></label>
<span class="col-sm-5"><input class="form-control" name="password" value="" type="password" /></span>
<span class="col-sm-5"><input id="password" class="form-control" name="password" value="" type="password" required/></span>
<span id="passwordError" class="alert alert-danger col-sm-4" style="display:none"></span>
</div>
<div class="form-group row">
<label class="col-sm-3"><spring:message code="label.user.confirmPass"></spring:message></label>
<span class="col-sm-5"><input class="form-control" name="matchingPassword" value="" type="password" /></span>
<span class="col-sm-5"><input id="matchPassword" class="form-control" name="matchingPassword" value="" type="password" required/></span>
<span id="globalError" class="alert alert-danger col-sm-4" style="display:none"></span>
</div>
<br>
<a href="#" class="btn btn-primary" onclick="register()">
<button type="submit" class="btn btn-primary">
<spring:message code="label.form.submit"></spring:message>
</a>
</button>
</form>
<br>
<a href="<c:url value="login.html" />"><spring:message code="label.form.loginLink"></spring:message></a>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
function register(){
event.preventDefault();
$(".alert").html("").hide();
if($("#password").val() != $("#matchPassword").val()){
$("#globalError").show().html("Password mismatch");
return;
}
var formData= $('form').serialize();
$.post("/spring-security-login-and-registration/user/registration",formData ,function(data){
$.post("<c:url value="/user/registration"/>",formData ,function(data){
if(data.message == "success"){
window.location.href = "<c:url value="/successRegister.html"></c:url>";
}
else if(data.message == "email"){
$("#emailError").show().html(data.error);
}
else{
var errors = $.parseJSON(data.message);
$.each( errors, function( index,item ){
$("#"+item.field+"Error").show().html(item.defaultMessage);
});
errors = $.parseJSON(data.error);
$.each( errors, function( index,item ){
$("#globalError").show().append(item.defaultMessage+"<br>");
});
}
})
.fail(function(data) {
if(data.responseJSON.error.indexOf("MailError") > -1)
{
window.location.href = "<c:url value="/emailError.html"></c:url>";
}
else if(data.responseJSON.error == "UserAlreadyExist"){
$("#emailError").show().html(data.responseJSON.message);
}
else if(data.responseJSON.error.indexOf("InternalError") > -1){
window.location.href = "<c:url value="/login.html"></c:url>" + "?message=" + data.responseJSON.message;
}