コード例 #1
0
  /**
   * Test a cookie date value.
   *
   * @param headerValue The cookie date value.
   */
  private void testCookieDate(String dateValue) {
    final Date date = DateUtils.parse(dateValue, DateUtils.FORMAT_RFC_1036);

    // Rewrite the date
    final String newDateValue = DateUtils.format(date, DateUtils.FORMAT_RFC_1036.get(0));

    // Compare initial and new headers
    assertEquals(dateValue, newDateValue);
  }
コード例 #2
0
  @Override
  public CookieSettingWriter append(CookieSetting cookieSetting) throws IllegalArgumentException {
    String name = cookieSetting.getName();
    String value = cookieSetting.getValue();
    int version = cookieSetting.getVersion();

    if ((name == null) || (name.length() == 0)) {
      throw new IllegalArgumentException("Can't write cookie. Invalid name detected");
    }

    append(name).append('=');

    // Append the value
    if ((value != null) && (value.length() > 0)) {
      appendValue(value, version);
    }

    // Append the version
    if (version > 0) {
      append("; Version=");
      appendValue(Integer.toString(version), version);
    }

    // Append the path
    String path = cookieSetting.getPath();

    if ((path != null) && (path.length() > 0)) {
      append("; Path=");

      if (version == 0) {
        append(path);
      } else {
        appendQuotedString(path);
      }
    }

    // Append the expiration date
    int maxAge = cookieSetting.getMaxAge();

    if (maxAge >= 0) {
      if (version == 0) {
        long currentTime = System.currentTimeMillis();
        long maxTime = (maxAge * 1000L);
        long expiresTime = currentTime + maxTime;
        Date expires = new Date(expiresTime);

        append("; Expires=");
        appendValue(DateUtils.format(expires, DateUtils.FORMAT_RFC_1036.get(0)), version);
      } else {
        append("; Max-Age=");
        appendValue(Integer.toString(cookieSetting.getMaxAge()), version);
      }
    } else if ((maxAge == -1) && (version > 0)) {
      // Discard the cookie at the end of the user's session (RFC
      // 2965)
      append("; Discard");
    } else {
      // NetScape cookies automatically expire at the end of the
      // user's session
    }

    // Append the domain
    String domain = cookieSetting.getDomain();

    if ((domain != null) && (domain.length() > 0)) {
      append("; Domain=");
      appendValue(domain.toLowerCase(), version);
    }

    // Append the secure flag
    if (cookieSetting.isSecure()) {
      append("; Secure");
    }

    // Append the secure flag
    if (cookieSetting.isAccessRestricted()) {
      append("; HttpOnly");
    }

    // Append the comment
    if (version > 0) {
      String comment = cookieSetting.getComment();

      if ((comment != null) && (comment.length() > 0)) {
        append("; Comment=");
        appendValue(comment, version);
      }
    }

    return this;
  }