public static String generateSetCookie(Cookie cookie) {
    if (cookie == null) {
      throw new IllegalArgumentException("the cookie is null");
    } else {
      StringBuilder sb = new StringBuilder();

      sb.append(cookie.getName()).append('=').append(cookie.getValue());

      if (VerifyUtils.isNotEmpty(cookie.getComment())) {
        sb.append(";Comment=").append(cookie.getComment());
      }

      if (VerifyUtils.isNotEmpty(cookie.getDomain())) {
        sb.append(";Domain=").append(cookie.getDomain());
      }
      if (cookie.getMaxAge() >= 0) {
        sb.append(";Max-Age=").append(cookie.getMaxAge());
      }

      String path = VerifyUtils.isEmpty(cookie.getPath()) ? "/" : cookie.getPath();
      sb.append(";Path=").append(path);

      if (cookie.getSecure()) {
        sb.append(";Secure");
      }

      sb.append(";Version=").append(cookie.getVersion());

      return sb.toString();
    }
  }
Exemple #2
0
 @Override
 public void addResponseCookie(final Cookie cookie) {
   javax.servlet.http.Cookie c =
       new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue());
   c.setSecure(cookie.isSecure());
   c.setPath(cookie.getPath());
   c.setMaxAge(cookie.getMaxAge());
   c.setHttpOnly(cookie.isHttpOnly());
   c.setComment(cookie.getComment());
   c.setDomain(cookie.getDomain());
   this.response.addCookie(c);
 }
Exemple #3
0
  private String encodeServerSide() {
    StringBuilder sb = new StringBuilder();

    for (Cookie cookie : cookies) {
      add(sb, cookie.getName(), cookie.getValue());

      if (cookie.getMaxAge() >= 0) {
        if (cookie.getVersion() == 0) {
          addUnquoted(
              sb,
              CookieHeaderNames.EXPIRES,
              new HttpHeaderDateFormat()
                  .format(new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L)));
        } else {
          add(sb, CookieHeaderNames.MAX_AGE, cookie.getMaxAge());
        }
      }

      if (cookie.getPath() != null) {
        if (cookie.getVersion() > 0) {
          add(sb, CookieHeaderNames.PATH, cookie.getPath());
        } else {
          addUnquoted(sb, CookieHeaderNames.PATH, cookie.getPath());
        }
      }

      if (cookie.getDomain() != null) {
        if (cookie.getVersion() > 0) {
          add(sb, CookieHeaderNames.DOMAIN, cookie.getDomain());
        } else {
          addUnquoted(sb, CookieHeaderNames.DOMAIN, cookie.getDomain());
        }
      }
      if (cookie.isSecure()) {
        sb.append(CookieHeaderNames.SECURE);
        sb.append((char) HttpConstants.SEMICOLON);
      }
      if (cookie.isHttpOnly()) {
        sb.append(CookieHeaderNames.HTTPONLY);
        sb.append((char) HttpConstants.SEMICOLON);
      }
      if (cookie.getVersion() >= 1) {
        if (cookie.getComment() != null) {
          add(sb, CookieHeaderNames.COMMENT, cookie.getComment());
        }

        add(sb, CookieHeaderNames.VERSION, 1);

        if (cookie.getCommentUrl() != null) {
          addQuoted(sb, CookieHeaderNames.COMMENTURL, cookie.getCommentUrl());
        }

        if (!cookie.getPorts().isEmpty()) {
          sb.append(CookieHeaderNames.PORT);
          sb.append((char) HttpConstants.EQUALS);
          sb.append((char) HttpConstants.DOUBLE_QUOTE);
          for (int port : cookie.getPorts()) {
            sb.append(port);
            sb.append((char) HttpConstants.COMMA);
          }
          sb.setCharAt(sb.length() - 1, (char) HttpConstants.DOUBLE_QUOTE);
          sb.append((char) HttpConstants.SEMICOLON);
        }
        if (cookie.isDiscard()) {
          sb.append(CookieHeaderNames.DISCARD);
          sb.append((char) HttpConstants.SEMICOLON);
        }
      }
    }

    if (sb.length() > 0) {
      sb.setLength(sb.length() - 1);
    }

    return sb.toString();
  }