2012-04-07 12:45:35 -03:00
|
|
|
/*
|
|
|
|
* Copyright [2011] [wisemapping]
|
|
|
|
*
|
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2012-02-21 20:04:17 -03:00
|
|
|
|
2012-04-07 12:45:35 -03:00
|
|
|
package com.wisemapping.rest;
|
2012-02-21 20:04:17 -03:00
|
|
|
|
2012-08-14 23:55:13 -03:00
|
|
|
import com.wisemapping.mail.NotificationService;
|
|
|
|
import com.wisemapping.model.User;
|
2012-04-07 12:45:35 -03:00
|
|
|
import com.wisemapping.rest.model.RestErrors;
|
2012-08-14 23:55:13 -03:00
|
|
|
import com.wisemapping.security.Utils;
|
2012-02-25 11:56:20 -03:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2012-04-07 12:45:35 -03:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2012-06-17 21:43:34 -03:00
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
2012-04-07 12:45:35 -03:00
|
|
|
import org.springframework.context.support.ResourceBundleMessageSource;
|
2012-02-21 20:04:17 -03:00
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
|
|
|
|
|
public class BaseController {
|
|
|
|
|
2012-06-17 21:43:34 -03:00
|
|
|
@Qualifier("messageSource")
|
2012-04-07 12:45:35 -03:00
|
|
|
@Autowired
|
|
|
|
private ResourceBundleMessageSource messageSource;
|
|
|
|
|
2012-08-14 23:55:13 -03:00
|
|
|
@Autowired
|
|
|
|
private NotificationService notificationService;
|
|
|
|
|
2012-02-21 20:04:17 -03:00
|
|
|
@ExceptionHandler(IllegalArgumentException.class)
|
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
@ResponseBody
|
2012-06-07 19:45:22 -03:00
|
|
|
public RestErrors handleClientErrors(@NotNull IllegalArgumentException ex) {
|
2012-06-17 21:43:34 -03:00
|
|
|
ex.printStackTrace();
|
2012-06-07 19:45:22 -03:00
|
|
|
return new RestErrors(ex.getMessage());
|
2012-02-21 20:04:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
@ResponseBody
|
2012-02-25 11:56:20 -03:00
|
|
|
public String handleServerErrors(@NotNull Exception ex) {
|
2012-08-14 23:55:13 -03:00
|
|
|
final User user = Utils.getUser();
|
|
|
|
notificationService.reportUnexpectedError(ex, user, "unknown browser");
|
2012-02-21 20:04:17 -03:00
|
|
|
return ex.getMessage();
|
|
|
|
}
|
2012-04-07 12:45:35 -03:00
|
|
|
|
|
|
|
@ExceptionHandler(ValidationException.class)
|
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
public RestErrors handleValidationErrors(@NotNull ValidationException ex) {
|
2012-08-14 23:55:13 -03:00
|
|
|
return new RestErrors(ex.getErrors(), messageSource);
|
2012-04-07 12:45:35 -03:00
|
|
|
}
|
|
|
|
|
2012-08-26 17:12:25 -03:00
|
|
|
@ExceptionHandler(java.lang.reflect.UndeclaredThrowableException.class)
|
2012-08-26 17:02:24 -03:00
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
public RestErrors handleSecurityErrors(@NotNull ValidationException ex) {
|
|
|
|
return new RestErrors(ex.getErrors(), messageSource);
|
|
|
|
}
|
2012-04-07 12:45:35 -03:00
|
|
|
|
2012-08-26 17:12:25 -03:00
|
|
|
@ExceptionHandler(com.wisemapping.exceptions.AccessDeniedSecurityException.class)
|
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
public RestErrors handleSecurity2Errors(@NotNull ValidationException ex) {
|
|
|
|
return new RestErrors(ex.getErrors(), messageSource);
|
|
|
|
}
|
|
|
|
|
2012-02-21 20:04:17 -03:00
|
|
|
}
|