@Override public Date getLastModified() { Object d = metadata.getFirst(HttpHeaders.LAST_MODIFIED); if (d == null) return null; if (d instanceof Date) return (Date) d; return DateUtil.parseDate(d.toString()); }
@Override public Date getDate() { Object d = metadata.getFirst(HttpHeaders.DATE); if (d == null) return null; if (d instanceof Date) return (Date) d; return DateUtil.parseDate(d.toString()); }
public ClientResponse updateOnNotModified( ClientRequest request, BrowserCache.Entry old, BaseClientResponse response) throws Exception { old.getHeaders().remove(HttpHeaders.CACHE_CONTROL); old.getHeaders().remove(HttpHeaders.EXPIRES); old.getHeaders().remove(HttpHeaders.LAST_MODIFIED); String cc = (String) response.getResponseHeaders().getFirst(HttpHeaders.CACHE_CONTROL); String exp = (String) response.getResponseHeaders().getFirst(HttpHeaders.EXPIRES); int expires = -1; if (cc != null) { CacheControl cacheControl = CacheControl.valueOf(cc); if (cacheControl.isNoCache()) { return createClientResponse(request, old); } expires = cacheControl.getMaxAge(); } else if (exp != null) { Date date = DateUtil.parseDate(exp); expires = (int) ((date.getTime() - System.currentTimeMillis()) / 1000); } if (cc != null) { old.getHeaders().putSingle(HttpHeaders.CACHE_CONTROL, cc); } if (exp != null) { old.getHeaders().putSingle(HttpHeaders.CACHE_CONTROL, exp); } String lastModified = (String) response.getResponseHeaders().getFirst(HttpHeaders.LAST_MODIFIED); String etag = (String) response.getResponseHeaders().getFirst(HttpHeaders.ETAG); if (etag == null) etag = old.getHeaders().getFirst(HttpHeaders.ETAG); else old.getHeaders().putSingle(HttpHeaders.ETAG, etag); if (lastModified != null) { old.getHeaders().putSingle(HttpHeaders.LAST_MODIFIED, lastModified); } if (etag == null && lastModified == null && cc == null && exp == null) // don't cache { return createClientResponse(request, old); } BrowserCache.Entry entry = cache.put( request.getUri(), old.getMediaType(), old.getHeaders(), old.getCached(), expires, etag, lastModified); return createClientResponse(request, entry); }
public ClientResponse cacheIfPossible(ClientRequest request, BaseClientResponse response) throws Exception { String cc = (String) response.getResponseHeaders().getFirst(HttpHeaders.CACHE_CONTROL); String exp = (String) response.getResponseHeaders().getFirst(HttpHeaders.EXPIRES); int expires = -1; if (cc != null) { CacheControl cacheControl = CacheControl.valueOf(cc); if (cacheControl.isNoCache()) return response; expires = cacheControl.getMaxAge(); } else if (exp != null) { Date date = DateUtil.parseDate(exp); expires = (int) ((date.getTime() - System.currentTimeMillis()) / 1000); } String lastModified = (String) response.getResponseHeaders().getFirst(HttpHeaders.LAST_MODIFIED); String etag = (String) response.getResponseHeaders().getFirst(HttpHeaders.ETAG); String contentType = (String) response.getResponseHeaders().getFirst(HttpHeaders.CONTENT_TYPE); byte[] cached = ReadFromStream.readFromStream(1024, response.getStreamFactory().getInputStream()); response.getStreamFactory().performReleaseConnection(); MediaType mediaType = MediaType.valueOf(contentType); final BrowserCache.Entry entry = cache.put( request.getUri(), mediaType, response.getResponseHeaders(), cached, expires, etag, lastModified); response.setStreamFactory(new CachedStreamFactory(entry)); return response; }
@Override public Date getDate() { String date = requestHeaders.getFirst(DATE); if (date == null) return null; return DateUtil.parseDate(date); }
@Override public String toString(Date value) { if (value == null) throw new IllegalArgumentException("param was null"); return DateUtil.formatDate(value); }
@Override public Date fromString(String value) { if (value == null) throw new IllegalArgumentException("param was null"); return DateUtil.parseDate(value); }