@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"));
  }
Ejemplo n.º 2
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 ok() throws Exception {
    // given
    final Response resp = resource.version();

    // when
    final RestfulResponse<VersionRepresentation> restfulResponse = RestfulResponse.ofT(resp);

    // then
    final CacheControl cacheControl = restfulResponse.getHeader(Header.CACHE_CONTROL);
    assertThat(cacheControl, hasMaxAge(24 * 60 * 60));
    assertThat(cacheControl.getMaxAge(), is(24 * 60 * 60));
  }
 @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);
   }
 }
Ejemplo n.º 5
0
  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;
  }