com.bsb.cms.moss.controller.exception.ExceptionController.java Source code

Java tutorial

Introduction

Here is the source code for com.bsb.cms.moss.controller.exception.ExceptionController.java

Source

/**
 * @(#)${file_name} ${date}
 *
 * Copyright (c) 2014-2015 BuShangBan (China) Int'l Co., Ltd
 * yongtai Road. pu dong District.Shanghai China
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of BuShangBan (China)
 *  Int'l Co., Ltd ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with BuShangBan (China).
 */
package com.bsb.cms.moss.controller.exception;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.bsb.cms.commons.web.JSONResultDTO;

/**
 * ???
 * @author hongjian.liu
 * @version 1.6.0 2014-6-11
 * @since 1.0
 */
@Controller
public class ExceptionController implements HandlerExceptionResolver {

    private static Logger LOG = Logger.getLogger(ExceptionController.class);

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.springframework.web.servlet.HandlerExceptionResolver#resolveException
     * (javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse, java.lang.Object,
     * java.lang.Exception)
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        ModelAndView mav = new ModelAndView();

        LOG.error(ex.getMessage(), ex);

        String url = request.getRequestURI();
        if (StringUtils.isNotBlank(url)) {
            if (!(request.getHeader("accept").indexOf("application/json") > -1
                    || (request.getHeader("X-Requested-With") != null
                            && request.getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1))) {// ?(?)

                mav.setViewName("/page/common/error");
            } else {//
                JSONResultDTO result = new JSONResultDTO();
                result.setMessage("???");
                result.setResult(JSONResultDTO.ERROR);
                try {
                    response.getWriter().write(JSONObject.fromObject(result).toString());
                } catch (IOException e) {
                    LOG.error("??=====" + ex.getLocalizedMessage());
                }
                return new ModelAndView();
            }
        }

        return mav;
    }

}