private int mergeMaxAgeAndExpire(int maxAge, String expires) { // max age has precedence over expires if (maxAge != Integer.MIN_VALUE) { return maxAge; } else if (expires != null) { Date expiresDate = HttpHeaderDateFormat.get().parse(expires, new ParsePosition(0)); if (expiresDate != null) { long maxAgeMillis = expiresDate.getTime() - System.currentTimeMillis(); return (int) (maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0 ? 1 : 0)); } } return Integer.MIN_VALUE; }
private static String toString(Object value) { if (value == null) { return null; } if (value instanceof String) { return (String) value; } if (value instanceof Number) { return value.toString(); } if (value instanceof Date) { return HttpHeaderDateFormat.get().format((Date) value); } if (value instanceof Calendar) { return HttpHeaderDateFormat.get().format(((Calendar) value).getTime()); } return value.toString(); }
/** * Returns the date header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. * * @return the header value * @throws ParseException * if there is no such header or the header value is not a formatted date */ public static Date getDateHeader(HttpMessage message, String name) throws ParseException { String value = getHeader(message, name); if (value == null) { throw new ParseException("header not found: " + name, 0); } return HttpHeaderDateFormat.get().parse(value); }
/** * Returns the date header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. * * @return the header value or the {@code defaultValue} if there is no such * header or the header value is not a formatted date */ public static Date getDateHeader(HttpMessage message, String name, Date defaultValue) { final String value = getHeader(message, name); if (value == null) { return defaultValue; } try { return HttpHeaderDateFormat.get().parse(value); } catch (ParseException e) { // Not a date header - use the default. return defaultValue; } }
try { long maxAgeMillis = HttpHeaderDateFormat.get().parse(value).getTime() - System.currentTimeMillis();
/** * Sets the {@code "Date"} header. */ public static void setDate(HttpMessage message, Date value) { if (value != null) { message.headers().set(Names.DATE, HttpHeaderDateFormat.get().format(value)); } else { message.headers().set(Names.DATE, null); } }
/** * Sets a new date header with the specified name and value. If there * is an existing header with the same name, the existing header is removed. * The specified value is formatted as defined in * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a> */ public static void setDateHeader(HttpMessage message, String name, Date value) { if (value != null) { message.headers().set(name, HttpHeaderDateFormat.get().format(value)); } else { message.headers().set(name, null); } }
add(buf, CookieHeaderNames.MAX_AGE, cookie.maxAge()); Date expires = new Date((long) cookie.maxAge() * 1000 + System.currentTimeMillis()); add(buf, CookieHeaderNames.EXPIRES, HttpHeaderDateFormat.get().format(expires));