All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.formkiq.server.api.ErrorHandlingController Maven / Gradle / Ivy

/*
 * Copyright (C) 2016 FormKiQ Inc.
 *
 * 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 com.formkiq.server.api;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.http.HttpStatus;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.MissingServletRequestParameterException;
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.bind.annotation.RestController;

import com.formkiq.server.service.AuthenticationFailureException;
import com.formkiq.server.service.FormAccessDeniedException;
import com.formkiq.server.service.FormNotFoundException;
import com.formkiq.server.service.InvalidEmailException;
import com.formkiq.server.service.InvalidJSONException;
import com.formkiq.server.service.InvalidRequestBodyException;
import com.formkiq.server.service.PreconditionFailedException;
import com.formkiq.server.service.SaveActionException;

import javassist.bytecode.stackmap.TypeData.ClassName;

/**
 * Error Handling Controller.
 *
 */
@ControllerAdvice(annotations = RestController.class)
public class ErrorHandlingController {

    /** Logger. */
    private static final Logger LOG = Logger.getLogger(ClassName.class
            .getName());

    /**
     * AuthenticationFailureException handler.
     * @param ex AuthenticationFailureException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(AuthenticationFailureException.class)
    @ResponseBody
    public static ApiMessageResponse handleAuthenticationFailureException(
            final AuthenticationFailureException ex) {
        LOG.log(Level.INFO, ex.getMessage(), ex);
        return new ApiMessageResponse(ex.getMessage());
    }

    /**
     * BadCredentialsException handler.
     * @param e {@link BadCredentialsException}
     * @return ApiStringResponse
     */
    @ExceptionHandler(BadCredentialsException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public static ApiMessageResponse handleBadCredentialsException(
            final BadCredentialsException e) {
        LOG.log(Level.INFO, e.getMessage(), e);
        return new ApiMessageResponse(e.getMessage());
    }

    /**
     * ClientRegistrationException handler.
     * @param ex ClientRegistrationException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ClientRegistrationException.class)
    @ResponseBody
    public static ApiMessageResponse handleClientRegistrationException(
            final ClientRegistrationException ex) {
        LOG.log(Level.WARNING, ex.getMessage(), ex);
        return new ApiMessageResponse(ex.getMessage());
    }

    /**
     * FormAccessDeniedException handler.
     * @return ApiStringResponse
     */
    @ExceptionHandler(FormAccessDeniedException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public static ApiMessageResponse handleFormAccessDeniedException() {
        return new ApiMessageResponse("Permission Denied");
    }

    /**
     * FormNotFoundException handler.
     * @param ex FormNotFoundException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(FormNotFoundException.class)
    @ResponseBody
    public static ApiMessageResponse handleFormNotFoundException(
            final FormNotFoundException ex) {
        return new ApiMessageResponse(ex.getMessage());
    }

    /**
     * InvalidDataAccessApiUsageException handler.
     * @param ex {@link InvalidDataAccessApiUsageException}
     * @return {@link ApiMessageResponse}
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(InvalidDataAccessApiUsageException.class)
    @ResponseBody
    public static ApiMessageResponse handleInvalidDataAccessApiUsageException(
            final InvalidDataAccessApiUsageException ex) {
        LOG.log(Level.WARNING, ex.getMessage(), ex);
        return new ApiMessageResponse("Invalid Request");
    }

    /**
     * InvalidEmailException handler.
     * @param ex InvalidEmailException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(InvalidEmailException.class)
    @ResponseBody
    public static ApiMessageResponse handleInvalidEmailException(
            final InvalidEmailException ex) {
        return new ApiMessageResponse(
                "invalid email address '"
                + ex.getInvalidEmail() + "'");
    }

    /**
     * InvalidJSONException handler.
     * @param ex InvalidJSONException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(InvalidJSONException.class)
    @ResponseBody
    public static ApiMessageResponse handleInvalidJSONException(
            final InvalidJSONException ex) {
        LOG.log(Level.INFO, ex.getMessage(), ex);
        return new ApiMessageResponse("invalid json");
    }

    /**
     * AuthenticationFailureException handler.
     * @param ex {@link InvalidMediaTypeException}
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(InvalidMediaTypeException.class)
    @ResponseBody
    public static ApiMessageResponse handleInvalidMediaTypeException(
            final InvalidMediaTypeException ex) {
        LOG.log(Level.INFO, ex.getMessage(), ex);
        return new ApiMessageResponse(ex.getMessage());
    }

    /**
     * InvalidRequestBodyException handler.
     * @param ex InvalidRequestBodyException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(InvalidRequestBodyException.class)
    @ResponseBody
    public static ApiMessageResponse handleInvalidRequestBodyException(
            final InvalidRequestBodyException ex) {
        LOG.log(Level.INFO, ex.getMessage(), ex);
        return new ApiMessageResponse(ex.getMessage());
    }

    /**
     * NoSuchClientException handler.
     * @param ex NoSuchClientException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(NoSuchClientException.class)
    @ResponseBody
    public static ApiMessageResponse handleNoSuchClientException(
            final NoSuchClientException ex) {
        LOG.log(Level.INFO, ex.getMessage(), ex);
        return new ApiMessageResponse(ex.getMessage());
    }

    /** MailSender. */
    @Autowired
    private MailSender mailSender;

    /**
     * Handler for Access Denied Exception.
     * @param ex AccessDeniedException
     * @return ApiMessageResponse
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(AccessDeniedException.class)
    public ApiMessageResponse handleAccessDenied(
            final AccessDeniedException ex) {
        LOG.log(Level.INFO, ex.getMessage(), ex);
        return new ApiMessageResponse(ex.getMessage());
    }

    /**
     * Exception handler.
     * @param ex Exception
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiMessageResponse handleExceptions(final Exception ex) {

        LOG.log(Level.SEVERE, ex.getMessage(), ex);

        try {

            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            ex.printStackTrace(pw);

            SimpleMailMessage msg = new SimpleMailMessage();
            msg.setSubject("Exception in FormKiQ");
            msg.setText(sw.toString());

            this.mailSender.send(msg);

        } catch (Exception e) {

            LOG.log(Level.SEVERE, e.getMessage(), e);
        }

        return new ApiMessageResponse(
                "unable to process request");
    }

    /**
     * Handle handleIllegalArgumentException.
     * @param ex IllegalArgumentException
     * @return ApiMessageResponse
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public ApiMessageResponse handleIllegalArgumentException(
            final IllegalArgumentException ex) {
        String msg = ex.getMessage();

        if (!StringUtils.isEmpty(msg) && msg.startsWith("No enum constant ")) {
            msg = msg.substring(msg.lastIndexOf(".") + 1);
        }

        LOG.log(Level.INFO, ex.getMessage(), ex);
        return new ApiMessageResponse(msg);
    }

    /**
     * MissingServletRequestParameterException handler.
     *
     * @param ex MissingServletRequestParameterException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public ApiMessageResponse handleMissingServletRequestParameterException(
            final MissingServletRequestParameterException ex) {
        LOG.log(Level.FINE, ex.getMessage(), ex);
        return new ApiMessageResponse("'" + ex.getParameterName()
                + "' parameter is required");
    }

    /**
     * PreconditionFailedException handler.
     *
     * @param ex PreconditionFailedException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
    @ExceptionHandler(PreconditionFailedException.class)
    @ResponseBody
    public ApiMessageResponse handlePreconditionFailedException(
            final PreconditionFailedException ex) {
        LOG.log(Level.FINE, ex.getMessage(), ex);
        return new ApiMessageResponse(ex.getMessage());
    }

    /**
     * SaveActionException handler.
     *
     * @param ex SaveActionException
     * @return ApiStringResponse
     */
    @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
    @ExceptionHandler(SaveActionException.class)
    @ResponseBody
    public ApiMessageResponse handleSaveActionException(
            final SaveActionException ex) {
        LOG.log(Level.INFO, ex.getMessage(), ex);
        return new ApiMessageResponse(ex.getMessage());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy