首页

对客户端http的Cookie增删改查curd处理工具类CookieUtils

标签:cookie工具类,java,会话session     发布时间:2016-07-22   

通过CookieUtils工具类对客户端信息的cookie文件进行增删改查处理,标准化相关逻辑,从而减少相关代码逻辑复杂性,具体实现代码如下

import javax.servlet.http.Cookie;@b@import javax.servlet.http.HttpServletRequest;@b@@b@public class CookieUtils@b@{@b@  public Cookie createCookie(String cookieName, String cookieValue, String domain)@b@  {@b@    return createCookie(cookieName, cookieValue, domain, null);@b@  }@b@@b@  public Cookie deleteCookie(String cookieName, String domain, String path, String cookieValue)@b@  {@b@    Cookie c = createCookie(cookieName, cookieValue, domain, path);@b@    c.setMaxAge(0);@b@    return c;@b@  }@b@@b@  public Cookie createCookie(String cookieName, String cookieValue, String domain, String path)@b@  {@b@    Cookie c = new Cookie(cookieName, cookieValue);@b@@b@    if (domain != null) if ((domain = domain.trim()).length() > 0)@b@        c.setDomain(domain);@b@@b@@b@    if (path != null) if ((path = path.trim()).length() > 0)@b@        c.setPath(path);@b@    else {@b@      c.setPath("/");@b@    }@b@@b@    c.setMaxAge(-1);@b@    c.setSecure(false);@b@    return c;@b@  }@b@@b@  public String getCookieValue(HttpServletRequest request, String cookieName)@b@  {@b@    Cookie[] arr$;@b@    int i$;@b@    Cookie[] cookies = request.getCookies();@b@    if (cookies != null) {@b@      arr$ = cookies; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { Cookie c = arr$[i$];@b@        if (c.getName().equals(cookieName))@b@          return c.getValue();@b@      }@b@    }@b@    return null;@b@  }@b@}

Cookie源码类,如下

package javax.servlet.http;@b@@b@import java.io.Serializable;@b@import java.text.MessageFormat;@b@import java.util.Locale;@b@import java.util.ResourceBundle;@b@@b@public class Cookie@b@  implements Cloneable, Serializable@b@{@b@  private static final long serialVersionUID = 1L;@b@  private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";@b@  private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");@b@  private String name;@b@  private String value;@b@  private String comment;@b@  private String domain;@b@  private int maxAge = -1;@b@  private String path;@b@  private boolean secure;@b@  private int version = 0;@b@  private boolean httpOnly;@b@  private static final String tspecials = ",; ";@b@  private static final String tspecials2NoSlash = "()<>@,;:\\\"[]?={} \t";@b@  private static final String tspecials2WithSlash = "()<>@,;:\\\"[]?={} \t/";@b@  private static final String tspecials2;@b@  private static final boolean STRICT_SERVLET_COMPLIANCE = Boolean.valueOf(System.getProperty("org.apache.catalina.STRICT_SERVLET_COMPLIANCE", "false")).booleanValue();@b@  private static final boolean FWD_SLASH_IS_SEPARATOR;@b@  private static final boolean STRICT_NAMING;@b@@b@  public Cookie(String name, String value)@b@  {@b@    if ((name == null) || (name.length() == 0)) {@b@      throw new IllegalArgumentException(lStrings.getString("err.cookie_name_blank"));@b@    }@b@@b@    if ((!(isToken(name))) || (name.equalsIgnoreCase("Comment")) || (name.equalsIgnoreCase("Discard")) || (name.equalsIgnoreCase("Domain")) || (name.equalsIgnoreCase("Expires")) || (name.equalsIgnoreCase("Max-Age")) || (name.equalsIgnoreCase("Path")) || (name.equalsIgnoreCase("Secure")) || (name.equalsIgnoreCase("Version")) || (name.startsWith("$")))@b@    {@b@      String errMsg = lStrings.getString("err.cookie_name_is_token");@b@      Object[] errArgs = new Object[1];@b@      errArgs[0] = name;@b@      errMsg = MessageFormat.format(errMsg, errArgs);@b@      throw new IllegalArgumentException(errMsg);@b@    }@b@@b@    this.name = name;@b@    this.value = value;@b@  }@b@@b@  public void setComment(String purpose)@b@  {@b@    this.comment = purpose;@b@  }@b@@b@  public String getComment()@b@  {@b@    return this.comment;@b@  }@b@@b@  public void setDomain(String pattern)@b@  {@b@    this.domain = pattern.toLowerCase(Locale.ENGLISH);@b@  }@b@@b@  public String getDomain()@b@  {@b@    return this.domain;@b@  }@b@@b@  public void setMaxAge(int expiry)@b@  {@b@    this.maxAge = expiry;@b@  }@b@@b@  public int getMaxAge()@b@  {@b@    return this.maxAge;@b@  }@b@@b@  public void setPath(String uri)@b@  {@b@    this.path = uri;@b@  }@b@@b@  public String getPath()@b@  {@b@    return this.path;@b@  }@b@@b@  public void setSecure(boolean flag)@b@  {@b@    this.secure = flag;@b@  }@b@@b@  public boolean getSecure()@b@  {@b@    return this.secure;@b@  }@b@@b@  public String getName()@b@  {@b@    return this.name;@b@  }@b@@b@  public void setValue(String newValue)@b@  {@b@    this.value = newValue;@b@  }@b@@b@  public String getValue()@b@  {@b@    return this.value;@b@  }@b@@b@  public int getVersion()@b@  {@b@    return this.version;@b@  }@b@@b@  public void setVersion(int v)@b@  {@b@    this.version = v;@b@  }@b@@b@  private boolean isToken(String possibleToken)@b@  {@b@    int len = possibleToken.length();@b@@b@    for (int i = 0; i < len; ++i) {@b@      char c = possibleToken.charAt(i);@b@@b@      if ((c < ' ') || (c >= '') || (",; ".indexOf(c) != -1) || ((STRICT_NAMING) && (tspecials2.indexOf(c) != -1)))@b@      {@b@        return false;@b@      }@b@    }@b@    return true;@b@  }@b@@b@  public Object clone()@b@  {@b@    try@b@    {@b@      return super.clone();@b@    } catch (CloneNotSupportedException e) {@b@      throw new RuntimeException(e.getMessage());@b@    }@b@  }@b@@b@  public void setHttpOnly(boolean httpOnly)@b@  {@b@    this.httpOnly = httpOnly;@b@  }@b@@b@  public boolean isHttpOnly()@b@  {@b@    return this.httpOnly;@b@  }@b@@b@  static@b@  {@b@    String fwdSlashIsSeparator = System.getProperty("org.apache.tomcat.util.http.ServerCookie.FWD_SLASH_IS_SEPARATOR");@b@@b@    if (fwdSlashIsSeparator == null)@b@      FWD_SLASH_IS_SEPARATOR = STRICT_SERVLET_COMPLIANCE;@b@    else {@b@      FWD_SLASH_IS_SEPARATOR = Boolean.valueOf(fwdSlashIsSeparator).booleanValue();@b@    }@b@@b@    if (FWD_SLASH_IS_SEPARATOR)@b@      tspecials2 = "()<>@,;:\\\"[]?={} \t/";@b@    else {@b@      tspecials2 = "()<>@,;:\\\"[]?={} \t";@b@    }@b@@b@    String strictNaming = System.getProperty("org.apache.tomcat.util.http.ServerCookie.STRICT_NAMING");@b@@b@    if (strictNaming == null)@b@      STRICT_NAMING = STRICT_SERVLET_COMPLIANCE;@b@    else@b@      STRICT_NAMING = Boolean.valueOf(strictNaming).booleanValue();@b@  }@b@}