Adap CachingDocumentLoader to ConfigurableDocumentLoader

This commit is contained in:
Xavi Aracil 2022-11-23 17:18:02 +01:00
parent 7beb69f87b
commit 19c97f2937

View File

@ -21,13 +21,34 @@ 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 {
Document document = super.loadDocument(url, options);
if (document == null) {
logger.error("documentCache not able to load {}", url);
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<String, DocumentLoaderOptions> tpl = new Tuple<>(url.toASCIIString(), options);
@ -38,6 +59,7 @@ public class CachingDocumentLoader implements DocumentLoader {
throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, e.getMessage());
}
}
}
static final ImmutableMap<String, URL> bundled = ImmutableMap.<String, URL>builder()
.put("https://purl.imsglobal.org/spec/clr/v2p0/context.json",Resources.getResource("contexts/clr-v2p0.json"))
@ -62,7 +84,7 @@ public class CachingDocumentLoader implements DocumentLoader {
.initialCapacity(32).maximumSize(64).expireAfterAccess(Duration.ofHours(24))
.build(new CacheLoader<Tuple<String, DocumentLoaderOptions>, Document>() {
public Document load(final Tuple<String, DocumentLoaderOptions> 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);