@Test public void testReadMultiplePrivateAndNoCacheFields() { String s = "private=\"foo1,foo2\",no-store,no-transform," + "must-revalidate,proxy-revalidate,max-age=2,s-maxage=3,no-cache=\"bar1,bar2\"," + "ext=1"; CacheControl cc = CacheControl.valueOf(s); assertTrue(cc.isPrivate()); List<String> privateFields = cc.getPrivateFields(); assertEquals(2, privateFields.size()); assertEquals("foo1", privateFields.get(0)); assertEquals("foo2", privateFields.get(1)); assertTrue(cc.isNoCache()); List<String> noCacheFields = cc.getNoCacheFields(); assertEquals(2, noCacheFields.size()); assertEquals("bar1", noCacheFields.get(0)); assertEquals("bar2", noCacheFields.get(1)); assertTrue(cc.isNoStore()); assertTrue(cc.isNoTransform()); assertTrue(cc.isMustRevalidate()); assertTrue(cc.isProxyRevalidate()); assertEquals(2, cc.getMaxAge()); assertEquals(3, cc.getSMaxAge()); Map<String, String> exts = cc.getCacheExtension(); assertEquals(1, exts.size()); assertEquals("1", exts.get("ext")); }
@Test public void testToString() { String s = "private=\"foo\",no-cache=\"bar\",no-store,no-transform," + "must-revalidate,proxy-revalidate,max-age=2,s-maxage=3"; String parsed = CacheControl.valueOf(s).toString(); assertEquals(s, parsed); }
@Test public void testFromSimpleString() { CacheControl c = CacheControl.valueOf("public,must-revalidate"); assertTrue(!c.isPrivate() && !c.isNoStore() && c.isMustRevalidate() && !c.isProxyRevalidate()); assertTrue( !c.isNoCache() && !c.isNoTransform() && c.getNoCacheFields().size() == 0 && c.getPrivateFields().size() == 0); }
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); }
@Test public void testResource2() throws Exception { ClientRequest request = new ClientRequest(generateURL("/nocache")); try { ClientResponse<?> response = request.get(); Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus()); String value = response.getResponseHeaders().getFirst("cache-control"); Assert.assertEquals("no-cache", value); System.out.println("Cache-Control: " + value); CacheControl cc = CacheControl.valueOf(value); Assert.assertTrue(cc.isNoCache()); response.releaseConnection(); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testFromComplexString() { CacheControl c = CacheControl.valueOf( "private=\"foo\",no-cache=\"bar\",no-store,no-transform," + "must-revalidate,proxy-revalidate,max-age=2,s-maxage=3"); assertTrue( c.isPrivate() && c.isNoStore() && c.isMustRevalidate() && c.isProxyRevalidate() && c.isNoCache()); assertTrue( c.isNoTransform() && c.getNoCacheFields().size() == 1 && c.getPrivateFields().size() == 1); assertEquals("foo", c.getPrivateFields().get(0)); assertEquals("bar", c.getNoCacheFields().get(0)); }
@Test public void testResource() throws Exception { ClientRequest request = new ClientRequest(generateURL("/maxage")); try { ClientResponse<?> response = request.get(); Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus()); System.out.println( "Cache-Control: " + response.getResponseHeaders().getFirst("cache-control")); CacheControl cc = CacheControl.valueOf(response.getResponseHeaders().getFirst("cache-control")); Assert.assertFalse(cc.isPrivate()); Assert.assertEquals(3600, cc.getMaxAge()); response.releaseConnection(); } catch (Exception e) { throw new RuntimeException(e); } }
// test all available non-static Response methods public Response method() { Response.ResponseBuilder responseBuilder = Response.ok(); responseBuilder.header("X-Test", "Hello"); responseBuilder.cacheControl(CacheControl.valueOf("")); responseBuilder.contentLocation(URI.create("")); responseBuilder.cookie(); responseBuilder.entity(12d); responseBuilder.expires(new Date()); responseBuilder.language(Locale.ENGLISH); responseBuilder.encoding("UTF-8"); responseBuilder.lastModified(new Date()); responseBuilder.link(URI.create(""), "rel"); responseBuilder.location(URI.create("")); responseBuilder.status(433); responseBuilder.tag(new EntityTag("")); responseBuilder.type(MediaType.APPLICATION_JSON_TYPE); responseBuilder.variants(new LinkedList<>()); return responseBuilder.build(); }
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; }
@SuppressWarnings("unchecked") protected boolean setResponseHeader( Exchange exchange, Response.ResponseBuilder responseBuilder, String header, Object value) { // there must be a value going forward if (value == null) { return true; } if (header.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) { if (value instanceof List) { // TODO } if (value instanceof String) { responseBuilder.cacheControl(CacheControl.valueOf(value.toString())); } return true; } else if (header.equalsIgnoreCase(HttpHeaders.EXPIRES)) { if (value instanceof Calendar) { responseBuilder.expires(((Calendar) value).getTime()); } else if (value instanceof Date) { responseBuilder.expires((Date) value); } else if (value instanceof String) { SimpleDateFormat format = new SimpleDateFormat(RFC_2822_DATE_PATTERN, Locale.ENGLISH); try { Date date = format.parse((String) value); responseBuilder.expires(date); } catch (ParseException e) { logger.debug( "Header {} with value {} cannot be converted as a Date. The value will be ignored.", HttpHeaders.EXPIRES, value); } } return true; } else if (header.equalsIgnoreCase(HttpHeaders.LAST_MODIFIED)) { if (value instanceof Calendar) { responseBuilder.lastModified(((Calendar) value).getTime()); } else if (value instanceof Date) { responseBuilder.lastModified((Date) value); } else if (value instanceof String) { SimpleDateFormat format = new SimpleDateFormat(RFC_2822_DATE_PATTERN, Locale.ENGLISH); try { Date date = format.parse((String) value); responseBuilder.lastModified(date); } catch (ParseException e) { logger.debug( "Header {} with value {} cannot be converted as a Date. The value will be ignored.", HttpHeaders.LAST_MODIFIED, value); } } return true; } else if (header.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) { if (value instanceof Long) { } else if (value instanceof Integer) { } else { Long num = exchange.getContext().getTypeConverter().tryConvertTo(Long.class, value); if (num != null) { } else { logger.debug( "Header {} with value {} cannot be converted as a Long. The value will be ignored.", HttpHeaders.CONTENT_LENGTH, value); } } return true; } else if (header.equalsIgnoreCase(HttpHeaders.CONTENT_TYPE)) { if (value instanceof MediaType) { responseBuilder.header(HttpHeaders.CONTENT_TYPE, value.toString()); } else { String type = value.toString(); MediaType media = MediaType.valueOf(type); if (media != null) { responseBuilder.header(HttpHeaders.CONTENT_TYPE, type); } else { logger.debug( "Header {} with value {} cannot be converted as a MediaType. The value will be ignored.", HttpHeaders.CONTENT_TYPE, value); } } return true; } else { responseBuilder.header(header, value); } return false; }
@Test(expected = IllegalArgumentException.class) public void testValueOfNull() { CacheControl.valueOf(null); }