Commit 37c086c3 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Revised MVC logging

parent 6b148a16
......@@ -20,8 +20,8 @@ import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
......@@ -33,7 +33,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
public abstract class BaseController {
protected Log _logger = LogFactory.getLog(getClass());
protected Logger _logger = LoggerFactory.getLogger(getClass());
protected static final String ANONYMOUS_USER = "anonymousUser";
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -16,10 +16,13 @@
package org.genesys2.server.servlet.controller;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
......@@ -36,14 +39,12 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class UserControllerAdvice extends BaseController {
@ResponseStatus(code = HttpStatus.FORBIDDEN)
@ExceptionHandler(value = { AccessDeniedException.class })
public ModelAndView handleAccessDeniedException(AccessDeniedException e) {
public ModelAndView handleAccessDeniedException(final AccessDeniedException e) {
final ModelAndView mav = new ModelAndView("/errors/error");
mav.addObject("exception", e);
return mav;
......@@ -51,7 +52,7 @@ public class UserControllerAdvice extends BaseController {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(value = { ResourceNotFoundException.class, NoHandlerFoundException.class })
public ModelAndView handleResourceNotFoundException(ResourceNotFoundException e) {
public ModelAndView handleResourceNotFoundException(final ResourceNotFoundException e) {
final ModelAndView mav = new ModelAndView("/errors/error");
mav.addObject("exception", e);
return mav;
......@@ -60,18 +61,23 @@ public class UserControllerAdvice extends BaseController {
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(value = { AuthenticationException.class })
@ResponseBody
public String handleAuthenticationException(AuthenticationException e) {
public String handleAuthenticationException(final AuthenticationException e) {
return simpleExceptionHandler(e);
}
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(value = { HttpRequestMethodNotSupportedException.class })
public ModelAndView handleRequestMethodFail(final HttpServletRequest req, final HttpRequestMethodNotSupportedException e) {
_logger.error("Request method {} not supported for URL {}", e.getMethod(), req.getRequestURL());
final ModelAndView mav = new ModelAndView("/errors/error");
mav.addObject("exception", e);
return mav;
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = { Throwable.class })
public ModelAndView handleAll(HttpServletRequest req, Throwable e) {
if (e instanceof HttpRequestMethodNotSupportedException) {
_logger.error("Request method '" + ((HttpRequestMethodNotSupportedException) e).getMethod() + "' not supported for URL " + req.getRequestURL());
} else {
_logger.error(e.getMessage(), e);
}
public ModelAndView handleAll(final HttpServletRequest req, final Throwable e) {
_logger.error(MessageFormat.format("{} on {} {}", e.getMessage(), req.getMethod(), req.getRequestURL()), e);
final ModelAndView mav = new ModelAndView("/errors/error");
mav.addObject("exception", e);
return mav;
......@@ -81,12 +87,12 @@ public class UserControllerAdvice extends BaseController {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = { MethodArgumentNotValidException.class })
@ResponseBody
public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
public Object handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
return transformErrors(e.getBindingResult());
}
private Map<String, String> transformErrors(Errors errors) {
final Map<String, String> errorsMap = new HashMap<String, String>();
private Map<String, String> transformErrors(final Errors errors) {
final Map<String, String> errorsMap = new HashMap<>();
final List<ObjectError> allErrors = errors.getAllErrors();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment