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

com.addplus.server.action.config.system.AddControllerAdvice Maven / Gradle / Ivy

The newest version!
package com.addplus.server.action.config.system;

import com.addplus.server.action.config.constant.ConfigConsts;
import com.addplus.server.core.exception.ErrorCodeBase;
import com.addplus.server.core.exception.ErrorException;
import com.addplus.server.core.model.base.ReturnDataSet;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.UndeclaredThrowableException;
import java.sql.Date;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.StringJoiner;


/**
 * Created by 特大碗拉面 on 2017/10/23 0023.
 */
@RestControllerAdvice
@Slf4j
public class AddControllerAdvice {

    @Value("${action.param.encrypted:false}")
    private Boolean encrypted;

    @Autowired
    private AddplusContainer addplusContainer;

    /**
     * 应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Object errorHandler(Exception ex, HttpServletRequest request) {
        ReturnDataSet returnDataSet = new ReturnDataSet();
        if (UndeclaredThrowableException.class.equals(ex.getClass())) {
            // 当前反射类没有进行throws
            Throwable throwable = ((UndeclaredThrowableException) ex).getUndeclaredThrowable();
            if (ErrorException.class.equals(throwable.getClass())) {
                ErrorException errorException = (ErrorException) throwable;
                returnDataSet.setErrorCode(errorException.getEnumErrorMessage());
                if (errorException.getReturnInfo() != null) {
                    returnDataSet.setDataSet(errorException.getReturnInfo());
                }
                return this.encryptAESContent(returnDataSet, request);
            }
        }
        String errorMessage = getErrorMessage(request.getRequestURI(), ex.getMessage());
        log.error("请求接口出错,请求URI:[{}],出错原因:[{}]", request.getRequestURI(), ex.getMessage(), ex);
        returnDataSet.setErrorCode(ErrorCodeBase.SYSTEM_ERROR);
        returnDataSet.setErrorInfo(errorMessage);
        return this.encryptAESContent(returnDataSet, request);
    }


    /**
     * 拦截捕捉自定义异常 ErrorException.class
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(ErrorException.class)
    public Object myErrorHandler(ErrorException ex, HttpServletRequest request) throws Exception {
        ReturnDataSet returnDataSet = new ReturnDataSet();
        returnDataSet.setErrorCode(ex.getEnumErrorMessage());
        if (ex.getReturnInfo() != null) {
            returnDataSet.setDataSet(ex.getReturnInfo());
        }
        return this.encryptAESContent(returnDataSet, request);
    }

    private String getErrorMessage(String... args) {
        String error = MessageFormat.format(ConfigConsts.ERROR_MESSAGE_MODEL, args);
        return error;
    }

    private Object encryptAESContent(ReturnDataSet returnDataSet, HttpServletRequest request) {
        if (encrypted) {
            String[] path = request.getServletPath().split("\\/");
            if (path.length >= 3) {
                if (!ConfigConsts.REST.equals(path[2])) {
                    return returnDataSet;
                }
            } else {
                return returnDataSet;
            }
            StringJoiner stringJoiner = new StringJoiner(".");
            String methodName = null;
            for (int i = 0; i < path.length; i++) {
                if (path.length - 1 == i) {
                    methodName = path[i];
                } else {
                    if (i > 1) {
                        stringJoiner.add(path[i]);
                    }
                }
            }
            return addplusContainer.encryptAESContent(returnDataSet, stringJoiner.toString(), methodName);
        }
        return returnDataSet;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy