/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.opensymphony.xwork2.ActionInvocation
 *  com.opensymphony.xwork2.ValidationAware
 *  com.opensymphony.xwork2.config.entities.ExceptionMappingConfig
 *  com.opensymphony.xwork2.interceptor.AbstractInterceptor
 *  com.opensymphony.xwork2.interceptor.ExceptionHolder
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.commons.logging.Log
 *  org.apache.commons.logging.LogFactory
 *  org.apache.struts2.ServletActionContext
 *  org.springframework.dao.DataAccessException
 */
package com.hst.framework.interceptor;

import com.hst.framework.constants.XtglConstants;
import com.hst.framework.domain.XtErrMsgDomain;
import com.hst.framework.exception.ServiceException;
import com.hst.framework.util.HstEncodeUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.ExceptionHolder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.dao.DataAccessException;

public class HstIntegratedExceptionInterceptor
extends AbstractInterceptor {
    public static Log _logger = LogFactory.getLog(HstIntegratedExceptionInterceptor.class);

    public String intercept(ActionInvocation invocation) throws Exception {
        String result;
        try {
            result = invocation.invoke();
        }
        catch (Exception e) {
            boolean ajaxMethod = false;
            ajaxMethod = this.isAjaxRequest();
            ServiceException se = this.handleException(e);
            if (_logger.isInfoEnabled()) {
                this.handleLogging(e);
            }
            "Y".equals(XtglConstants.XT_XTCS.get(XtglConstants.XT_XTCS_EXCEPTION).getCsmrz());
            String aMessage = "\u9519\u8bef\u4ee3\u7801\uff1a" + se.getErrorCode() + "<br> \u9519\u8bef\u4fe1\u606f\uff1a" + this.dealErrorMess(se.getErrorMess(), se.getParamList());
            if (StringUtils.isNotBlank((CharSequence)se.getErrorDo())) {
                aMessage = String.valueOf(aMessage) + "<br> \u5904\u7406\u65b9\u5f0f\uff1a<br>" + se.getErrorDo();
            }
            if (ajaxMethod) {
                HttpServletResponse response = ServletActionContext.getResponse();
                String contentType = "text/plain;charset=UTF-8";
                response.setContentType(contentType);
                response.setStatus(500);
                response.getWriter().write(aMessage);
                return null;
            }
            Object action = invocation.getAction();
            if (action instanceof ValidationAware) {
                ValidationAware validationAwareAction = (ValidationAware)action;
                validationAwareAction.addActionError(aMessage);
            } else {
                _logger.debug((Object)("Action [" + action + "] is not ValidationAware, no message / error that are storeable"));
            }
            return invocation.getProxy().getMethod();
        }
        return result;
    }

    private String dealErrorMess(String errorMess, List<String> paramList) {
        String result = errorMess;
        int i = 0;
        while (i < paramList.size()) {
            result = result.replaceAll("\\$" + (i + 1) + "\\$", paramList.get(i));
            ++i;
        }
        if (result.length() > 200) {
            result = String.valueOf(result.substring(0, 200)) + "......";
        }
        return result;
    }

    private boolean isAjaxRequest() {
        String requestType = ServletActionContext.getRequest().getHeader("X-Requested-With");
        boolean ajaxMethod = requestType != null && requestType.equals("XMLHttpRequest");
        return ajaxMethod;
    }

    private ServiceException handleException(Exception e) {
        if (e instanceof DataAccessException) {
            DataAccessException dae = (DataAccessException)e;
            ServiceException se = new ServiceException((Throwable)dae);
            se.setErrorCode(XtglConstants.ERR_CODE_DB_EXCEPTION);
            this.setErrorMess(se);
            String message = dae.getMessage();
            message = HstEncodeUtil.ISO2GBK(message);
            int oraIndex = message.indexOf("ORA-");
            if (oraIndex > 0) {
                int secondOraIndex;
                if ((message = message.substring(oraIndex)).length() >= 4 && (secondOraIndex = message.substring(4, message.length()).indexOf("ORA")) > 0) {
                    message = message.substring(0, secondOraIndex + 4);
                }
                message = message.replaceAll("\"", "").replaceAll("\n", "");
            }
            ArrayList<String> paramList = new ArrayList<String>();
            paramList.add(message);
            se.setParamList(paramList);
            return se;
        }
        if (e instanceof ServiceException) {
            ServiceException se = (ServiceException)e;
            if (StringUtils.isBlank((CharSequence)se.getErrorCode())) {
                se.setErrorCode(XtglConstants.ERR_CODE_APP_EXCEPTION);
            }
            this.setErrorMess(se);
            return se;
        }
        ServiceException se = new ServiceException(e);
        se.setErrorCode(XtglConstants.ERR_CODE_APP_EXCEPTION);
        this.setErrorMess(se);
        return se;
    }

    private void setErrorMess(ServiceException se) {
        if (StringUtils.isBlank((CharSequence)se.getErrorCode())) {
            se.setErrorCode(XtglConstants.ERR_CODE_APP_EXCEPTION);
        }
        if (XtglConstants.ERROR_MES.containsKey(se.getErrorCode())) {
            XtErrMsgDomain msg = XtglConstants.ERROR_MES.get(se.getErrorCode());
            se.setErrorMess(msg.getErrMess());
            se.setErrorDo(msg.getErrDo());
        }
    }

    protected void handleLogging(Exception e) {
        _logger.error((Object)e.getMessage(), (Throwable)e);
    }

    protected String findResultFromExceptions(List<ExceptionMappingConfig> exceptionMappings, Throwable t) {
        String result = null;
        if (exceptionMappings != null) {
            int deepest = Integer.MAX_VALUE;
            for (ExceptionMappingConfig exceptionMapping : exceptionMappings) {
                ExceptionMappingConfig exceptionMappingConfig = exceptionMapping;
                int depth = this.getDepth(exceptionMappingConfig.getExceptionClassName(), t);
                if (depth < 0 || depth >= deepest) continue;
                deepest = depth;
                result = exceptionMappingConfig.getResult();
            }
        }
        return result;
    }

    public int getDepth(String exceptionMapping, Throwable t) {
        return this.getDepth(exceptionMapping, t.getClass(), 0);
    }

    private int getDepth(String exceptionMapping, Class<?> exceptionClass, int depth) {
        if (exceptionClass.getName().contains(exceptionMapping)) {
            return depth;
        }
        if (exceptionClass.equals(Throwable.class)) {
            return -1;
        }
        return this.getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);
    }

    protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) {
        invocation.getStack().push((Object)exceptionHolder);
    }
}
