org.opentestsystem.ap.ivs.rest.ExceptionAdvice.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.ap.ivs.rest.ExceptionAdvice.java

Source

/*
 * Copyright 2017 Regents of the University of California. Licensed under the Educational Community 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
 *
 * https://opensource.org/licenses/ECL-2.0
 *
 * Unless required under applicable law or agreed to in writing, software distributed under the License is distributed
 * in an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
 * specific language governing permissions and limitations under the license.
 */
package org.opentestsystem.ap.ivs.rest;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.opentestsystem.ap.common.exception.AcquireLockException;
import org.opentestsystem.ap.common.exception.ResourceNotFoundException;
import org.opentestsystem.ap.common.exception.UnauthorizedException;
import org.opentestsystem.ap.common.exception.ValidationException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import static com.google.common.collect.Lists.newArrayList;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
import static org.springframework.http.MediaType.APPLICATION_JSON;

/**
 * This class is responsible for intercepting exceptions and returning HTTP responses.
 * <p>
 * See http://www.baeldung.com/global-error-handler-in-a-spring-rest-api
 * </p>
 */
@Slf4j
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ExceptionAdvice extends ResponseEntityExceptionHandler {

    @ExceptionHandler(AcquireLockException.class)
    public ResponseEntity<List<ErrorMessage>> handleLockError(final AcquireLockException ex) {
        log.warn("Lock error: {}", ex.getMessage());
        return generateResponse(ex.getMessage(), CONFLICT);
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<List<ErrorMessage>> handleResourceNotFound(final ResourceNotFoundException ex) {
        log.warn("Resource not found: {}", ex.getMessage());
        return generateResponse(ex.getMessage(), NOT_FOUND);
    }

    @ExceptionHandler(ValidationException.class)
    public ResponseEntity<List<ErrorMessage>> handleBadRequest(final ValidationException ex) {
        log.warn("Validation error:", ex.getMessage());
        return generateResponse(ex.getMessage(), BAD_REQUEST);
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<List<ErrorMessage>> handleInternalServerError(final RuntimeException ex) {
        log.error("Internal server error", ex);
        return generateResponse(ex.getMessage(), INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(UnauthorizedException.class)
    public ResponseEntity<List<ErrorMessage>> handleUnauthorizedError(final RuntimeException ex) {
        log.error("User Unauthorized", ex);
        return generateResponse(ex.getMessage(), UNAUTHORIZED);
    }

    private ResponseEntity<List<ErrorMessage>> generateResponse(final String message, final HttpStatus status) {
        final ErrorMessage errorMessage = new ErrorMessage(message);
        return new ResponseEntity<>(newArrayList(errorMessage), jsonHeaders(), status);
    }

    private HttpHeaders jsonHeaders() {
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(APPLICATION_JSON);
        return headers;
    }
}