@Test public void testETagScenario() throws Exception { stubFor(getPath().willReturn(aResponse().withStatus(200).withBody("init").withHeader("ETag", "1ab"))); stubFor(getPath().withHeader("If-None-Match", equalTo("1ab")).willReturn(aResponse().withStatus(304))); stubFor(getPath().withHeader("If-None-Match", equalTo("1ac")).willReturn(aResponse().withStatus(200) .withHeader("ETag", "1ad").withBody("one"))); var source = Http.builder().socketTimeoutMs(10000).connectTimeoutMs(10000).build(); var id = Http.url(defaultURL(), ETAG); var loadedResource = source.load(id); assertThat(IOUtils.toString(loadedResource.getInputStream(), UTF_8)).isEqualTo("init"); assertThat(source.loadIfModified(id, loadedResource.getVersion())).isNull(); verify(exactly(1), getRequestedFor(urlPathEqualTo(PATH)).withHeader("If-None-Match", equalTo("1ab"))); loadedResource = source.loadIfModified(id, new ETag("1ac")); assertThat(loadedResource).isNotNull(); assertThat(IOUtils.toString(loadedResource.getInputStream(), UTF_8)).isEqualTo("one"); assertThat(loadedResource.getVersion().val().toString()).isEqualTo("1ad"); }
@Override public LoadedResource loadIfModified(HttpResourceId resourceId, SourceVersion version) { var cacheControl = resourceId.getCacheControl(); if (version == SourceVersion.EMPTY || cacheControl == CacheControlStrategy.NONE) { return loadResource(resourceId); } else if (cacheControl == CacheControlStrategy.ETAG) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_NONE_MATCH, version.val().toString()))); } else if (cacheControl == CacheControlStrategy.IF_MODIFIED_SINCE) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedValue(version.val())))); } else if (cacheControl == CacheControlStrategy.LAST_MODIFIED_HEAD || cacheControl == CacheControlStrategy.ETAG_HEAD) { if (isChanged(resourceId, version)) { return loadResource(resourceId); } } return null; }
HttpSource extends AbstractSource<HttpResourceId> implements NonListenableSource<HttpResourceId> { @Override public LoadedResource loadIfModified(HttpResourceId resourceId, SourceVersion version) { var cacheControl = resourceId.getCacheControl(); if (version == SourceVersion.EMPTY || cacheControl == CacheControlStrategy.NONE) { return loadResource(resourceId); } else if (cacheControl == CacheControlStrategy.ETAG) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_NONE_MATCH, version.val().toString()))); } else if (cacheControl == CacheControlStrategy.IF_MODIFIED_SINCE) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedValue(version.val())))); } else if (cacheControl == CacheControlStrategy.LAST_MODIFIED_HEAD || cacheControl == CacheControlStrategy.ETAG_HEAD) { if (isChanged(resourceId, version)) { return loadResource(resourceId); } } return null; } }
HttpSource extends AbstractSource<HttpResourceId> implements NonListenableSource<HttpResourceId> { @Override public LoadedResource loadIfModified(HttpResourceId resourceId, SourceVersion version) { var cacheControl = resourceId.getCacheControl(); if (version == SourceVersion.EMPTY || cacheControl == CacheControlStrategy.NONE) { return loadResource(resourceId); } else if (cacheControl == CacheControlStrategy.ETAG) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_NONE_MATCH, version.val().toString()))); } else if (cacheControl == CacheControlStrategy.IF_MODIFIED_SINCE) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedValue(version.val())))); } else if (cacheControl == CacheControlStrategy.LAST_MODIFIED_HEAD || cacheControl == CacheControlStrategy.ETAG_HEAD) { if (isChanged(resourceId, version)) { return loadResource(resourceId); } } return null; } HttpSource(CloseableHttpClient client);  HttpSource(CloseableHttpClient client,
                      @NotNull Consumer<HttpRequestBase> requestInterceptor, boolean keepAlive); }
HttpSource extends AbstractSource<HttpResourceId> implements NonListenableSource<HttpResourceId> { @Override public LoadedResource loadIfModified(HttpResourceId resourceId, SourceVersion version) { var cacheControl = resourceId.getCacheControl(); if (version == SourceVersion.EMPTY || cacheControl == CacheControlStrategy.NONE) { return loadResource(resourceId); } else if (cacheControl == CacheControlStrategy.ETAG) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_NONE_MATCH, version.val().toString()))); } else if (cacheControl == CacheControlStrategy.IF_MODIFIED_SINCE) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedValue(version.val())))); } else if (cacheControl == CacheControlStrategy.LAST_MODIFIED_HEAD || cacheControl == CacheControlStrategy.ETAG_HEAD) { if (isChanged(resourceId, version)) { return loadResource(resourceId); } } return null; } HttpSource(CloseableHttpClient client);  HttpSource(CloseableHttpClient client,
                      @NotNull Consumer<HttpRequestBase> requestInterceptor, boolean keepAlive); @Override LoadedResource loadIfModified(HttpResourceId resourceId, SourceVersion version); @Override String describe(); }
HttpSource extends AbstractSource<HttpResourceId> implements NonListenableSource<HttpResourceId> { @Override public LoadedResource loadIfModified(HttpResourceId resourceId, SourceVersion version) { var cacheControl = resourceId.getCacheControl(); if (version == SourceVersion.EMPTY || cacheControl == CacheControlStrategy.NONE) { return loadResource(resourceId); } else if (cacheControl == CacheControlStrategy.ETAG) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_NONE_MATCH, version.val().toString()))); } else if (cacheControl == CacheControlStrategy.IF_MODIFIED_SINCE) { return loadResource(resourceId, singletonList(new BasicHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedValue(version.val())))); } else if (cacheControl == CacheControlStrategy.LAST_MODIFIED_HEAD || cacheControl == CacheControlStrategy.ETAG_HEAD) { if (isChanged(resourceId, version)) { return loadResource(resourceId); } } return null; } HttpSource(CloseableHttpClient client);  HttpSource(CloseableHttpClient client,
                      @NotNull Consumer<HttpRequestBase> requestInterceptor, boolean keepAlive); @Override LoadedResource loadIfModified(HttpResourceId resourceId, SourceVersion version); @Override String describe();  }