From 19c97f29376ce2a60e0f7e9321d4abef33dffd10 Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Wed, 23 Nov 2022 17:18:02 +0100 Subject: [PATCH] Adap CachingDocumentLoader to ConfigurableDocumentLoader --- .../vc/util/CachingDocumentLoader.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/CachingDocumentLoader.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/CachingDocumentLoader.java index 6db88fc..dee682d 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/CachingDocumentLoader.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/CachingDocumentLoader.java @@ -21,21 +21,43 @@ import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; import com.google.common.io.Resources; +import foundation.identity.jsonld.ConfigurableDocumentLoader; + /** * A com.apicatalog DocumentLoader with a threadsafe static cache. * * @author mgylling */ -public class CachingDocumentLoader implements DocumentLoader { +public class CachingDocumentLoader extends ConfigurableDocumentLoader { + + + public CachingDocumentLoader() { + super(); + setEnableHttp(true); + setEnableHttps(true); + setDefaultHttpLoader(new HttpLoader()); + } @Override public Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError { - Tuple tpl = new Tuple<>(url.toASCIIString(), options); - try { - return documentCache.get(tpl); - } catch (Exception e) { + Document document = super.loadDocument(url, options); + if (document == null) { logger.error("documentCache not able to load {}", url); - throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, e.getMessage()); + throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT); + } + return document; + } + + public class HttpLoader implements DocumentLoader { + @Override + public Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError { + Tuple tpl = new Tuple<>(url.toASCIIString(), options); + try { + return documentCache.get(tpl); + } catch (Exception e) { + logger.error("documentCache not able to load {}", url); + throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, e.getMessage()); + } } } @@ -62,7 +84,7 @@ public class CachingDocumentLoader implements DocumentLoader { .initialCapacity(32).maximumSize(64).expireAfterAccess(Duration.ofHours(24)) .build(new CacheLoader, Document>() { public Document load(final Tuple id) throws Exception { - try (InputStream is = bundled.keySet().contains(id.t1) + try (InputStream is = bundled.containsKey(id.t1) ? bundled.get(id.t1).openStream() : new URI(id.t1).toURL().openStream();) { return JsonDocument.of(is);