Skip to content

Commit

Permalink
mask the jcr namespace behind our own.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed May 9, 2013
1 parent 85dc79e commit 2a81368
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
Expand Up @@ -99,7 +99,7 @@ public void testGetObjectGraph() throws Exception {
assertEquals(200, response.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(response.getEntity());

assertTrue("Didn't find an expected ntriple", compile("<info:fedora/objects/FedoraDescribeTestGraph> <http://www.jcp.org/jcr/1.0mixinTypes> \"fedora:object\" \\.",
assertTrue("Didn't find an expected ntriple", compile("<info:fedora/objects/FedoraDescribeTestGraph> <info:fedora/fedora-system:def/internal#mixinTypes> \"fedora:object\" \\.",
DOTALL).matcher(content).find());

logger.debug("Retrieved object graph:\n" + content);
Expand Down Expand Up @@ -128,7 +128,7 @@ public void testUpdateObjectGraphWithProblems() throws Exception {
new HttpPost(serverAddress + "objects/FedoraDescribeTestGraphUpdateBad");
getObjMethod.addHeader("Content-Type", "application/sparql-update");
BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream("INSERT { <info:fedora/objects/FedoraDescribeTestGraphUpdateBad> <http://www.jcp.org/jcr/1.0uuid> \"00e686e2-24d4-40c2-92ce-577c0165b158\" } WHERE {}\n".getBytes()));
e.setContent(new ByteArrayInputStream("INSERT { <info:fedora/objects/FedoraDescribeTestGraphUpdateBad> <info:fedora/fedora-system:def/internal#uuid> \"00e686e2-24d4-40c2-92ce-577c0165b158\" } WHERE {}\n".getBytes()));
getObjMethod.setEntity(e);
final HttpResponse response = client.execute(getObjMethod);
assertEquals(403, response.getStatusLine().getStatusCode());
Expand Down
12 changes: 9 additions & 3 deletions fcrepo-kernel/src/main/java/org/fcrepo/FedoraResource.java
Expand Up @@ -28,6 +28,7 @@
import java.util.Date;

import static org.fcrepo.services.ServiceHelpers.getObjectSize;
import static org.fcrepo.utils.FedoraTypesUtils.getRDFNamespaceForJcrNamespace;
import static org.fcrepo.utils.FedoraTypesUtils.map;
import static org.fcrepo.utils.FedoraTypesUtils.nodetype2name;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -165,7 +166,12 @@ public Model getPropertiesModel() throws RepositoryException {
final String nsURI = namespaceRegistry.getURI(prefix);
if (nsURI != null && !nsURI.equals("") &&
!prefix.equals("xmlns")) {
model.setNsPrefix(prefix, nsURI);

if (prefix.equals("jcr")) {
model.setNsPrefix("fedora-internal", getRDFNamespaceForJcrNamespace(nsURI));
} else {
model.setNsPrefix(prefix, getRDFNamespaceForJcrNamespace(nsURI));
}
}
}

Expand All @@ -179,12 +185,12 @@ public Model getPropertiesModel() throws RepositoryException {
final Value[] values = property.getValues();

for(Value v : values) {
model.add(subject, ResourceFactory.createProperty(nsProperty.getNamespaceURI(), nsProperty.getLocalName()), v.getString());
model.add(subject, ResourceFactory.createProperty(getRDFNamespaceForJcrNamespace(nsProperty.getNamespaceURI()), nsProperty.getLocalName()), v.getString());
}

} else {
final Value value = property.getValue();
model.add(subject, ResourceFactory.createProperty(nsProperty.getNamespaceURI(), nsProperty.getLocalName()), value.getString());
model.add(subject, ResourceFactory.createProperty(getRDFNamespaceForJcrNamespace(nsProperty.getNamespaceURI()), nsProperty.getLocalName()), value.getString());
}

}
Expand Down
24 changes: 24 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/utils/FedoraTypesUtils.java
Expand Up @@ -20,6 +20,8 @@

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import org.modeshape.jcr.JcrValueFactory;
import org.modeshape.jcr.value.binary.StrategyHint;

Expand All @@ -31,6 +33,28 @@
*/
public abstract class FedoraTypesUtils {

public static BiMap<String, String> jcrNamespacesToRDFNamespaces = ImmutableBiMap.of(
"http://www.jcp.org/jcr/1.0", "info:fedora/fedora-system:def/internal#"
);

public static BiMap<String, String> rdfNamespacesToJcrNamespaces = jcrNamespacesToRDFNamespaces.inverse();

public static String getJcrNamespaceForRDFNamespace(final String rdfNamespaceUri) {
if (rdfNamespacesToJcrNamespaces.containsKey(rdfNamespaceUri)) {
return rdfNamespacesToJcrNamespaces.get(rdfNamespaceUri);
} else {
return rdfNamespaceUri;
}
}

public static String getRDFNamespaceForJcrNamespace(final String jcrNamespaceUri) {
if (jcrNamespacesToRDFNamespaces.containsKey(jcrNamespaceUri)) {
return jcrNamespacesToRDFNamespaces.get(jcrNamespaceUri);
} else {
return jcrNamespaceUri;
}
}

/**
* Predicate for determining whether this {@link Node} is a Fedora object.
*/
Expand Down
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Collections;

import static org.fcrepo.utils.FedoraTypesUtils.getJcrNamespaceForRDFNamespace;
import static org.slf4j.LoggerFactory.getLogger;

public class JcrPropertyStatementListener extends StatementListener {
Expand Down Expand Up @@ -69,7 +70,7 @@ public void addedStatement( Statement s ) {
Collections.addAll(newValues, node.getProperty(propertyName).getValues());
newValues.add(newValue);

property.setValue((Value[]) newValues.toArray(new Value[0]));
property.setValue((Value[]) newValues.toArray(new Value[newValues.size()]));
} else {
// or we'll just overwrite it
property.setValue(newValue);
Expand Down Expand Up @@ -265,12 +266,14 @@ private String getPropertyNameFromPredicate(com.hp.hpl.jena.rdf.model.Property p

final String prefix;

final String namespace = getJcrNamespaceForRDFNamespace(predicate.getNameSpace());

final NamespaceRegistry namespaceRegistry = getNamespaceRegistry();

if (namespaceRegistry.isRegisteredUri(predicate.getNameSpace())) {
prefix = namespaceRegistry.getPrefix(predicate.getNameSpace());
if (namespaceRegistry.isRegisteredUri(namespace)) {
prefix = namespaceRegistry.getPrefix(namespace);
} else {
prefix = namespaceRegistry.registerNamespace(predicate.getNameSpace());
prefix = namespaceRegistry.registerNamespace(namespace);
}

final String localName = predicate.getLocalName();
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import static java.util.regex.Pattern.compile;
import static org.junit.Assert.*;

@ContextConfiguration({"/spring-test/repo.xml"})
Expand Down Expand Up @@ -62,6 +63,8 @@ public void testObjectGraph() throws Exception {
final FedoraObject object = objectService.createObject(session, "/graphObject");
final GraphStore graphStore = object.getGraphStore();

assertFalse("Graph store should not contain JCR prefixes", compile("jcr").matcher(graphStore.toString()).find());
assertFalse("Graph store should contain our fedora-internal prefix", compile("fedora-internal").matcher(graphStore.toString()).find());
assertEquals("info:fedora/graphObject", object.getGraphSubject().toString());

UpdateAction.parseExecute("PREFIX dc: <http://purl.org/dc/terms/>\n" +
Expand Down

0 comments on commit 2a81368

Please sign in to comment.