Ejemplo n.º 1
0
 @Nullable
 private static DateTime parseDateHeader(@Nullable final String httpDate) {
   if (!Strings.isNullOrEmpty(httpDate)) {
     final Date date = DateUtils.parseDate(httpDate);
     if (date != null) {
       return new DateTime(date.getTime());
     }
   }
   return null;
 }
  @Override
  public void addCookie(Cookie cookie) {

    StringBuffer cookieHeader = new StringBuffer();

    cookieHeader.append(cookie.getName());
    cookieHeader.append("=");
    cookieHeader.append(cookie.getValue());
    if (cookie.getPath() != null) {
      cookieHeader.append("; Path=");
      cookieHeader.append(cookie.getPath());
    }
    if (cookie.getDomain() != null) {
      cookieHeader.append("; Domain=");
      cookieHeader.append(cookie.getDomain());
    }
    if (cookie.getMaxAge() > 0) {
      cookieHeader.append("; Max-Age=");
      cookieHeader.append(cookie.getMaxAge());
      /** This breaks IE when date of server and browser do not match */
      cookieHeader.append("; Expires=");
      if (cookie.getMaxAge() == 0) {
        cookieHeader.append(DateUtils.formatDate(new Date(10000), DateUtils.PATTERN_RFC1036));
      } else {
        cookieHeader.append(
            DateUtils.formatDate(
                new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L),
                DateUtils.PATTERN_RFC1036));
      }
    }

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

    /** Make sure we are not adding duplicate cookies */
    for (Entry<String, String> entry : response.getHeaders()) {
      if (entry.getKey().equals("Set-Cookie") && entry.getValue().equals(cookieHeader.toString())) {
        return;
      }
    }
    addHeader("Set-Cookie", cookieHeader.toString());
  }
 @Override
 public void addDateHeader(String name, long date) {
   response.setHeader(name, DateUtils.formatDate(new Date(date)));
 }
 @Override
 public void setDateHeader(String name, long date) {
   setHeader(name, DateUtils.formatDate(new Date(date)));
 }