From 1afe6a601bdcb084bcb7aba7c02453552f6cb317 Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Fri, 18 Mar 2022 22:06:45 +0400 Subject: [PATCH] Polish. --- .../nstdio/http/ext/CachedHttpResponse.java | 6 ++-- .../nstdio/http/ext/CachingInterceptor.java | 31 ++++++++++--------- .../io/github/nstdio/http/ext/NullCache.java | 5 +-- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/github/nstdio/http/ext/CachedHttpResponse.java b/src/main/java/io/github/nstdio/http/ext/CachedHttpResponse.java index f614346..3f42617 100644 --- a/src/main/java/io/github/nstdio/http/ext/CachedHttpResponse.java +++ b/src/main/java/io/github/nstdio/http/ext/CachedHttpResponse.java @@ -16,6 +16,8 @@ package io.github.nstdio.http.ext; +import io.github.nstdio.http.ext.Cache.CacheEntry; + import javax.net.ssl.SSLSession; import java.net.URI; import java.net.http.HttpClient; @@ -27,11 +29,11 @@ final class CachedHttpResponse implements HttpResponse { private final BodyHandler bodyHandler; private final HttpRequest request; - private final Cache.CacheEntry entry; + private final CacheEntry entry; private T body; - CachedHttpResponse(BodyHandler bodyHandler, HttpRequest request, Cache.CacheEntry entry) { + CachedHttpResponse(BodyHandler bodyHandler, HttpRequest request, CacheEntry entry) { this.bodyHandler = bodyHandler; this.request = request; this.entry = entry; diff --git a/src/main/java/io/github/nstdio/http/ext/CachingInterceptor.java b/src/main/java/io/github/nstdio/http/ext/CachingInterceptor.java index 5b88ee4..3ace233 100644 --- a/src/main/java/io/github/nstdio/http/ext/CachingInterceptor.java +++ b/src/main/java/io/github/nstdio/http/ext/CachingInterceptor.java @@ -24,10 +24,13 @@ import static java.util.stream.Collectors.toList; import static lombok.Lombok.sneakyThrow; +import io.github.nstdio.http.ext.Cache.CacheEntry; import lombok.RequiredArgsConstructor; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandler; +import java.net.http.HttpResponse.BodySubscriber; import java.time.Clock; import java.util.List; import java.util.Optional; @@ -79,7 +82,7 @@ public Chain intercept(Chain in) { RequestContext ctx = in.ctx(); if (ctx.isCacheable()) { - final Cache.CacheEntry entry = cacheEntry(ctx); + final CacheEntry entry = cacheEntry(ctx); if (ctx.cacheControl().onlyIfCached()) { return in.withResponse(forcedCacheResponse(ctx, entry)); @@ -103,9 +106,9 @@ public Chain intercept(Chain in) { } } - private HttpResponse forcedCacheResponse(RequestContext ctx, Cache.CacheEntry entry) { + private HttpResponse forcedCacheResponse(RequestContext ctx, CacheEntry entry) { final var request = ctx.request(); - Cache.CacheEntry forcedEntry = entry == null ? getCacheEntry(request) : entry; + CacheEntry forcedEntry = entry == null ? getCacheEntry(request) : entry; final HttpResponse response; if (forcedEntry == null || !forcedEntry.metadata().isFresh(ctx.cacheControl())) { response = gatewayTimeoutResponse(request); @@ -117,8 +120,8 @@ private HttpResponse forcedCacheResponse(RequestContext ctx, Cache.CacheE } - private Chain sendAndCache(Chain in, Cache.CacheEntry entry) { - var metadata = Optional.ofNullable(entry).map(Cache.CacheEntry::metadata); + private Chain sendAndCache(Chain in, CacheEntry entry) { + var metadata = Optional.ofNullable(entry).map(CacheEntry::metadata); var newCtx = metadata .map(m -> applyConditions(in.ctx().request(), m)) .map(in.ctx()::withRequest) @@ -143,10 +146,10 @@ && hasConditions(newCtx.request())) { return Chain.of(newCtx.withBodyHandler(bodyHandler), in.futureHandler().andThen(handler)); } - private HttpResponse.BodyHandler cacheAware(RequestContext ctx) { + private BodyHandler cacheAware(RequestContext ctx) { return info -> { - ctx.responseTime().set(clock.millis()); - HttpResponse.BodySubscriber sub = ctx.bodyHandler().apply(info); + ctx.responseTime().compareAndSet(0, clock.millis()); + BodySubscriber sub = ctx.bodyHandler().apply(info); if (isCacheable(info)) { var metadata = CacheEntryMetadata.of(ctx.requestTime().get(), ctx.responseTime().get(), @@ -189,7 +192,7 @@ private boolean isUnsafe(HttpRequest request) { || "DELETE".equalsIgnoreCase(method); } - private Cache.CacheEntry cacheEntry(RequestContext ctx) { + private CacheEntry cacheEntry(RequestContext ctx) { if (ctx.cacheControl().noCache()) { // TODO: check if entry is stale and remove it return null; @@ -198,18 +201,18 @@ private Cache.CacheEntry cacheEntry(RequestContext ctx) { return getCacheEntry(ctx.request()); } - private Cache.CacheEntry getCacheEntry(HttpRequest request) { + private CacheEntry getCacheEntry(HttpRequest request) { return cache.get(request); } - private boolean isFresh(RequestContext ctx, Cache.CacheEntry entry) { + private boolean isFresh(RequestContext ctx, CacheEntry entry) { return Optional.ofNullable(entry) - .map(Cache.CacheEntry::metadata) + .map(CacheEntry::metadata) .filter(m -> m.isFresh(ctx.cacheControl())) .isPresent(); } - private HttpResponse possiblyCached(RequestContext ctx, Cache.CacheEntry entry, HttpResponse r) { + private HttpResponse possiblyCached(RequestContext ctx, CacheEntry entry, HttpResponse r) { CacheEntryMetadata metadata = entry != null ? entry.metadata() : null; if (metadata != null) { switch (r.statusCode()) { @@ -238,7 +241,7 @@ private HttpResponse possiblyCached(RequestContext ctx, Cache.CacheEntry return r; } - private HttpResponse createCachedResponse(RequestContext ctx, Cache.CacheEntry entry) { + private HttpResponse createCachedResponse(RequestContext ctx, CacheEntry entry) { return new CachedHttpResponse<>(ctx.bodyHandler(), ctx.request(), entry); } } diff --git a/src/main/java/io/github/nstdio/http/ext/NullCache.java b/src/main/java/io/github/nstdio/http/ext/NullCache.java index dffe140..fd3c473 100644 --- a/src/main/java/io/github/nstdio/http/ext/NullCache.java +++ b/src/main/java/io/github/nstdio/http/ext/NullCache.java @@ -24,6 +24,8 @@ class NullCache implements Cache { static final NullCache INSTANCE = new NullCache(); + private static final Consumer NOOP_CONSUMER = t -> { + }; private static final Writer WRITER = new Writer<>() { @Override public HttpResponse.BodySubscriber subscriber() { @@ -32,8 +34,7 @@ public HttpResponse.BodySubscriber subscriber() { @Override public Consumer finisher() { - return t -> { - }; + return NOOP_CONSUMER; } };