-
Notifications
You must be signed in to change notification settings - Fork 129
/
JcrRdfTools.java
402 lines (346 loc) · 15.4 KB
/
JcrRdfTools.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
* Copyright 2014 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.kernel.impl.rdf;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.util.UUID.randomUUID;
import static javax.jcr.PropertyType.REFERENCE;
import static javax.jcr.PropertyType.UNDEFINED;
import static javax.jcr.PropertyType.URI;
import static javax.jcr.PropertyType.WEAKREFERENCE;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_PAIRTREE;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_RESOURCE;
import static org.fcrepo.kernel.RdfLexicon.JCR_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.isManagedPredicate;
import static org.fcrepo.kernel.impl.identifiers.NodeResourceConverter.nodeToResource;
import static org.fcrepo.kernel.impl.rdf.converters.PropertyConverter.getPropertyNameFromPredicate;
import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.getClosestExistingAncestor;
import static org.modeshape.jcr.api.JcrConstants.NT_FOLDER;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.NodeTypeTemplate;
import com.google.common.annotations.VisibleForTesting;
import com.hp.hpl.jena.rdf.model.AnonId;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.exception.MalformedRdfException;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.exception.ServerManagedPropertyException;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.rdf.converters.ValueConverter;
import org.fcrepo.kernel.impl.utils.NodePropertiesTools;
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
/**
* A set of helpful tools for converting JCR properties to RDF
*
* @author Chris Beer
* @since May 10, 2013
*/
public class JcrRdfTools {
private static final Logger LOGGER = getLogger(JcrRdfTools.class);
/**
* A map of JCR namespaces to Fedora's RDF namespaces
*/
public static BiMap<String, String> jcrNamespacesToRDFNamespaces =
ImmutableBiMap.of(JCR_NAMESPACE,
RdfLexicon.REPOSITORY_NAMESPACE);
/**
* A map of Fedora's RDF namespaces to the JCR equivalent
*/
public static BiMap<String, String> rdfNamespacesToJcrNamespaces =
jcrNamespacesToRDFNamespaces.inverse();
private final IdentifierConverter<Resource, FedoraResource> idTranslator;
private final ValueConverter valueConverter;
private Session session;
private final NodePropertiesTools nodePropertiesTools = new NodePropertiesTools();
@VisibleForTesting
protected JcrTools jcrTools = new JcrTools();
private final Map<AnonId, Resource> skolemizedBnodeMap;
private static final Model m = createDefaultModel();
/**
* Constructor with even more context.
*
* @param idTranslator
* @param session
*/
public JcrRdfTools(final IdentifierConverter<Resource, FedoraResource> idTranslator,
final Session session) {
this.idTranslator = idTranslator;
this.session = session;
this.valueConverter = new ValueConverter(session, idTranslator);
this.skolemizedBnodeMap = new HashMap<>();
}
/**
* Convert a Fedora RDF Namespace into its JCR equivalent
*
* @param rdfNamespaceUri a namespace from an RDF document
* @return the JCR namespace, or the RDF namespace if no matching JCR
* namespace is found
*/
public static String getJcrNamespaceForRDFNamespace(
final String rdfNamespaceUri) {
if (rdfNamespacesToJcrNamespaces.containsKey(rdfNamespaceUri)) {
return rdfNamespacesToJcrNamespaces.get(rdfNamespaceUri);
}
return rdfNamespaceUri;
}
/**
* Convert a JCR namespace into an RDF namespace fit for downstream
* consumption.
*
* @param jcrNamespaceUri a namespace from the JCR NamespaceRegistry
* @return an RDF namespace for downstream consumption.
*/
public static String getRDFNamespaceForJcrNamespace(
final String jcrNamespaceUri) {
if (jcrNamespacesToRDFNamespaces.containsKey(jcrNamespaceUri)) {
return jcrNamespacesToRDFNamespaces.get(jcrNamespaceUri);
}
return jcrNamespaceUri;
}
/**
* Create a JCR value from an RDFNode for a given JCR property
* @param node the JCR node we want a property for
* @param data an RDF Node (possibly with a DataType)
* @param propertyName name of the property to populate (used to use the right type for the value)
* @return
* @throws RepositoryException
*/
public Value createValue(final Node node,
final RDFNode data,
final String propertyName) throws RepositoryException {
final ValueFactory valueFactory = node.getSession().getValueFactory();
return createValue(valueFactory, data, nodePropertiesTools.getPropertyType(node, propertyName));
}
/**
* Create a JCR value from an RDF node with the given JCR type
* @param valueFactory
* @param data
* @param type
* @return created value
* @throws RepositoryException
*/
public Value createValue(final ValueFactory valueFactory, final RDFNode data, final int type)
throws RepositoryException {
assert (valueFactory != null);
if (type == UNDEFINED) {
return valueConverter.reverse().convert(data);
} else if (data.isURIResource()
&& (type == REFERENCE || type == WEAKREFERENCE)) {
// reference to another node (by path)
try {
final Node nodeFromGraphSubject = idTranslator.convert(data.asResource()).getNode();
return valueFactory.createValue(nodeFromGraphSubject,
type == WEAKREFERENCE);
} catch (final RepositoryRuntimeException e) {
throw new MalformedRdfException("Unable to find referenced node", e);
}
} else if (!data.isURIResource() && (type == REFERENCE || type == WEAKREFERENCE)) {
throw new ValueFormatException("Reference properties can only refer to URIs, not literals");
} else if (type == URI) {
// some random opaque URI
return valueFactory.createValue(data.toString(), PropertyType.URI);
} else {
LOGGER.debug("Using default JCR value creation for RDF literal: {}",
data);
return valueFactory.createValue(data.asLiteral().getString(), type);
}
}
/**
* Add a mixin to a node
* @param resource
* @param mixinResource
* @param namespaces
* @throws RepositoryException
*/
public void addMixin(final FedoraResource resource,
final Resource mixinResource,
final Map<String,String> namespaces)
throws RepositoryException {
final Node node = resource.getNode();
final Session session = node.getSession();
final String mixinName = getPropertyNameFromPredicate(node, mixinResource, namespaces);
if (!repositoryHasType(session, mixinName)) {
final NodeTypeManager mgr = session.getWorkspace().getNodeTypeManager();
final NodeTypeTemplate type = mgr.createNodeTypeTemplate();
type.setName(mixinName);
type.setMixin(true);
type.setQueryable(true);
mgr.registerNodeType(type, false);
}
if (node.isNodeType(mixinName)) {
LOGGER.trace("Subject {} is already a {}; skipping", node, mixinName);
return;
}
if (node.canAddMixin(mixinName)) {
LOGGER.debug("Adding mixin: {} to node: {}.", mixinName, node.getPath());
node.addMixin(mixinName);
} else {
throw new MalformedRdfException("Could not persist triple containing type assertion: "
+ mixinResource.toString()
+ " because no such mixin/type can be added to this node: "
+ node.getPath() + "!");
}
}
/**
* Add property to a node
* @param resource
* @param predicate
* @param value
* @param namespaces
* @throws RepositoryException
*/
public void addProperty(final FedoraResource resource,
final com.hp.hpl.jena.rdf.model.Property predicate,
final RDFNode value,
final Map<String,String> namespaces) throws RepositoryException {
final Node node = resource.getNode();
if (isManagedPredicate.apply(predicate)) {
throw new ServerManagedPropertyException("Could not persist triple containing predicate "
+ predicate.toString()
+ " to node "
+ node.getPath());
}
final String propertyName =
getPropertyNameFromPredicate(node, predicate, namespaces);
final Value v = createValue(node, value, propertyName);
nodePropertiesTools.appendOrReplaceNodeProperty(idTranslator, node, propertyName, v);
}
protected boolean repositoryHasType(final Session session, final String mixinName) throws RepositoryException {
return session.getWorkspace().getNodeTypeManager().hasNodeType(mixinName);
}
/**
* Remove a mixin from a node
* @param resource
* @param mixinResource
* @param nsPrefixMap
* @throws RepositoryException
*/
public void removeMixin(final FedoraResource resource,
final Resource mixinResource,
final Map<String, String> nsPrefixMap) throws RepositoryException {
final Node node = resource.getNode();
final String mixinName = getPropertyNameFromPredicate(node, mixinResource, nsPrefixMap);
if (repositoryHasType(session, mixinName) && node.isNodeType(mixinName)) {
node.removeMixin(mixinName);
}
}
/**
* Remove a property from a node
* @param resource
* @param predicate
* @param objectNode
* @param nsPrefixMap
* @throws RepositoryException
*/
public void removeProperty(final FedoraResource resource,
final com.hp.hpl.jena.rdf.model.Property predicate,
final RDFNode objectNode,
final Map<String, String> nsPrefixMap) throws RepositoryException {
final Node node = resource.getNode();
final String propertyName = getPropertyNameFromPredicate(node, predicate, nsPrefixMap);
if (isManagedPredicate.apply(predicate)) {
throw new ServerManagedPropertyException("Could not remove triple containing predicate "
+ predicate.toString()
+ " to node "
+ node.getPath());
}
// if the property doesn't exist, we don't need to worry about it.
if (node.hasProperty(propertyName)) {
final Value v = createValue(node, objectNode, propertyName);
nodePropertiesTools.removeNodeProperty(idTranslator, node, propertyName, v);
}
}
/**
* Convert an external statement into a persistable statement by skolemizing
* blank nodes, creating hash-uri subjects, etc
*
* @param idTranslator
* @param t
* @return
* @throws RepositoryException
*/
public Statement skolemize(final IdentifierConverter<Resource, FedoraResource> idTranslator, final Statement t)
throws RepositoryException {
Statement skolemized = t;
if (t.getSubject().isAnon()) {
skolemized = m.createStatement(getSkolemizedResource(idTranslator, skolemized.getSubject()),
t.getPredicate(),
t.getObject());
} else if (idTranslator.inDomain(t.getSubject()) && t.getSubject().getURI().contains("#")) {
findOrCreateHashUri(idTranslator, t.getSubject());
}
if (t.getObject().isAnon()) {
skolemized = skolemized.changeObject(getSkolemizedResource(idTranslator, t.getObject()));
} else if (t.getObject().isResource()
&& idTranslator.inDomain(t.getObject().asResource())
&& t.getObject().asResource().getURI().contains("#")) {
findOrCreateHashUri(idTranslator, t.getObject().asResource());
}
return skolemized;
}
private void findOrCreateHashUri(final IdentifierConverter<Resource, FedoraResource> idTranslator,
final Resource s) throws RepositoryException {
final String absPath = idTranslator.asString(s);
if (!absPath.isEmpty() && !session.nodeExists(absPath)) {
final Node closestExistingAncestor = getClosestExistingAncestor(session, absPath);
final Node orCreateNode = jcrTools.findOrCreateNode(session, absPath, NT_FOLDER);
orCreateNode.addMixin(FEDORA_RESOURCE);
final Node parent = orCreateNode.getParent();
if (!parent.getName().equals("#")) {
throw new AssertionError("Hash URI resource created with too much hierarchy: " + s);
}
// We require the closest node to be either "#" resource, or its parent.
if (!parent.equals(closestExistingAncestor)
&& !parent.getParent().equals(closestExistingAncestor)) {
throw new PathNotFoundException("Unexpected request to create new resource " + s);
}
if (parent.isNew()) {
parent.addMixin(FEDORA_PAIRTREE);
}
}
}
private Resource getSkolemizedResource(final IdentifierConverter<Resource, FedoraResource> idTranslator,
final RDFNode resource) throws RepositoryException {
final AnonId id = resource.asResource().getId();
if (!skolemizedBnodeMap.containsKey(id)) {
final Node orCreateNode = jcrTools.findOrCreateNode(session, skolemizedId());
orCreateNode.addMixin("fedora:blanknode");
final Resource skolemizedSubject = nodeToResource(idTranslator).convert(orCreateNode);
skolemizedBnodeMap.put(id, skolemizedSubject);
}
return skolemizedBnodeMap.get(id);
}
private String skolemizedId() {
return "/.well-known/genid/" + randomUUID().toString();
}
}