com.clinic.controller.BaseController.java Source code

Java tutorial

Introduction

Here is the source code for com.clinic.controller.BaseController.java

Source

/*
 * Copyright (C) 2015 earth GuangHui Co.,Ltd All Rights Reserved.
 * ????????????.
 * ?????? www.heshidai.com.
 */
package com.clinic.controller;

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

import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.exception.ActionException;
import com.clinic.exception.ExceptionCode;
import com.clinic.exception.ServiceException;
import com.clinic.model.User;
import com.clinic.response.ResponseHandler;
import com.clinic.response.ResponseMessage;
import com.clinic.session.service.SessionService;
import com.clinic.util.LogTool;

/**
 * BaseController
 * 
 * @version 2016522?1:16:43
 * @author guangxi.zhang
 */
public class BaseController {
    protected static LogTool log = LogTool.getInstance(BaseController.class);

    public static final String REQUEST_DATA = "data";

    @SuppressWarnings("unused")
    @Autowired
    private SessionService sessionService;

    /**
     * 
     * ?id null
     * 
     * @param sessionid
     * @return
     * @throws ServiceException
     */
    protected User getUser(String sid) throws ServiceException {
        User user = null;
        /*
         * if (!StringUtils.isEmpty(sid)) { DSession session = sessionService.getSession(sid); if (null != session) { Object obj =
         * session.get(Constants.SESSION_USER); if (obj instanceof User) { return (User) obj; } } }
         */
        user = new User();
        user.setId((long) 123);
        return user;
    }

    public void exceptionHander(HttpServletRequest request, Exception exception, HttpServletResponse response) {
        // 
        StringBuffer sb = new StringBuffer();
        StackTraceElement[] stackArray = exception.getStackTrace();
        for (int i = 0; i < stackArray.length; i++) {
            StackTraceElement element = stackArray[i];
            sb.append(element.toString() + "\n");
        }
        log.error("" + sb.toString() + "" + exception.getMessage(), exception);
        // ???????
        if (exception instanceof ServiceException) {
            ServiceException e = (ServiceException) exception;
            switch (e.getErrorType()) {
            case ExceptionCode.GOODSCATEGORY_NULL_EXCEPTION:
                ResponseHandler.responseError(ResponseMessage.goodsCategory_dont_exist, response);
                break;
            case ExceptionCode.PRICE_ERROR:
                ResponseHandler.responseError(ResponseMessage.price_error, response);
                break;
            case ExceptionCode.USER_NO_LOGIN:
                ResponseHandler.responseApiLoginError(response);
                break;
            case ExceptionCode.DEL_GOODSCATEGORY_EXIST_PRODUCT:
                ResponseHandler.responseError(ResponseMessage.goodsCategory_product_error, response);
                break;
            case ExceptionCode.USER_NO_AUTHEN:
                ResponseHandler.responseError(ResponseMessage.account_no_auth, response);
                break;
            case ExceptionCode.SELLER_NO_OPEN_STORE:
                ResponseHandler.responseError(ResponseMessage.seller_no_store, response);
                break;
            case ExceptionCode.EXIST_BUSINESSLICENSENAME:
                ResponseHandler.responseError(ResponseMessage.exist_businesslicensename, response);
                break;
            case ExceptionCode.BUSINESSLICENSENAME_LENGTH_GT_30:
                ResponseHandler.responseError(ResponseMessage.businessLicenseName_length_gt_30, response);
                break;
            case ExceptionCode.BUSINESSLICENSENAME_LENGTH_LT_5:
                ResponseHandler.responseError(ResponseMessage.businessLicenseName_length_lt_5, response);
                break;
            default:
                ResponseHandler.responseServerError(response);
                break;
            }
            return;
        }
        if (exception instanceof com.alibaba.dubbo.rpc.RpcException) {
            ResponseHandler.responseServerTimeout(response);
        } else {
            ResponseHandler.responseServerError(response);
        }
    }

    /**
     * TODO ??
     *
     * @param token token
     * @param response ?
     * @throws ServiceException
     * @throws ActionException
     */
    public void checkLogin(String token, HttpServletResponse response) throws ServiceException, ActionException {
        User user = getUser(token);
        if (user == null) {
            ResponseHandler.responseError(ResponseMessage.user_not_login, response);
            throw new ActionException("?");
        }
    }

}