Allow redirection of url to local resources
This commit is contained in:
parent
7ee4475917
commit
2d6d93041f
@ -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()
|
||||
|
@ -1,13 +1,17 @@
|
||||
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 {
|
||||
|
||||
@ -22,6 +26,22 @@ public class CachingDocumentLoaderTests {
|
||||
});
|
||||
}
|
||||
|
||||
@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(()->{
|
||||
|
Loading…
Reference in New Issue
Block a user