⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useronlineinterceptor.java

📁 java论坛
💻 JAVA
字号:
package com.laoer.bbscs.web.interceptor;

import net.sf.struts.saif.ActionHaveForwardInterceptor;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import com.laoer.bbscs.comm.Constant;
import com.laoer.bbscs.web.servlet.UserSession;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.WebApplicationContext;
import com.laoer.bbscs.service.UserOnlineService;
import com.laoer.bbscs.bean.UserOnline;
import com.laoer.bbscs.service.config.SysConfig;
import com.laoer.bbscs.service.UserService;
import com.laoer.bbscs.bean.UserInfo;
import com.laoer.bbscs.exception.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.apache.struts.action.ActionMessage;
import com.laoer.bbscs.web.action.ActionUtils;
import org.apache.struts.action.ActionMessages;
import com.laoer.bbscs.comm.BBSCSUtil;
import com.laoer.bbscs.bean.Permission;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;
import com.laoer.bbscs.comm.AjaxMessagesXML;

/**
 * <p>Title: Tianyi BBS</p>
 *
 * <p>Description: BBSCS</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: Laoer.com</p>
 *
 * @author Gong Tianyi
 * @version 7.0
 */
public class UserOnlineInterceptor
    implements ActionHaveForwardInterceptor {

  private static final Log logger = LogFactory.getLog(UserOnlineInterceptor.class);

  public UserOnlineInterceptor() {
  }

  /**
   * beforeAction
   *
   * @param action Action
   * @param actionMapping ActionMapping
   * @param actionForm ActionForm
   * @param httpServletRequest HttpServletRequest
   * @param httpServletResponse HttpServletResponse
   * @return ActionForward
   * @throws IOException
   * @throws ServletException
   * @todo Implement this net.sf.struts.saif.ActionHaveForwardInterceptor method
   */
  public ActionForward beforeAction(Action action, ActionMapping actionMapping,
                                    ActionForm actionForm, HttpServletRequest httpServletRequest,
                                    HttpServletResponse httpServletResponse) throws IOException,
      ServletException {
    HttpSession session = httpServletRequest.getSession();
    String ajax = httpServletRequest.getParameter("ajax");
    if (StringUtils.isBlank(ajax)) {
      ajax = "html";
    }
    UserSession us = (UserSession) session.getAttribute(Constant.USER_SESSION_KEY);
    WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(action.
        getServlet().getServletContext());

    SysConfig sysConfig = (SysConfig) wc.getBean("sysConfig");

    long nowTime = System.currentTimeMillis();
    long addedTime = nowTime - us.getLastActiveTime();
    us.setAddedOnlineTime(us.getAddedOnlineTime() + addedTime);
    us.setAddedOnlineHour(us.getAddedOnlineHour() + addedTime);

    //logger.info(us.getUserName() + "累计在线时间:" + us.getAddedOnlineTime());

    if (us.getAddedOnlineTime() >= (sysConfig.getUserOnlineTime() * 1000)) {
      logger.info("用户 " + us.getUserName() + " 在线累计超过" + sysConfig.getUserOnlineTime() + "秒");

      UserOnlineService userOnlineService = (UserOnlineService) wc.getBean("userOnlineService");

      UserOnline uo = userOnlineService.findUserOnlineByUserID(us.getId()); //取得用户在线信息
      if (!uo.getValidateCode().equals(us.getValidateCode())) { //用户重复登录
        if (ajax.equalsIgnoreCase("html") || ajax.equalsIgnoreCase("shtml")) {
          ActionMessages errors = new ActionMessages();
          errors.add("error.login.re", new ActionMessage("error.login.re"));
          ActionUtils.addErrors(httpServletRequest, errors);
          //return actionMapping.findForward("login");
          session.removeAttribute(Constant.USER_SESSION_KEY);
          return ActionUtils.getLoginForward(actionMapping, httpServletRequest, sysConfig, "index");
        }
        else {
          AjaxMessagesXML ajaxMessagesXML = (AjaxMessagesXML) wc.getBean("ajaxMessagesXML");
          ajaxMessagesXML.setMessage(Constant.CODEID_USER_LOGIN_RE,
                                     Constant.MESSAGE.getMessage(httpServletRequest.getLocale(),
              "error.login.re"));
          httpServletRequest.setAttribute("xmlo", ajaxMessagesXML.getXmlo());
          httpServletRequest.setAttribute("messagesDocument", ajaxMessagesXML.getMessagesDocument());
          return actionMapping.findForward("ajaxjsp");

        }
      }

      if (us.getGroupID() != Constant.USER_GROUP_GUEST) { //如果不是游客

        UserService userService = (UserService) wc.getBean("userService");
        UserInfo ui = userService.findUserInfoById(us.getId());

        if (ui != null) { //取得正确用户信息

          ui.setStayTime(ui.getStayTime() + (us.getAddedOnlineTime() / 1000)); //增加用户在线时长
          us.setAddedOnlineTime(0);
          //更新用户权限 此处消耗性能较为严重,暂时注释
          //Map[] pmap = userService.getUserPermission(ui);
          //us = ActionUtils.reloadUserPermission(httpServletRequest, us, pmap);

          if (us.getAddedOnlineHour() >= 3600000) {
            logger.info("用户在线累计超过1个小时,增加用户经验值");

            ui.setExperience(ui.getExperience() + (int) (us.getAddedOnlineHour() / 3600000));
            us.setAddedOnlineHour(0);

          }
          try {
            ui = userService.saveUserInfo(ui);
            userService.writeUserFile(ui);
          }
          catch (BbscsException ex) {
            logger.error(ex);
          }
        }

      }
      else { //游客
        us.setAddedOnlineTime(0);
        if (us.getAddedOnlineHour() >= 3600000) {
          us.setAddedOnlineHour(0);
        }
      }

      //修改Online表用户在线时间
      uo.setOnlineTime(nowTime);
      try {
        userOnlineService.saveUserOnline(uo);
      }
      catch (BbscsException ex1) {
        logger.error(ex1);
      }

    }

    us.setLastActiveTime(nowTime);
    session.setAttribute(Constant.USER_SESSION_KEY, us);
    return null;
  }

  /**
   * afterAction
   *
   * @param action Action
   * @param actionMapping ActionMapping
   * @param actionForm ActionForm
   * @param httpServletRequest HttpServletRequest
   * @param httpServletResponse HttpServletResponse
   * @return ActionForward
   * @throws IOException
   * @throws ServletException
   * @todo Implement this net.sf.struts.saif.ActionHaveForwardInterceptor method
   */
  public ActionForward afterAction(Action action, ActionMapping actionMapping,
                                   ActionForm actionForm, HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) throws IOException,
      ServletException {
    return null;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -