Skip to content

Commit 8563132

Browse files
authored
GH-5080 Support for using a user provided document loader with HASMAC JSON-LD (#5162)
2 parents 736e4b1 + 2582602 commit 8563132

13 files changed

Lines changed: 1091 additions & 62 deletions

File tree

core/rio/api/src/main/java/org/eclipse/rdf4j/rio/RioConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
package org.eclipse.rdf4j.rio;
1212

1313
import java.io.Serializable;
14+
import java.util.Collections;
15+
import java.util.HashMap;
16+
import java.util.HashSet;
17+
import java.util.Map;
1418
import java.util.Objects;
1519
import java.util.concurrent.ConcurrentHashMap;
1620
import java.util.concurrent.ConcurrentMap;
@@ -152,4 +156,8 @@ public RioConfig useDefaults() {
152156
systemPropertyCache.clear();
153157
return this;
154158
}
159+
160+
public Map<RioSetting<Object>, Object> getSettings() {
161+
return Collections.unmodifiableMap(new HashMap<>(settings));
162+
}
155163
}

core/rio/api/src/main/java/org/eclipse/rdf4j/rio/helpers/JSONLDSettings.java

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@ public class JSONLDSettings {
3535
* Can be overridden by setting system property {@code org.eclipse.rdf4j.rio.jsonld.compact_arrays}.
3636
*
3737
* @see <a href="http://json-ld.org/spec/latest/json-ld-api/#data-structures">JSONLD Data Structures</a>
38-
*
3938
*/
4039
public static final BooleanRioSetting COMPACT_ARRAYS = new BooleanRioSetting(
4140
"org.eclipse.rdf4j.rio.jsonld.compact_arrays", "Compact arrays", Boolean.TRUE);
4241

4342
/**
4443
* If specified, it is used to retrieve remote documents and contexts; otherwise the processor's built-in loader is
4544
* used.
46-
*
4745
*/
4846
public static final RioSetting<DocumentLoader> DOCUMENT_LOADER = new ClassRioSetting<>(
4947
"org.eclipse.rdf4j.rio.jsonld.document_loader", "Document loader", null);
@@ -66,7 +64,6 @@ public class JSONLDSettings {
6664

6765
/**
6866
* The JSON-LD processor will throw an exception if a warning is encountered during processing.
69-
*
7067
*/
7168
public static final BooleanRioSetting EXCEPTION_ON_WARNING = new BooleanRioSetting(
7269
"org.eclipse.rdf4j.rio.jsonld.exception_on_warning",
@@ -83,7 +80,6 @@ public class JSONLDSettings {
8380
* Can be overridden by setting system property {@code org.eclipse.rdf4j.rio.jsonld.optimize}.
8481
*
8582
* @see <a href="http://json-ld.org/spec/latest/json-ld-api/#data-structures">JSONLD Data Structures</a>
86-
*
8783
*/
8884
public static final BooleanRioSetting OPTIMIZE = new BooleanRioSetting("org.eclipse.rdf4j.rio.jsonld.optimize",
8985
"Optimize output", Boolean.FALSE);
@@ -99,7 +95,6 @@ public class JSONLDSettings {
9995
* Can be overridden by setting system property {@code org.eclipse.rdf4j.rio.jsonld.produce_generalized_rdf}.
10096
*
10197
* @see <a href="http://json-ld.org/spec/latest/json-ld-api/#data-structures">JSONLD Data Structures</a>
102-
*
10398
*/
10499
public static final BooleanRioSetting PRODUCE_GENERALIZED_RDF = new BooleanRioSetting(
105100
"org.eclipse.rdf4j.rio.jsonld.produce_generalized_rdf", "Produce generalized RDF", Boolean.FALSE);
@@ -114,7 +109,6 @@ public class JSONLDSettings {
114109
* Can be overridden by setting system property {@code org.eclipse.rdf4j.rio.jsonld.use_native_types}.
115110
*
116111
* @see <a href="http://json-ld.org/spec/latest/json-ld-api/#data-structures">JSONLD Data Structures</a>
117-
*
118112
*/
119113
public static final BooleanRioSetting USE_NATIVE_TYPES = new BooleanRioSetting(
120114
"org.eclipse.rdf4j.rio.jsonld.use_native_types", "Use Native JSON Types", Boolean.FALSE);
@@ -128,7 +122,6 @@ public class JSONLDSettings {
128122
* Can be overridden by setting system property {@code org.eclipse.rdf4j.rio.jsonld.use_rdf_type}.
129123
*
130124
* @see <a href="http://json-ld.org/spec/latest/json-ld-api/#data-structures">JSONLD Data Structures</a>
131-
*
132125
*/
133126
public static final BooleanRioSetting USE_RDF_TYPE = new BooleanRioSetting(
134127
"org.eclipse.rdf4j.rio.jsonld.use_rdf_type", "Use RDF Type", Boolean.FALSE);
@@ -139,7 +132,6 @@ public class JSONLDSettings {
139132
* Defaults to {@link JSONLDMode#EXPAND} to provide maximum RDF compatibility.
140133
*
141134
* @see <a href="http://json-ld.org/spec/latest/json-ld-api/#features">JSONLD Features</a>
142-
*
143135
*/
144136
public static final RioSetting<JSONLDMode> JSONLD_MODE = new RioSettingImpl<>(
145137
"org.eclipse.rdf4j.rio.jsonld_mode", "JSONLD Mode", JSONLDMode.EXPAND);
@@ -150,7 +142,6 @@ public class JSONLDSettings {
150142
* Default to false
151143
* <p>
152144
* Can be overridden by setting system property {@code org.eclipse.rdf4j.rio.jsonld.hierarchical_view}.
153-
*
154145
*/
155146
public static final BooleanRioSetting HIERARCHICAL_VIEW = new BooleanRioSetting(
156147
"org.eclipse.rdf4j.rio.jsonld.hierarchical_view", "Hierarchical representation of the JSON", Boolean.FALSE);
@@ -161,38 +152,88 @@ public class JSONLDSettings {
161152
* array of the desired values.
162153
* <p>
163154
* Default:
164-
* {@code Set.of("http://www.w3.org/ns/anno.jsonld", "http://www.w3.org/ns/activitystreams.jsonld", "http://www.w3.org/ns/ldp.jsonld", "http://www.w3.org/ns/oa.jsonld", "http://www.w3.org/ns/hydra/context.jsonld", "http://schema.org/", "https://w3id.org/security/v1", "https://w3c.github.io/json-ld-rc/context.jsonld", "https://www.w3.org/2018/credentials/v1", "https://health-lifesci.schema.org/", "https://auto.schema.org/", "https://bib.schema.org/", "http://xmlns.com/foaf/spec/index.jsonld", "https://pending.schema.org/", "https://schema.org/", "https://schema.org/docs/jsonldcontext.jsonld", "https://schema.org/version/latest/schemaorg-current-https.jsonld", "https://schema.org/version/latest/schemaorg-all-http.jsonld", "https://schema.org/version/latest/schemaorg-all-https.jsonld", "https://schema.org/version/latest/schemaorg-current-http.jsonld", "https://schema.org/version/latest/schemaorg-all.jsonld", "https://schema.org/version/latest/schemaorg-current.jsonld", "https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld", "https://geojson.org/geojson-ld/geojson-context.jsonld", "https://www.w3.org/2019/wot/td/v1");
165-
*
155+
* {@code Set.of("http://www.w3.org/ns/anno.jsonld", "https://www.w3.org/ns/anno.jsonld", "http://www.w3.org/ns/anno", "https://www.w3.org/ns/anno", "http://www.w3.org/ns/activitystreams.jsonld", "https://www.w3.org/ns/activitystreams.jsonld", "http://www.w3.org/ns/activitystreams", "https://www.w3.org/ns/activitystreams", "http://www.w3.org/ns/ldp.jsonld", "https://www.w3.org/ns/ldp.jsonld", "http://www.w3.org/ns/ldp", "https://www.w3.org/ns/ldp", "http://www.w3.org/ns/oa.jsonld", "https://www.w3.org/ns/oa.jsonld", "http://www.w3.org/ns/oa", "https://www.w3.org/ns/oa", "http://www.w3.org/ns/hydra/context.jsonld", "https://www.w3.org/ns/hydra/context.jsonld", "http://www.w3.org/ns/hydra/context", "https://www.w3.org/ns/hydra/context", "http://www.w3.org/2018/credentials/v1.jsonld", "https://www.w3.org/2018/credentials/v1.jsonld", "http://www.w3.org/2018/credentials/v1", "https://www.w3.org/2018/credentials/v1", "http://www.w3.org/2019/wot/td/v1.jsonld", "https://www.w3.org/2019/wot/td/v1.jsonld", "http://www.w3.org/2019/wot/td/v1", "https://www.w3.org/2019/wot/td/v1", "http://w3c.github.io/json-ld-rc/context.jsonld", "https://w3c.github.io/json-ld-rc/context.jsonld", "http://schema.org/", "https://schema.org/", "http://health-lifesci.schema.org/", "https://health-lifesci.schema.org/", "http://auto.schema.org/", "https://auto.schema.org/", "http://bib.schema.org/", "https://bib.schema.org/", "http://pending.schema.org/", "https://pending.schema.org/", "http://schema.org", "https://schema.org", "http://health-lifesci.schema.org", "https://health-lifesci.schema.org", "http://auto.schema.org", "https://auto.schema.org", "http://bib.schema.org", "https://bib.schema.org", "http://pending.schema.org", "https://pending.schema.org", "http://schema.org/docs/jsonldcontext.jsonld", "https://schema.org/docs/jsonldcontext.jsonld", "http://schema.org/version/latest/schemaorg-current-https.jsonld", "https://schema.org/version/latest/schemaorg-current-https.jsonld", "http://schema.org/version/latest/schemaorg-all-http.jsonld", "https://schema.org/version/latest/schemaorg-all-http.jsonld", "http://schema.org/version/latest/schemaorg-all-https.jsonld", "https://schema.org/version/latest/schemaorg-all-https.jsonld", "http://schema.org/version/latest/schemaorg-current-http.jsonld", "https://schema.org/version/latest/schemaorg-current-http.jsonld", "http://schema.org/version/latest/schemaorg-all.jsonld", "https://schema.org/version/latest/schemaorg-all.jsonld", "http://schema.org/version/latest/schemaorg-current.jsonld", "https://schema.org/version/latest/schemaorg-current.jsonld", "http://project-open-data.cio.gov/v1.1/schema/catalog.jsonld", "https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld", "http://geojson.org/geojson-ld/geojson-context.jsonld", "https://geojson.org/geojson-ld/geojson-context.jsonld", "http://w3id.org/security/v1", "https://w3id.org/security/v1", "http://xmlns.com/foaf/spec/index.jsonld", "https://xmlns.com/foaf/spec/index.jsonld", "http://xmlns.com/foaf/spec/", "https://xmlns.com/foaf/spec/", "http://xmlns.com/foaf/spec", "https://xmlns.com/foaf/spec")}
166156
*/
167157
public static final SetRioSetting<String> WHITELIST = new SetRioSetting<>(
168158
"org.eclipse.rdf4j.rio.jsonld_whitelist",
169159
"Whitelist of remote/local resources that the JSON-LD parser can retrieve. Set of URIs as strings.",
170160
Set.of(
171161
"http://www.w3.org/ns/anno.jsonld",
162+
"https://www.w3.org/ns/anno.jsonld",
163+
"http://www.w3.org/ns/anno",
164+
"https://www.w3.org/ns/anno",
172165
"http://www.w3.org/ns/activitystreams.jsonld",
166+
"https://www.w3.org/ns/activitystreams.jsonld",
167+
"http://www.w3.org/ns/activitystreams",
168+
"https://www.w3.org/ns/activitystreams",
173169
"http://www.w3.org/ns/ldp.jsonld",
170+
"https://www.w3.org/ns/ldp.jsonld",
171+
"http://www.w3.org/ns/ldp",
172+
"https://www.w3.org/ns/ldp",
174173
"http://www.w3.org/ns/oa.jsonld",
174+
"https://www.w3.org/ns/oa.jsonld",
175+
"http://www.w3.org/ns/oa",
176+
"https://www.w3.org/ns/oa",
175177
"http://www.w3.org/ns/hydra/context.jsonld",
176-
"http://schema.org/",
177-
"https://w3id.org/security/v1",
178-
"https://w3c.github.io/json-ld-rc/context.jsonld",
178+
"https://www.w3.org/ns/hydra/context.jsonld",
179+
"http://www.w3.org/ns/hydra/context",
180+
"https://www.w3.org/ns/hydra/context",
181+
"http://www.w3.org/2018/credentials/v1.jsonld",
182+
"https://www.w3.org/2018/credentials/v1.jsonld",
183+
"http://www.w3.org/2018/credentials/v1",
179184
"https://www.w3.org/2018/credentials/v1",
185+
"http://www.w3.org/2019/wot/td/v1.jsonld",
186+
"https://www.w3.org/2019/wot/td/v1.jsonld",
187+
"http://www.w3.org/2019/wot/td/v1",
188+
"https://www.w3.org/2019/wot/td/v1",
189+
"http://w3c.github.io/json-ld-rc/context.jsonld",
190+
"https://w3c.github.io/json-ld-rc/context.jsonld",
191+
"http://schema.org/",
192+
"https://schema.org/",
193+
"http://health-lifesci.schema.org/",
180194
"https://health-lifesci.schema.org/",
195+
"http://auto.schema.org/",
181196
"https://auto.schema.org/",
197+
"http://bib.schema.org/",
182198
"https://bib.schema.org/",
183-
"http://xmlns.com/foaf/spec/index.jsonld",
199+
"http://pending.schema.org/",
184200
"https://pending.schema.org/",
185-
"https://schema.org/",
201+
"http://schema.org",
202+
"https://schema.org",
203+
"http://health-lifesci.schema.org",
204+
"https://health-lifesci.schema.org",
205+
"http://auto.schema.org",
206+
"https://auto.schema.org",
207+
"http://bib.schema.org",
208+
"https://bib.schema.org",
209+
"http://pending.schema.org",
210+
"https://pending.schema.org",
211+
"http://schema.org/docs/jsonldcontext.jsonld",
186212
"https://schema.org/docs/jsonldcontext.jsonld",
213+
"http://schema.org/version/latest/schemaorg-current-https.jsonld",
187214
"https://schema.org/version/latest/schemaorg-current-https.jsonld",
215+
"http://schema.org/version/latest/schemaorg-all-http.jsonld",
188216
"https://schema.org/version/latest/schemaorg-all-http.jsonld",
217+
"http://schema.org/version/latest/schemaorg-all-https.jsonld",
189218
"https://schema.org/version/latest/schemaorg-all-https.jsonld",
219+
"http://schema.org/version/latest/schemaorg-current-http.jsonld",
190220
"https://schema.org/version/latest/schemaorg-current-http.jsonld",
221+
"http://schema.org/version/latest/schemaorg-all.jsonld",
191222
"https://schema.org/version/latest/schemaorg-all.jsonld",
223+
"http://schema.org/version/latest/schemaorg-current.jsonld",
192224
"https://schema.org/version/latest/schemaorg-current.jsonld",
225+
"http://project-open-data.cio.gov/v1.1/schema/catalog.jsonld",
193226
"https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld",
227+
"http://geojson.org/geojson-ld/geojson-context.jsonld",
194228
"https://geojson.org/geojson-ld/geojson-context.jsonld",
195-
"https://www.w3.org/2019/wot/td/v1"
229+
"http://w3id.org/security/v1",
230+
"https://w3id.org/security/v1",
231+
"http://xmlns.com/foaf/spec/index.jsonld",
232+
"https://xmlns.com/foaf/spec/index.jsonld",
233+
"http://xmlns.com/foaf/spec/",
234+
"https://xmlns.com/foaf/spec/",
235+
"http://xmlns.com/foaf/spec",
236+
"https://xmlns.com/foaf/spec"
196237
));
197238

198239
/**

core/rio/jsonld/src/main/java/org/eclipse/rdf4j/rio/jsonld/CachingDocumentLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class CachingDocumentLoader implements DocumentLoader {
3737
private static final LoadingCache<URI, Document> cache = CacheBuilder.newBuilder()
3838
.maximumSize(1000) // Maximum 1000 documents in cache
3939
.expireAfterWrite(1, TimeUnit.HOURS) // Expire after 1 hour
40-
.concurrencyLevel(8) // Optimize for 8 concurrent threads
40+
.concurrencyLevel(Runtime.getRuntime().availableProcessors())
4141
.build(new CacheLoader<>() {
4242
@Override
4343
public Document load(URI url) throws Exception {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Distribution License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/org/documents/edl-v10.php.
8+
*
9+
* SPDX-License-Identifier: BSD-3-Clause
10+
*******************************************************************************/
11+
package org.eclipse.rdf4j.rio.jsonld;
12+
13+
/**
14+
* Specifies constants to identify various modes that are relevant to JSONLD documents.
15+
*
16+
* @author Peter Ansell
17+
* @see <a href="http://json-ld.org/spec/latest/json-ld-api/#features">JSONLD Features</a>
18+
*
19+
*/
20+
public enum JSONLDMode {
21+
22+
EXPAND("Expansion", "http://json-ld.org/spec/latest/json-ld-api/index.html#expansion"),
23+
24+
COMPACT("Compaction", "http://json-ld.org/spec/latest/json-ld-api/index.html#compaction"),
25+
26+
FLATTEN("Flattening", "http://json-ld.org/spec/latest/json-ld-api/index.html#flattening"),
27+
28+
FRAME("Framing", "https://www.w3.org/TR/json-ld11-framing/"),
29+
30+
;
31+
32+
private final String label;
33+
34+
private final String reference;
35+
36+
JSONLDMode(String label, String reference) {
37+
this.label = label;
38+
this.reference = reference;
39+
}
40+
41+
/**
42+
* @return Returns the label.
43+
*/
44+
public String getLabel() {
45+
return label;
46+
}
47+
48+
/**
49+
* @return Returns the reference URL for the given mode.
50+
*/
51+
public String getReference() {
52+
return reference;
53+
}
54+
}

core/rio/jsonld/src/main/java/org/eclipse/rdf4j/rio/jsonld/JSONLDParser.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
*******************************************************************************/
1111
package org.eclipse.rdf4j.rio.jsonld;
1212

13-
import static org.eclipse.rdf4j.rio.helpers.JSONLDSettings.DOCUMENT_LOADER_CACHE;
14-
import static org.eclipse.rdf4j.rio.helpers.JSONLDSettings.SECURE_MODE;
15-
import static org.eclipse.rdf4j.rio.helpers.JSONLDSettings.WHITELIST;
16-
1713
import java.io.IOException;
1814
import java.io.InputStream;
1915
import java.io.Reader;
2016
import java.net.URI;
2117
import java.net.URISyntaxException;
2218
import java.util.Collection;
19+
import java.util.Map;
2320
import java.util.Optional;
2421
import java.util.Set;
2522
import java.util.function.BiConsumer;
@@ -35,10 +32,11 @@
3532
import org.eclipse.rdf4j.rio.RDFHandlerException;
3633
import org.eclipse.rdf4j.rio.RDFParseException;
3734
import org.eclipse.rdf4j.rio.RDFParser;
35+
import org.eclipse.rdf4j.rio.RioConfig;
3836
import org.eclipse.rdf4j.rio.RioSetting;
3937
import org.eclipse.rdf4j.rio.helpers.AbstractRDFParser;
4038
import org.eclipse.rdf4j.rio.helpers.BasicParserSettings;
41-
import org.eclipse.rdf4j.rio.helpers.JSONLDSettings;
39+
import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings;
4240
import org.slf4j.Logger;
4341
import org.slf4j.LoggerFactory;
4442

@@ -91,6 +89,13 @@ public Collection<RioSetting<?>> getSupportedSettings() {
9189

9290
result.add(JSONLDSettings.EXPAND_CONTEXT);
9391
result.add(JSONLDSettings.EXCEPTION_ON_WARNING);
92+
result.add(JSONLDSettings.SECURE_MODE);
93+
result.add(JSONLDSettings.WHITELIST);
94+
result.add(JSONLDSettings.DOCUMENT_LOADER);
95+
result.add(JSONLDSettings.DOCUMENT_LOADER_CACHE);
96+
result.add(org.eclipse.rdf4j.rio.helpers.JSONLDSettings.SECURE_MODE);
97+
result.add(org.eclipse.rdf4j.rio.helpers.JSONLDSettings.WHITELIST);
98+
result.add(org.eclipse.rdf4j.rio.helpers.JSONLDSettings.DOCUMENT_LOADER_CACHE);
9499

95100
return result;
96101
}
@@ -133,9 +138,9 @@ private void parse(InputStream in, Reader reader, String baseURI)
133138
BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES);
134139
}
135140

136-
boolean secureMode = getParserConfig().get(SECURE_MODE);
137-
Set<String> whitelist = getParserConfig().get(WHITELIST);
138-
boolean documentLoaderCache = getParserConfig().get(DOCUMENT_LOADER_CACHE);
141+
boolean secureMode = getParserConfig().get(JSONLDSettings.SECURE_MODE);
142+
Set<String> whitelist = getParserConfig().get(JSONLDSettings.WHITELIST);
143+
boolean documentLoaderCache = getParserConfig().get(JSONLDSettings.DOCUMENT_LOADER_CACHE);
139144

140145
JsonLdOptions opts = new JsonLdOptions();
141146
opts.setUriValidation(false);
@@ -144,8 +149,13 @@ private void parse(InputStream in, Reader reader, String baseURI)
144149
Document context = getParserConfig().get(JSONLDSettings.EXPAND_CONTEXT);
145150

146151
DocumentLoader defaultDocumentLoader = opts.getDocumentLoader();
147-
CachingDocumentLoader cachingDocumentLoader = new CachingDocumentLoader(secureMode, whitelist,
148-
documentLoaderCache);
152+
153+
DocumentLoader documentLoader;
154+
if (getParserConfig().get(JSONLDSettings.DOCUMENT_LOADER) == null) {
155+
documentLoader = new CachingDocumentLoader(secureMode, whitelist, documentLoaderCache);
156+
} else {
157+
documentLoader = getParserConfig().get(JSONLDSettings.DOCUMENT_LOADER);
158+
}
149159

150160
if (context != null) {
151161

@@ -162,14 +172,14 @@ private void parse(InputStream in, Reader reader, String baseURI)
162172
return context;
163173
}
164174

165-
return cachingDocumentLoader.loadDocument(uri, options);
175+
return documentLoader.loadDocument(uri, options);
166176
});
167177
}
168178

169179
}
170180

171-
if (secureMode && opts.getDocumentLoader() == defaultDocumentLoader) {
172-
opts.setDocumentLoader(cachingDocumentLoader);
181+
if (opts.getDocumentLoader() == defaultDocumentLoader) {
182+
opts.setDocumentLoader(documentLoader);
173183
}
174184

175185
if (baseURI != null && !baseURI.isEmpty()) {

0 commit comments

Comments
 (0)