/*
   * (non-Javadoc)
   *
   * @see
   * com.newtouch.lion.dsession.store.DistributedCookieStore#invalidate(com
   * .newtouch.lion.session.context.RequestContext)
   */
  @Override
  public void invalidate(DistributedSessionContext sessionContext) {
    Cookie[] cookies = sessionContext.getRequest().getCookies();
    // 判断是否为空
    if (cookies == null || cookies.length == 0) {
      return;
    }

    for (Cookie cookie : cookies) {
      String cookieName = cookie.getName();
      String decodeCookieName = cookieName;
      // 编码
      if (distributedCookieAttributeConfig.getEncoder() != null) {
        decodeCookieName =
            distributedCookieAttributeConfig.getEncoder().encodeName(decodeCookieName);
      }

      if (distributedCookieAttributeConfig.isMatch(decodeCookieName)) {
        cookie.setDomain(distributedCookieAttributeConfig.getDomain());
        cookie.setPath(distributedCookieAttributeConfig.getPath());
        cookie.setSecure(distributedCookieAttributeConfig.isSecure());
        cookie.setMaxAge(0);
        sessionContext.getResponse().addCookie(cookie);
      }
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.newtouch.lion.dsession.store.DistributedCookieStore#setAttribute(
   * com.newtouch.lion.session.context.RequestContext, java.lang.String,
   * java.lang.Object)
   */
  @Override
  public void setAttribute(DistributedSessionContext sessionContext, String key, Object value) {
    if (this.distributedCookieAttributeConfig.getEncoder() != null) {
      key = this.distributedCookieAttributeConfig.getEncoder().encodeName(key);
      value = this.distributedCookieAttributeConfig.getEncoder().encodeValue(value);
    }

    Cookie cookie = new Cookie(key, value.toString());
    cookie.setDomain(this.distributedCookieAttributeConfig.getDomain());
    cookie.setMaxAge(this.distributedCookieAttributeConfig.getMaxAge());
    cookie.setPath(this.distributedCookieAttributeConfig.getPath());
    cookie.setSecure(this.distributedCookieAttributeConfig.isSecure());
    logger.debug("set cookie: {}-{}", key, value);
    sessionContext.getResponse().addCookie(cookie);
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.newtouch.lion.dsession.store.DistributedCookieStore#removeAttribute
   * (com.newtouch.lion.session.context.SessionRequestContext,
   * java.lang.String)
   */
  @Override
  public void removeAttribute(DistributedSessionContext requestContext, String key) {
    if (key == null) {
      return;
    }

    String decodeCookieName = key;
    if (distributedCookieAttributeConfig.getEncoder() != null) {
      decodeCookieName = distributedCookieAttributeConfig.getEncoder().encodeName(decodeCookieName);
    }
    Cookie cookie = new Cookie(decodeCookieName, null);
    cookie.setDomain(distributedCookieAttributeConfig.getDomain());
    cookie.setPath(distributedCookieAttributeConfig.getPath());
    cookie.setSecure(distributedCookieAttributeConfig.isSecure());
    cookie.setMaxAge(0);
    logger.debug("Remove-cookie: {}", key);
    requestContext.getResponse().addCookie(cookie);
  }