Fix ReCaptha NPE

Improve error handling when permission are removed.
This commit is contained in:
Paulo Gustavo Veiga
2012-09-06 23:52:53 -03:00
parent 743164ade4
commit 337a67a8f6
12 changed files with 84 additions and 24 deletions

View File

@@ -60,7 +60,7 @@ public class AdminController extends BaseController {
@RequestMapping(method = RequestMethod.POST, value = "admin/users", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
@ResponseStatus(value = HttpStatus.CREATED)
public void createUser(@RequestBody RestUser user, HttpServletResponse response) throws IOException, WiseMappingException {
public void createUser(@RequestBody RestUser user, HttpServletResponse response) throws WiseMappingException {
if (user == null) {
throw new IllegalArgumentException("User could not be found");
}
@@ -90,7 +90,7 @@ public class AdminController extends BaseController {
@RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void changePassword(@RequestBody String password, @PathVariable long id) throws IOException, WiseMappingException {
public void changePassword(@RequestBody String password, @PathVariable long id) throws WiseMappingException {
if (password == null) {
throw new IllegalArgumentException("Password can not be null");
}
@@ -105,7 +105,7 @@ public class AdminController extends BaseController {
@RequestMapping(method = RequestMethod.DELETE,value = "admin/users/{id}")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void getUserByEmail(@PathVariable long id) throws IOException, WiseMappingException {
public void getUserByEmail(@PathVariable long id) throws WiseMappingException {
final User user = userService.getUserBy(id);
if (user == null) {
throw new IllegalArgumentException("User '" + id + "' could not be found");

View File

@@ -19,6 +19,7 @@
package com.wisemapping.rest;
import com.wisemapping.exceptions.AccessDeniedSecurityException;
import com.wisemapping.exceptions.ClientException;
import com.wisemapping.filter.UserAgent;
import com.wisemapping.mail.NotificationService;
import com.wisemapping.model.User;
@@ -27,6 +28,7 @@ import com.wisemapping.security.Utils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -36,6 +38,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Locale;
public class BaseController {
@@ -75,13 +78,20 @@ public class BaseController {
@ExceptionHandler(java.lang.reflect.UndeclaredThrowableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestErrors handleSecurityErrors(@NotNull UndeclaredThrowableException ex) {
return new RestErrors(ex.getMessage());
final Throwable cause = ex.getCause();
RestErrors result;
if (cause instanceof ClientException) {
result = handleClientErrors((ClientException) cause);
} else {
result = new RestErrors(ex.getMessage());
}
return result;
}
@ExceptionHandler(com.wisemapping.exceptions.AccessDeniedSecurityException.class)
@ExceptionHandler(ClientException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestErrors handleSecurityException(@NotNull AccessDeniedSecurityException ex) {
return new RestErrors(ex.getMessage());
public RestErrors handleClientErrors(@NotNull ClientException ex) {
final Locale locale = LocaleContextHolder.getLocale();
return new RestErrors(ex.getMessage(messageSource, locale));
}
}

View File

@@ -30,7 +30,7 @@ public class RestErrors {
private Errors errors;
@JsonIgnore
private List<String> globalErrors;
private List<String> gErrors;
@JsonIgnore
MessageSource messageSource;
@@ -43,12 +43,12 @@ public class RestErrors {
this.errors = errors;
this.messageSource = messageSource;
this.globalErrors = this.processGlobalErrors(errors, messageSource);
this.gErrors = this.processGlobalErrors(errors, messageSource);
}
public RestErrors(@NotNull String errorMsg) {
globalErrors = new ArrayList<String>();
globalErrors.add(errorMsg);
gErrors = new ArrayList<String>();
gErrors.add(errorMsg);
}
private List<String> processGlobalErrors(@NotNull Errors errors, @NotNull MessageSource messageSource) {
@@ -61,7 +61,7 @@ public class RestErrors {
}
public List<String> getGlobalErrors() {
return globalErrors;
return gErrors;
}
public void setGlobalErrors(List<String> list) {