Skip to content

Commit

Permalink
clean(core): Switch OkHttpResource to use overwrite() instead of dete…
Browse files Browse the repository at this point in the history
…ct()
  • Loading branch information
vorburger committed Oct 25, 2024
1 parent 677e8c9 commit 7b1afb0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
8 changes: 2 additions & 6 deletions java/dev/enola/common/io/resource/MediaTypeDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 9 additions & 6 deletions java/dev/enola/common/io/resource/OkHttpResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")) {
Expand All @@ -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);
}
Expand All @@ -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! :(
Expand Down

0 comments on commit 7b1afb0

Please sign in to comment.