001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *******************************************************************************/
016
017package org.mitre.oauth2.web;
018
019import org.slf4j.Logger;
020import org.slf4j.LoggerFactory;
021import org.springframework.beans.factory.annotation.Autowired;
022import org.springframework.http.ResponseEntity;
023import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
024import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
025import org.springframework.web.bind.annotation.ControllerAdvice;
026import org.springframework.web.bind.annotation.ExceptionHandler;
027
028/**
029 * Controller helper that handles OAuth2 exceptions and propagates them as JSON errors.
030 *
031 * @author jricher
032 *
033 */
034@ControllerAdvice
035public class OAuth2ExceptionHandler {
036        private static final Logger logger = LoggerFactory.getLogger(OAuth2ExceptionHandler.class);
037
038        @Autowired
039        private WebResponseExceptionTranslator providerExceptionHandler;
040
041        @ExceptionHandler(OAuth2Exception.class)
042        public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
043                logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
044                return providerExceptionHandler.translate(e);
045        }
046
047}