From 2d6d93041fe14f17319d2dda8ebc555a8fc86daf Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Thu, 24 Nov 2022 11:58:48 +0100 Subject: [PATCH] Allow redirection of url to local resources --- .../vc/util/CachingDocumentLoader.java | 35 +++++++++++++++++-- .../vc/util/CachingDocumentLoaderTests.java | 30 +++++++++++++--- 2 files changed, 58 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 42dc5cc..27d7b36 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 @@ -2,8 +2,10 @@ package org.oneedtech.inspect.vc.util; import java.io.InputStream; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.time.Duration; +import java.util.Map; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -32,10 +34,14 @@ public class CachingDocumentLoader extends ConfigurableDocumentLoader { public CachingDocumentLoader() { + this(null); + } + + public CachingDocumentLoader(Map localDomains) { super(); setEnableHttp(true); setEnableHttps(true); - setDefaultHttpLoader(new HttpLoader()); + setDefaultHttpLoader(new HttpLoader(localDomains)); } @Override @@ -49,16 +55,41 @@ public class CachingDocumentLoader extends ConfigurableDocumentLoader { } public class HttpLoader implements DocumentLoader { + final Map localDomains; + + public HttpLoader(Map localDomains) { + this.localDomains = localDomains; + } + @Override public Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError { - Tuple tpl = new Tuple<>(url.toASCIIString(), options); try { + // resolve url + URI resolvedUrl = resolve(url); + + Tuple tpl = new Tuple<>(resolvedUrl.toASCIIString(), options); + return documentCache.get(tpl); } catch (Exception e) { logger.error("documentCache not able to load {}", url); throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, e.getMessage()); } } + + /** + * Resolved given url. If the url is from one of local domain, a URL of the relative resource will be returned + * @throws URISyntaxException + */ + private URI resolve(URI url) throws URISyntaxException { + if (localDomains != null) { + URI base = url.resolve("/"); + if (localDomains.containsKey(base)) { + URL resource = Resources.getResource(localDomains.get(base) + "/" + base.relativize(url).toString()); + return resource.toURI(); + } + } + return url; + } } static final ImmutableMap bundled = ImmutableMap.builder() diff --git a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/CachingDocumentLoaderTests.java b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/CachingDocumentLoaderTests.java index b56542f..198e6ee 100644 --- a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/CachingDocumentLoaderTests.java +++ b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/CachingDocumentLoaderTests.java @@ -1,27 +1,47 @@ package org.oneedtech.inspect.vc.util; import java.net.URI; +import java.net.URL; +import java.util.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import com.apicatalog.jsonld.document.Document; +import com.apicatalog.jsonld.document.JsonDocument; import com.apicatalog.jsonld.loader.DocumentLoader; import com.apicatalog.jsonld.loader.DocumentLoaderOptions; +import com.google.common.io.Resources; public class CachingDocumentLoaderTests { @Test void testStaticCachedDocumentBundled() { Assertions.assertDoesNotThrow(()->{ - DocumentLoader loader = new CachingDocumentLoader(); + DocumentLoader loader = new CachingDocumentLoader(); for(String id : CachingDocumentLoader.bundled.keySet()) { Document doc = loader.loadDocument(new URI(id), new DocumentLoaderOptions()); - Assertions.assertNotNull(doc); - } + Assertions.assertNotNull(doc); + } }); } - + + @Test + void testLocalDomainCachedDocument() { + Assertions.assertDoesNotThrow(()->{ + Map localDomains = Map.of(new URI("http://example.org/"), "ob20"); + DocumentLoader loader = new CachingDocumentLoader(localDomains); + URI uri = new URI("http://example.org/basic-assertion.json"); + Document doc = loader.loadDocument(uri, new DocumentLoaderOptions()); + Assertions.assertNotNull(doc); + + // assert the returned document is the same as the local resource + URL resource = Resources.getResource("ob20/basic-assertion.json"); + JsonDocument resourceDocument = JsonDocument.of(resource.openStream()); + Assertions.assertEquals(resourceDocument.getJsonContent().toString(), doc.getJsonContent().toString()); + }); + } + @Test void testStaticCachedDocumentKey() { Assertions.assertDoesNotThrow(()->{ @@ -30,5 +50,5 @@ public class CachingDocumentLoaderTests { Document doc = loader.loadDocument(uri, new DocumentLoaderOptions()); Assertions.assertNotNull(doc); }); - } + } } \ No newline at end of file