/* 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.leanwo.activiti.app.config.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
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 com.leanwo.activiti.exception.AuthorizeException;
import com.leanwo.activiti.exception.BeanNotInjectException;
import com.leanwo.activiti.exception.ExceptionUtil;
import com.leanwo.activiti.exception.TokenException;

/**
 * From http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
 * 
 * @author Joram Barrez
 */
@ControllerAdvice
public class RestExceptionHandlerAdvice {

	private static Logger logger = LoggerFactory.getLogger(RestExceptionHandlerAdvice.class);
	
	private static final String UNAUTHORIZED_MESSAGE_KEY = "GENERAL.ERROR.UNAUTHORIZED";
	private static final String NOT_FOUND_MESSAGE_KEY = "GENERAL.ERROR.NOT-FOUND";
	private static final String BAD_REQUEST_MESSAGE_KEY = "GENERAL.ERROR.BAD-REQUEST";
	private static final String INTERNAL_SERVER_ERROR_MESSAGE_KEY = "GENERAL.ERROR.INTERNAL-SERVER_ERROR";
	private static final String FORBIDDEN_MESSAGE_KEY = "GENERAL.ERROR.FORBIDDEN";
	private static final String INACTIVE_USER_MESSAGE_KEY = "GENERAL.ERROR.INACTIVE_USER";
	private static final String UPLOAD_LIMIT_EXCEEDED = "GENERAL.ERROR.UPLOAD-LIMIT-EXCEEDED";
	private static final String UPLOAD_LIMIT_EXCEEDED_TRIAL_USER = "GENERAL.ERROR.UPLOAD-LIMIT-EXCEEDED-TRIAL-USER";
	private static final String QUOTA_EXCEEDED_PREFIX = "GENERAL.ERROR.QUOTA-EXCEEDED-";


	@ExceptionHandler(value = { AuthorizeException.class, TokenException.class })
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	@ResponseBody
	public Object AuthorizeExceptionHandler(HttpServletRequest req, Exception exception) throws Exception {
		logger.error("无权限访问服务器。", exception);
		return ExceptionUtil.getMessage("无权限访问服务器。", exception);
	}

	@ExceptionHandler(value = BeanNotInjectException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	public Object BeanNotInjectExceptionHandler(HttpServletRequest req, Exception exception) throws Exception {
		logger.error("Bean未注入。", exception);
		return ExceptionUtil.getMessage("Bean未注入。", exception);
	}

	@ExceptionHandler(value = Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	public Object ExceptionHandler(HttpServletRequest req, Exception exception) throws Exception {
		logger.error("", exception);
		return ExceptionUtil.getMessage("服务器异常。", exception);
	}


	protected String getSafeMessageKey(String fragment) {
		if (fragment != null) {
			return fragment.toUpperCase();
		}
		return "";
	}

}
