/** * Can be used to parse http times. For instance something like a http * header Date: Tue, 26 Mar 2013 13:47:13 GMT * * @param httpDateFormat in http format: Date: Tue, 26 Mar 2013 13:47:13 GMT * @return A nice "DateTime" (JodaTime) object containing that http * timestamp. * @throws ParseException If something goes wrong. */ public static Date parseHttpDateFormatToDateTime(String httpDateFormat) throws ParseException { return getRFC1123_DateFormat().parse(httpDateFormat); }
/** * Can be used to parse http times. For instance something like a http * header Date: Tue, 26 Mar 2013 13:47:13 GMT * <p/> * INFO: consider the JodaTime based * DateUtil.parseHttpDateFormatToDateTime(...) version * * @param httpDateFormat in http format: Date: Tue, 26 Mar 2013 13:47:13 GMT * @return A nice "Date" object containing that http timestamp. * @throws ParseException If something goes wrong. */ public static Date parseHttpDateFormat(String httpDateFormat) throws ParseException { return parseHttpDateFormatToDateTime(httpDateFormat); }
/** * Sets a header. * * @param name * @param value * @return the response */ public Response header(String name, Date value) { checkCommitted(); getHeaderMap().put(name, DateUtils.formatForHttpHeader(value)); return this; }
public boolean isModified(String etag, long lastModified, RouteContext routeContext) { final String browserEtag = routeContext.getHeader(HttpConstants.Header.IF_NONE_MATCH); if (browserEtag != null && !StringUtils.isNullOrEmpty(etag)) { return !(browserEtag.equals(etag)); } final String ifModifiedSince = routeContext.getHeader(HttpConstants.Header.IF_MODIFIED_SINCE); if ((lastModified > 0) && !StringUtils.isNullOrEmpty(ifModifiedSince)) { try { Date browserDate = DateUtils.parseHttpDateFormat(ifModifiedSince); if (browserDate.getTime() >= lastModified) { return false; } } catch (ParseException e) { log.warn("Can't parse HTTP date", e); } } return true; }
public void addEtag(RouteContext routeContext, long lastModified) { if (pippoSettings.isProd()) { String maxAge = pippoSettings.getString(PippoConstants.SETTING_HTTP_CACHE_CONTROL, "3600"); if (maxAge.equals("0")) { routeContext.setHeader(HttpConstants.Header.CACHE_CONTROL, "no-cache"); } else { routeContext.setHeader(HttpConstants.Header.CACHE_CONTROL, "max-age=" + maxAge); } } else { routeContext.setHeader(HttpConstants.Header.CACHE_CONTROL, "no-cache"); } // Use etag on demand: String etag = null; boolean useEtag = pippoSettings.getBoolean(PippoConstants.SETTING_HTTP_USE_ETAG, true); if (useEtag) { // ETag right now is only lastModified long. // maybe we change that in the future. etag = "\"" + lastModified + "\""; routeContext.setHeader(HttpConstants.Header.ETAG, etag); } if (isModified(etag, lastModified, routeContext)) { routeContext.setHeader(HttpConstants.Header.LAST_MODIFIED, DateUtils.formatForHttpHeader(lastModified)); } else if (routeContext.isRequestMethod(HttpConstants.Method.GET)) { routeContext.status(HttpConstants.StatusCode.NOT_MODIFIED); } }
/** * Can be used to format a date into http header compatible strings. * <p/> * It can be used to generate something like: Date: Wed, 05 Sep 2012 * 09:16:19 GMT Expires: Thu, 01 Jan 1970 00:00:00 GMT * * @param date The date to format * @return a http header compatible string like * "Thu, 01 Jan 1970 00:00:00 GMT" */ public static String formatForHttpHeader(Date date) { return getRFC1123_DateFormat().format(date); }
/** * Can be used to format a unix timestamp into http header compatible * strings. * <p/> * It can be used to generate something like: Date: Wed, 05 Sep 2012 * 09:16:19 GMT Expires: Thu, 01 Jan 1970 00:00:00 GMT * * @param unixTime The long (unixtime) to format * @return a http header compatible string like * "Thu, 01 Jan 1970 00:00:00 GMT" */ public static String formatForHttpHeader(long unixTime) { return getRFC1123_DateFormat().format(new Date(unixTime)); }