HtmlExceptionAdvice.java

/*
 * Copyright 2019 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.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.gringlobal.mvc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.genesys.blocks.security.service.PasswordPolicy;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.firewall.RequestRejectedException;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.fasterxml.jackson.core.JsonProcessingException;

import io.swagger.v3.oas.annotations.Hidden;

@ControllerAdvice(basePackages = { "org.gringlobal.mvc" })
@Hidden
@Slf4j
public class HtmlExceptionAdvice extends AbstractMvcController {

	// logs exception and returns it's message
	protected String simpleExceptionHandler(final Throwable th) {
		log.error(th.getMessage(), th);
		return th.getMessage();
	}

	@ResponseStatus(code = HttpStatus.FORBIDDEN)
	@ExceptionHandler(value = { AccessDeniedException.class })
	public ModelAndView handleAccessDeniedException(final AccessDeniedException e) {
		final ModelAndView mav = new ModelAndView("/errors/error");
		mav.addObject("exception", e);
		return mav;
	}

	@ResponseStatus(HttpStatus.NOT_FOUND)
	@ExceptionHandler(value = { NoHandlerFoundException.class })
	public ModelAndView handleResourceNotFoundException(final Exception e) {
		final ModelAndView mav = new ModelAndView("/errors/error");
		mav.addObject("exception", e);
		return mav;
	}

	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	@ExceptionHandler(value = { AuthenticationException.class })
	@ResponseBody
	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) {
		log.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.BAD_REQUEST)
	@ExceptionHandler(value = { RequestRejectedException.class, IllegalArgumentException.class })
	public ModelAndView handleMaxPageLimitException(final Throwable e, final HttpServletRequest request) {
		log.warn("Bad request {} {}: {}", request.getMethod(), request.getRequestURL(), e.getMessage());
		final ModelAndView mav = new ModelAndView("/errors/error");
		mav.addObject("exception", e);
		return mav;
	}

	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(value = { JsonProcessingException.class })
	public ModelAndView handleJsonParseExceptions(final JsonProcessingException e, final HttpServletRequest request) {
		log.warn("JSON processing error {} {}: {}", request.getMethod(), request.getRequestURL(), e.getMessage());
		final ModelAndView mav = new ModelAndView("/errors/error");
		mav.addObject("exception", e);
		return mav;
	}

	/**
	 * Handle password policy mismatch.
	 *
	 * @param e the e
	 * @param request the request
	 * @return the api error
	 */
	@ResponseStatus(code = HttpStatus.BAD_REQUEST)
	@ExceptionHandler(PasswordPolicy.PasswordPolicyException.class)
	public ModelAndView handlePasswordPolicyMismatch(final PasswordPolicy.PasswordPolicyException e, final HttpServletRequest request) {
		log.warn("{} for {} {}", e.getMessage(), request.getMethod(), request.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(final HttpServletRequest req, final Throwable e) {
		log.error("{} on {} {}", e.getMessage(), req.getMethod(), req.getRequestURL(), e);
		final ModelAndView mav = new ModelAndView("/errors/error");
		mav.addObject("exception", e);
		return mav;
	}

	// do not pass validation
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(value = { MethodArgumentNotValidException.class, MethodArgumentTypeMismatchException.class })
	@ResponseBody
	public Object handleMethodArgumentNotValidException(final MethodArgumentNotValidException e, final HttpServletRequest request) {
		log.error("Argument not valid for {} {}: {}", request.getMethod(), request.getRequestURL(), e.getMessage());
		return transformErrors(e.getBindingResult());
	}

	private Map<String, String> transformErrors(final Errors errors) {
		final Map<String, String> errorsMap = new HashMap<>();

		final List<ObjectError> allErrors = errors.getAllErrors();

		// todo probably handle and values
		for (final ObjectError error : allErrors) {
			final String objectName = error instanceof FieldError ? ((FieldError) error).getField() : error.getObjectName();
			errorsMap.put(objectName, error.getDefaultMessage());
		}

		return errorsMap;
	}
}