Allow redirection of url to local resources

This commit is contained in:
Xavi Aracil 2022-11-24 11:58:48 +01:00
parent 7ee4475917
commit 2d6d93041f
2 changed files with 58 additions and 7 deletions

View File

@ -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<URI, String> 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<URI, String> localDomains;
public HttpLoader(Map<URI, String> localDomains) {
this.localDomains = localDomains;
}
@Override
public Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError {
Tuple<String, DocumentLoaderOptions> tpl = new Tuple<>(url.toASCIIString(), options);
try {
// resolve url
URI resolvedUrl = resolve(url);
Tuple<String, DocumentLoaderOptions> 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<String, URL> bundled = ImmutableMap.<String, URL>builder()

View File

@ -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<URI, String> 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);
});
}
}
}