wisemapping-open-source/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java

116 lines
4.4 KiB
Java
Raw Normal View History

/*
* 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.
*/
package com.wisemapping.rest;
import com.wisemapping.exceptions.ClientException;
import com.wisemapping.exceptions.ImportUnexpectedException;
import com.wisemapping.mail.NotificationService;
import com.wisemapping.model.User;
import com.wisemapping.rest.model.RestErrors;
import com.wisemapping.security.Utils;
2012-09-30 22:15:01 +02:00
import org.apache.log4j.Logger;
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;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
2012-08-28 06:34:15 +02:00
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
2012-08-31 06:00:30 +02:00
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Locale;
public class BaseController {
2012-09-30 22:15:01 +02:00
final protected static Logger logger = Logger.getLogger("com.wisemapping.rest");
@Qualifier("messageSource")
@Autowired
private ResourceBundleMessageSource messageSource;
2012-08-28 06:34:15 +02:00
@Autowired
ServletContext context;
@Autowired
private NotificationService notificationService;
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestErrors handleClientErrors(@NotNull IllegalArgumentException ex) {
ex.printStackTrace();
return new RestErrors(ex.getMessage());
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
2012-08-28 06:34:15 +02:00
public String handleServerErrors(@NotNull Exception ex, @NotNull HttpServletRequest request) {
final User user = Utils.getUser();
2012-08-31 02:23:10 +02:00
notificationService.reportJavaException(ex, user, request);
return ex.getMessage();
}
2012-09-29 20:42:41 +02:00
@ExceptionHandler(ImportUnexpectedException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public String handleImportErrors(@NotNull ImportUnexpectedException ex, @NotNull HttpServletRequest request) {
final User user = Utils.getUser();
notificationService.reportJavaException(ex, user, new String(ex.getFreemindXml()), request);
return ex.getMessage();
}
2012-09-11 19:02:06 +02:00
@ExceptionHandler(ValidationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestErrors handleValidationErrors(@NotNull ValidationException ex) {
return new RestErrors(ex.getErrors(), messageSource);
}
2012-09-11 04:51:53 +02:00
@ExceptionHandler(JsonHttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
2012-09-11 19:02:06 +02:00
public String handleJSONErrors(@NotNull JsonHttpMessageNotReadableException ex) {
2012-09-11 04:51:53 +02:00
return "Request could not be saved. Message is not valid";
}
@ExceptionHandler(java.lang.reflect.UndeclaredThrowableException.class)
2012-08-26 22:02:24 +02:00
@ResponseStatus(HttpStatus.BAD_REQUEST)
2012-08-31 06:00:30 +02:00
public RestErrors handleSecurityErrors(@NotNull UndeclaredThrowableException ex) {
final Throwable cause = ex.getCause();
RestErrors result;
if (cause instanceof ClientException) {
result = handleClientErrors((ClientException) cause);
} else {
result = new RestErrors(ex.getMessage());
}
return result;
2012-08-26 22:02:24 +02:00
}
@ExceptionHandler(ClientException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestErrors handleClientErrors(@NotNull ClientException ex) {
final Locale locale = LocaleContextHolder.getLocale();
return new RestErrors(ex.getMessage(messageSource, locale));
}
}