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.io.InputStream;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -32,10 +34,14 @@ public class CachingDocumentLoader extends ConfigurableDocumentLoader {
|
|||||||
|
|
||||||
|
|
||||||
public CachingDocumentLoader() {
|
public CachingDocumentLoader() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CachingDocumentLoader(Map<URI, String> localDomains) {
|
||||||
super();
|
super();
|
||||||
setEnableHttp(true);
|
setEnableHttp(true);
|
||||||
setEnableHttps(true);
|
setEnableHttps(true);
|
||||||
setDefaultHttpLoader(new HttpLoader());
|
setDefaultHttpLoader(new HttpLoader(localDomains));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -49,16 +55,41 @@ public class CachingDocumentLoader extends ConfigurableDocumentLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class HttpLoader implements DocumentLoader {
|
public class HttpLoader implements DocumentLoader {
|
||||||
|
final Map<URI, String> localDomains;
|
||||||
|
|
||||||
|
public HttpLoader(Map<URI, String> localDomains) {
|
||||||
|
this.localDomains = localDomains;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError {
|
public Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError {
|
||||||
Tuple<String, DocumentLoaderOptions> tpl = new Tuple<>(url.toASCIIString(), options);
|
|
||||||
try {
|
try {
|
||||||
|
// resolve url
|
||||||
|
URI resolvedUrl = resolve(url);
|
||||||
|
|
||||||
|
Tuple<String, DocumentLoaderOptions> tpl = new Tuple<>(resolvedUrl.toASCIIString(), options);
|
||||||
|
|
||||||
return documentCache.get(tpl);
|
return documentCache.get(tpl);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("documentCache not able to load {}", url);
|
logger.error("documentCache not able to load {}", url);
|
||||||
throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, e.getMessage());
|
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()
|
static final ImmutableMap<String, URL> bundled = ImmutableMap.<String, URL>builder()
|
||||||
|
@ -1,27 +1,47 @@
|
|||||||
package org.oneedtech.inspect.vc.util;
|
package org.oneedtech.inspect.vc.util;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import com.apicatalog.jsonld.document.Document;
|
import com.apicatalog.jsonld.document.Document;
|
||||||
|
import com.apicatalog.jsonld.document.JsonDocument;
|
||||||
import com.apicatalog.jsonld.loader.DocumentLoader;
|
import com.apicatalog.jsonld.loader.DocumentLoader;
|
||||||
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
|
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
|
||||||
|
import com.google.common.io.Resources;
|
||||||
|
|
||||||
public class CachingDocumentLoaderTests {
|
public class CachingDocumentLoaderTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testStaticCachedDocumentBundled() {
|
void testStaticCachedDocumentBundled() {
|
||||||
Assertions.assertDoesNotThrow(()->{
|
Assertions.assertDoesNotThrow(()->{
|
||||||
DocumentLoader loader = new CachingDocumentLoader();
|
DocumentLoader loader = new CachingDocumentLoader();
|
||||||
for(String id : CachingDocumentLoader.bundled.keySet()) {
|
for(String id : CachingDocumentLoader.bundled.keySet()) {
|
||||||
Document doc = loader.loadDocument(new URI(id), new DocumentLoaderOptions());
|
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
|
@Test
|
||||||
void testStaticCachedDocumentKey() {
|
void testStaticCachedDocumentKey() {
|
||||||
Assertions.assertDoesNotThrow(()->{
|
Assertions.assertDoesNotThrow(()->{
|
||||||
@ -30,5 +50,5 @@ public class CachingDocumentLoaderTests {
|
|||||||
Document doc = loader.loadDocument(uri, new DocumentLoaderOptions());
|
Document doc = loader.loadDocument(uri, new DocumentLoaderOptions());
|
||||||
Assertions.assertNotNull(doc);
|
Assertions.assertNotNull(doc);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user