it.reply.orchestrator.exception.GlobalControllerExceptionHandler.java Source code

Java tutorial

Introduction

Here is the source code for it.reply.orchestrator.exception.GlobalControllerExceptionHandler.java

Source

package it.reply.orchestrator.exception;

/*
 * Copyright  2015-2017 Santer Reply S.p.A.
 *
 * 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.
 */
import it.reply.orchestrator.dto.common.Error;
import it.reply.orchestrator.exception.http.BadRequestException;
import it.reply.orchestrator.exception.http.ConflictException;
import it.reply.orchestrator.exception.http.NotFoundException;
import it.reply.orchestrator.exception.service.ToscaException;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
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.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * Provide a centralized exception handling
 * 
 * @author m.bassi
 *
 */
@ControllerAdvice
public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {

    /**
     * Not Found exception handler.
     * 
     * @param ex
     *          the exception
     * @return a {@code ResponseEntity} instance
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    public Error handleException(NotFoundException ex) {

        return new Error().withCode(HttpStatus.NOT_FOUND.value()).withTitle(HttpStatus.NOT_FOUND.getReasonPhrase())
                .withMessage(ex.getMessage());
    }

    /**
     * Conflict exception handler.
     * 
     * @param ex
     *          the exception
     * @return a {@code ResponseEntity} instance
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.CONFLICT)
    @ResponseBody
    public Error handleException(ConflictException ex) {

        return new Error().withCode(HttpStatus.CONFLICT.value()).withTitle(HttpStatus.CONFLICT.getReasonPhrase())
                .withMessage(ex.getMessage());
    }

    /**
     * Bad Request exception handler.
     * 
     * @param ex
     *          the exception
     * @return a {@code ResponseEntity} instance
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Error handleException(BadRequestException ex) {

        return new Error().withCode(HttpStatus.BAD_REQUEST.value())
                .withTitle(HttpStatus.BAD_REQUEST.getReasonPhrase()).withMessage(ex.getMessage());
    }

    /**
     * Invalid TOSCA exception handler.
     * 
     * @param ex
     *          the exception
     * @return a {@code ResponseEntity} instance
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Error handleException(ToscaException ex) {

        return new Error().withCode(HttpStatus.BAD_REQUEST.value())
                .withTitle(HttpStatus.BAD_REQUEST.getReasonPhrase()).withMessage(ex.getMessage());
    }

    /**
     * Server Error exception handler.
     * 
     * @param ex
     *          the exception
     * @return a {@code ResponseEntity} instance
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    Error handleException(Exception ex) {

        return new Error().withCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
                .withTitle(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()).withMessage(ex.getMessage());
    }

    @Override
    protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {

        return handleResponse(ex, headers, status);
    }

    /**
     * METHOD_NOT_ALLOWED exception handler.
     * 
     * @param ex
     *          the exception
     * @return a {@code ResponseEntity} instance
     */
    @Override
    protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {

        return handleResponse(ex, headers, status);
    }

    /**
     * UNSUPPORTED_MEDIA_TYPE exception handler.
     * 
     * @param ex
     *          {@code HttpMediaTypeNotSupportedException}
     * @return a {@code ResponseEntity} instance
     */
    @Override
    protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {

        return handleResponse(ex, headers, status);
    }

    /**
     * Customize the response when there are not handler.
     * 
     * @param ex
     *          the exception
     * @param headers
     *          {@code HttpHeaders} instance
     * @param status
     *          {@code HttpStatus} instance
     * @param request
     *          {@code WebRequest} instance
     * @return a {@code ResponseEntity} instance
     */
    @Override
    protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers,
            HttpStatus status, WebRequest request) {

        return handleResponse(ex, headers, status);
    }

    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {
        return handleResponse(ex, headers, status);
    }

    /**
     * Convert the exception into {@link Error} object.
     * 
     * @param ex
     *          the exception to be handled
     * @param headers
     *          {@code HttpHeaders} instance
     * @param status
     *          {@code HttpStatus} instance
     * @return the error response
     */
    private ResponseEntity<Object> handleResponse(Exception ex, HttpHeaders headers, HttpStatus status) {
        Error error = new Error().withCode(status.value()).withTitle(status.getReasonPhrase())
                .withMessage(ex.getMessage());

        return new ResponseEntity<Object>(error, headers, status);

    }
}