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

📄 usercookie.java

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

import javax.servlet.http.*;

import org.apache.commons.lang.*;
import org.apache.commons.logging.*;
import com.laoer.bbscs.bean.*;
import com.laoer.bbscs.service.config.*;
import com.laoer.comm.util.*;

/**
 * <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 UserCookie {

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

  private static final String PASS_USERNAME_KEY = "PASS_USERNAME";
  private static final String PASS_USERNAME_DES_KEY = "PASS_USERNAME_DES";

  private static final String BBSCS_FORUMPERNUM_KEY = "FN";
  private static final String BBSCS_POSTPERNUM_KEY = "PN";
  private static final String BBSCS_TIMEZONE_KEY = "TZ";
  private static final String BBSCS_FORUMVIEWMODE_KEY = "VM";
  private static final String BBSCS_LASTSENDNOTETIME_KEY = "LN";
  private static final String BBSCS_LASTPOSTTIME_KEY = "LP";
  private static final String BBSCS_EDITTYPE = "ET";

  private HttpServletRequest request;
  private HttpServletResponse response;
  private SysConfig sysConfig;
  private DES des;

  private int postPerNum = 10;
  private int forumPerNum = 20;
  private int forumViewMode = 0;
  private String timeZone = "GMT+08:00";
  private String pusername = "";
  private String pusernamedes = "";
  private long lastSendNoteTime = 0;
  private long lastPostTime = 0;
  private int editType = 0;

  public UserCookie(HttpServletRequest request, HttpServletResponse response, SysConfig sysConfig) {
    this.request = request;
    this.response = response;
    this.sysConfig = sysConfig;
    try {
      des = new DES(DES._DESede);
    }
    catch (Exception ex) {
      logger.error(ex);
    }
    getCookies();
  }

  private void getCookies() {
    Cookie cookies[] = request.getCookies();
    Cookie sCookie = null;
    byte[] buf;
    try {
      if (cookies != null && cookies.length > 0) {
        for (int i = 0; i < cookies.length; i++) {
          sCookie = cookies[i];
          if (this.sysConfig.isUsePass()) {
            if (sCookie.getName().equals(PASS_USERNAME_KEY)) {
              this.pusername = sCookie.getValue();
              //System.out.println("pass username:" + username);
            }
            if (sCookie.getName().equals(PASS_USERNAME_DES_KEY)) {
              if (StringUtils.isNotBlank(sCookie.getValue())) {
                buf = Util.base64decodebyte(sCookie.getValue());
                byte[] dec = des.decode(buf, Util.base64decodebyte(this.sysConfig.getCookieKey()));
                this.pusernamedes = new String(dec);
                //System.out.println("pass usernamedes:" + usernamedes);
              }
            }
          }
          if (sCookie.getName().equals(BBSCS_FORUMPERNUM_KEY)) {
            this.forumPerNum = Integer.parseInt(sCookie.getValue());
            //System.out.println("this.formPerNum "+this.formPerNum);
          }
          if (sCookie.getName().equals(BBSCS_POSTPERNUM_KEY)) {
            this.postPerNum = Integer.parseInt(sCookie.getValue());
          }
          if (sCookie.getName().equals(BBSCS_TIMEZONE_KEY)) {
            this.timeZone = Util.base64decode(sCookie.getValue());
          }
          if (sCookie.getName().equals(BBSCS_FORUMVIEWMODE_KEY)) {
            this.forumViewMode = Integer.parseInt(sCookie.getValue());

          }
          if (sCookie.getName().equals(BBSCS_EDITTYPE)) {
            this.editType = Integer.parseInt(sCookie.getValue());
          }

          if (sCookie.getName().equals(BBSCS_LASTSENDNOTETIME_KEY)) {
            if (StringUtils.isNotBlank(sCookie.getValue())) {
              buf = Util.base64decodebyte(sCookie.getValue());
              byte[] dec = des.decode(buf, Util.base64decodebyte(this.sysConfig.getCookieKey()));
              this.lastSendNoteTime = Long.parseLong(new String(dec));
            }
          }
          if (sCookie.getName().equals(BBSCS_LASTPOSTTIME_KEY)) {
            if (StringUtils.isNotBlank(sCookie.getValue())) {
              buf = Util.base64decodebyte(sCookie.getValue());
              byte[] dec = des.decode(buf, Util.base64decodebyte(this.sysConfig.getCookieKey()));
              this.lastPostTime = Long.parseLong(new String(dec));
            }
          }
        }
      }
    }
    catch (Exception ex) {
      logger.error(ex);
    }
  }

  public void addC(String name, String value) {
    Cookie cookies = new Cookie(name, value);
    cookies.setPath(this.sysConfig.getCookiePath());
    cookies.setMaxAge( -1);
    //cookies.setMaxAge(30 * 60);
    if (StringUtils.isNotBlank(this.sysConfig.getCookieDomain())) {
      cookies.setDomain(this.sysConfig.getCookieDomain());
    }
    this.response.addCookie(cookies);
  }

  public void addDES(String name, String value) {
    try {
      //DES des = new DES(DES._DESede);
      des.setKey(Util.base64decodebyte(this.sysConfig.getCookieKey()));
      byte[] enc = des.encode(value.getBytes());
      value = Util.base64Encode(enc);
      Cookie cookies = new Cookie(name, value);
      cookies.setPath(this.sysConfig.getCookiePath());
      //cookies.setMaxAge(30 * 60);
      cookies.setMaxAge( -1);
      if (StringUtils.isNotBlank(this.sysConfig.getCookieDomain())) {
        cookies.setDomain(this.sysConfig.getCookieDomain());
      }
      this.response.addCookie(cookies);
    }
    catch (Exception ex) {
      //ex.printStackTrace();
      logger.error("addDES(String name, String value)" + ex);
    }
  }

  public void addC(String name, String value, int maxage) {
    Cookie cookies = new Cookie(name, value);
    cookies.setPath(this.sysConfig.getCookiePath());
    cookies.setMaxAge(maxage);
    //cookies.setMaxAge(30 * 60);
    if (StringUtils.isNotBlank(this.sysConfig.getCookieDomain())) {
      cookies.setDomain(this.sysConfig.getCookieDomain());
    }
    this.response.addCookie(cookies);
  }

  public void addDES(String name, String value, int maxage) {
    try {
      //DES des = new DES(DES._DESede);
      des.setKey(Util.base64decodebyte(this.sysConfig.getCookieKey()));
      byte[] enc = des.encode(value.getBytes());
      value = Util.base64Encode(enc);
      Cookie cookies = new Cookie(name, value);
      cookies.setPath(this.sysConfig.getCookiePath());
      //cookies.setMaxAge(30 * 60);
      cookies.setMaxAge(maxage);
      if (StringUtils.isNotBlank(this.sysConfig.getCookieDomain())) {
        cookies.setDomain(this.sysConfig.getCookieDomain());
      }
      this.response.addCookie(cookies);
    }
    catch (Exception ex) {
      //ex.printStackTrace();
      logger.error("addDES(String name, String value)" + ex);
    }
  }

  public void addCookies(UserInfo ui, int maxage) {
    this.forumPerNum = ui.getForumPerNum();
    addC(BBSCS_FORUMPERNUM_KEY, String.valueOf(ui.getForumPerNum()), maxage);

    this.postPerNum = ui.getPostPerNum();
    addC(BBSCS_POSTPERNUM_KEY, String.valueOf(ui.getPostPerNum()), maxage);

    this.timeZone = ui.getTimeZone();
    addC(BBSCS_TIMEZONE_KEY, Util.base64Encode(ui.getTimeZone()), maxage);

    this.forumViewMode = ui.getForumViewMode();
    addC(BBSCS_FORUMVIEWMODE_KEY, String.valueOf(ui.getForumViewMode()), maxage);

    this.editType = ui.getEditType();
    addC(BBSCS_EDITTYPE, String.valueOf(ui.getEditType()), maxage);
  }

  public void addGuestCookies(int maxage) {
    this.forumPerNum = 20;
    addC(BBSCS_FORUMPERNUM_KEY, String.valueOf(this.forumPerNum), maxage);
    this.postPerNum = 10;
    addC(BBSCS_POSTPERNUM_KEY, String.valueOf(this.postPerNum), maxage);
    this.timeZone = "GMT+08:00";
    addC(BBSCS_TIMEZONE_KEY, Util.base64Encode(this.timeZone), maxage);
    this.forumViewMode = 0;
    addC(BBSCS_FORUMVIEWMODE_KEY, String.valueOf(this.forumViewMode), maxage);
    this.editType = -1;
    addC(BBSCS_EDITTYPE, String.valueOf(this.editType), maxage);
  }

  public void addLastNoteSendTime() {
    this.lastSendNoteTime = System.currentTimeMillis();
    addDES(BBSCS_LASTSENDNOTETIME_KEY, String.valueOf(this.lastSendNoteTime));
  }

  public void addLastPostTime() {
    this.lastPostTime = System.currentTimeMillis();
    addDES(BBSCS_LASTPOSTTIME_KEY, String.valueOf(this.lastPostTime));
  }

  public void addForumViewMode(int vm, int maxage) {
    this.forumViewMode = vm;
    addC(BBSCS_FORUMVIEWMODE_KEY, String.valueOf(vm), maxage);
  }

  public void removeAllCookies() {
    addC(BBSCS_FORUMPERNUM_KEY, "", 0);
    addC(BBSCS_POSTPERNUM_KEY, "", 0);
    addC(BBSCS_TIMEZONE_KEY, "", 0);
    //addC(BBSCS_FORUMVIEWMODE_KEY, "", 0);
    addC(BBSCS_LASTSENDNOTETIME_KEY, "", 0);
    addC(BBSCS_LASTPOSTTIME_KEY, "", 0);
    addC(BBSCS_FORUMVIEWMODE_KEY, "", 0);
    addC(BBSCS_EDITTYPE, "-1", 0);
  }

  public boolean isLoginPass() {
    if (StringUtils.isNotBlank(this.pusername) && StringUtils.isNotBlank(this.pusernamedes) &&
        this.pusername.equals(this.pusernamedes)) {
      return true;
    }
    return false;
  }

  public int getPostPerNum() {
    return postPerNum;
  }

  public int getForumPerNum() {

    return forumPerNum;
  }

  public String getTimeZone() {
    return timeZone;
  }

  public int getForumViewMode() {
    return forumViewMode;
  }

  public String getPusername() {
    return pusername;
  }

  public String getPusernamedes() {
    return pusernamedes;
  }

  public long getLastPostTime() {
    return lastPostTime;
  }

  public long getLastSendNoteTime() {
    return lastSendNoteTime;
  }

  public int getEditType() {
    return editType;
  }

  public void setPostPerNum(int postPerNum) {
    this.postPerNum = postPerNum;
  }

  public void setForumPerNum(int forumPerNum) {

    this.forumPerNum = forumPerNum;
  }

  public void setTimeZone(String timeZone) {
    this.timeZone = timeZone;
  }

  public void setForumViewMode(int forumViewMode) {
    this.forumViewMode = forumViewMode;
  }

  public void setPusername(String pusername) {
    this.pusername = pusername;
  }

  public void setPusernamedes(String pusernamedes) {
    this.pusernamedes = pusernamedes;
  }

  public void setLastPostTime(long lastPostTime) {
    this.lastPostTime = lastPostTime;
  }

  public void setLastSendNoteTime(long lastSendNoteTime) {
    this.lastSendNoteTime = lastSendNoteTime;
  }

  public void setEditType(int editType) {
    this.editType = editType;
  }

}

⌨️ 快捷键说明

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