From 7b1afb0d66f79f2ae7f6a59879954e53f76fd0a3 Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Fri, 25 Oct 2024 15:54:15 +0200 Subject: [PATCH] clean(core): Switch OkHttpResource to use overwrite() instead of detect() --- .../common/io/resource/MediaTypeDetector.java | 8 ++------ .../enola/common/io/resource/OkHttpResource.java | 15 +++++++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/java/dev/enola/common/io/resource/MediaTypeDetector.java b/java/dev/enola/common/io/resource/MediaTypeDetector.java index cc03fb7a8..6a1928cc0 100644 --- a/java/dev/enola/common/io/resource/MediaTypeDetector.java +++ b/java/dev/enola/common/io/resource/MediaTypeDetector.java @@ -73,7 +73,7 @@ private static boolean isSpecial(MediaType mediaType) { */ MediaType detect(URI uri, ByteSource byteSource) { var mediaTypeCharset = URIs.getMediaTypeAndCharset(uri); - var detected = detect(mediaTypeCharset.mediaType(), mediaTypeCharset.charset(), uri); + var detected = detect(mediaTypeCharset.mediaType(), mediaTypeCharset.charset()); detected = detectCharsetAndMediaType(uri, byteSource, detected); return detected; } @@ -144,11 +144,7 @@ private MediaType fixMissingCharset(MediaType mediaType) { return mediaType; } - // This is currently still used by both UrlResource & OkHttpResource (and MediaTypeDetectorTest) - // but this is conceptually the same as the overwrite(URI uri, MediaType originalMediaType) - // TODO Switch OkHttpResource to use that instead - // TODO Make private (or inline and remove) - MediaType detect(@Nullable String contentType, @Nullable String contentEncoding, URI uri) { + private MediaType detect(@Nullable String contentType, @Nullable String contentEncoding) { MediaType mediaType = null; if (contentType != null) { mediaType = MediaTypes.parse(contentType); diff --git a/java/dev/enola/common/io/resource/OkHttpResource.java b/java/dev/enola/common/io/resource/OkHttpResource.java index a88828299..38374d596 100644 --- a/java/dev/enola/common/io/resource/OkHttpResource.java +++ b/java/dev/enola/common/io/resource/OkHttpResource.java @@ -54,15 +54,14 @@ public class OkHttpResource extends BaseResource implements ReadableResource { private static final Logger LOG = LoggerFactory.getLogger(OkHttpResource.class); + // This must be increased if there are test failures on slow CI servers :( + private static final Duration t = Duration.ofMillis(7500); + // https://square.github.io/okhttp/features/caching/ private static final File cacheDir = new File(FreedesktopDirectories.CACHE_FILE, OkHttpResource.class.getSimpleName()); private static final Cache cache = new Cache(cacheDir, 50L * 1024L * 1024L /* 50 MiB */); private static final HttpLoggingInterceptor httpLog = new HttpLoggingInterceptor(); - - // This must be increased if there are test failures on slow CI servers :( - private static final Duration t = Duration.ofMillis(7500); - private static final OkHttpClient client = new OkHttpClient.Builder() .cache(cache) @@ -88,7 +87,6 @@ public class OkHttpResource extends BaseResource implements ReadableResource { private static final MediaTypeDetector mtd = new MediaTypeDetector(); public static class Provider implements ResourceProvider { - @Override public @Nullable Resource getResource(URI uri) { if (uri.getScheme().startsWith("http")) { @@ -113,7 +111,7 @@ private static MediaType mediaType(String url) { throw new IllegalArgumentException(unsuccessfulMessage(url, response)); var mt = response.body().contentType(); if (mt != null) { - return mtd.detect(mt.toString(), null, URI.create(url)); + return mtd.overwrite(URI.create(url), okToGuavaMediaType(mt)); } else { throw new IllegalStateException("Success, but no Content-Type header: " + url); } @@ -123,6 +121,11 @@ private static MediaType mediaType(String url) { } } + private static MediaType okToGuavaMediaType(okhttp3.MediaType okMediaType) { + // TODO Optimize? + return MediaType.parse(okMediaType.toString()); + } + @Override // TODO Re-design to fix resource leak... as-is, if you call this but then never call // the returned ByteSource's openStream(), then the OkHttp Response will never be closed! :(