Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding appropriate inbound triples based on LDP containment propertie…
…s, adding IT to cover typical PCDM use case of proxies
  • Loading branch information
escowles committed Jul 16, 2015
1 parent 1c0156c commit 56b8a4a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 17 deletions.
Expand Up @@ -1478,6 +1478,83 @@ public void testGetObjectReferences() throws Exception {

}

@Test
public void testGetObjectReferencesIndirect() throws Exception {
final String uuid = getRandomUniquePid();
final String pid1 = uuid + "/parent";
final String pid2 = uuid + "/child";
createObject(pid1);
createObject(pid2);

final String memberRelation = "http://pcdm.org/models#hasMember";

// create an indirect container
final HttpPut createContainer = new HttpPut(serverAddress + pid1 + "/members");
createContainer.addHeader("Content-Type", "text/turtle");
final String membersRDF = "<> a <http://www.w3.org/ns/ldp#IndirectContainer>; "
+ "<http://www.w3.org/ns/ldp#hasMemberRelation> <" + memberRelation + ">; "
+ "<http://www.w3.org/ns/ldp#insertedContentRelation> <http://www.openarchives.org/ore/terms/proxyFor>; "
+ "<http://www.w3.org/ns/ldp#membershipResource> <" + serverAddress + pid1 + "> . ";
createContainer.setEntity(new StringEntity(membersRDF));
assertEquals(CREATED.getStatusCode(), getStatus(createContainer));

// create a proxy in the indirect container
final HttpPost createProxy = new HttpPost(serverAddress + pid1 + "/members");
createProxy.addHeader("Content-Type", "text/turtle");
final String proxyRDF = "<> <http://www.openarchives.org/ore/terms/proxyFor> <" + serverAddress + pid2 + ">;"
+ " <http://www.openarchives.org/ore/terms/proxyIn> <" + serverAddress + pid1 + "> .";
createProxy.setEntity(new StringEntity(proxyRDF));
assertEquals(CREATED.getStatusCode(), getStatus(createProxy));

// retrieve the parent and verify the outbound triples exist
final HttpGet getParent = new HttpGet(serverAddress + pid1);
getParent.addHeader("Accept", "application/n-triples");
final GraphStore parentGraph = getGraphStore(getParent);

assertTrue(parentGraph.contains(Node.ANY,
NodeFactory.createURI(serverAddress + pid1),
NodeFactory.createURI(memberRelation),
NodeFactory.createURI(serverAddress + pid2)));

// retrieve the members container and verify the LDP triples exist
final HttpGet getContainer = new HttpGet(serverAddress + pid1 + "/members");
getContainer.addHeader("Prefer", "return=representation;include=\"http://www.w3.org/ns/ldp#PreferMembership\"");
getContainer.addHeader("Accept", "application/n-triples");
final GraphStore containerGraph = getGraphStore(getContainer);

assertTrue(containerGraph.contains(Node.ANY,
NodeFactory.createURI(serverAddress + pid1 + "/members"),
NodeFactory.createURI("http://www.w3.org/ns/ldp#hasMemberRelation"),
NodeFactory.createURI(memberRelation)));

assertTrue(containerGraph.contains(Node.ANY,
NodeFactory.createURI(serverAddress + pid1 + "/members"),
NodeFactory.createURI("http://www.w3.org/ns/ldp#insertedContentRelation"),
NodeFactory.createURI("http://www.openarchives.org/ore/terms/proxyFor")));

assertTrue(containerGraph.contains(Node.ANY,
NodeFactory.createURI(serverAddress + pid1 + "/members"),
NodeFactory.createURI("http://www.w3.org/ns/ldp#membershipResource"),
NodeFactory.createURI(serverAddress + pid1)));


// retrieve the member and verify inbound triples exist
final HttpGet getMember = new HttpGet(serverAddress + pid2);
getMember.addHeader("Prefer", "return=representation; include=\"" + INBOUND_REFERENCES.toString() + "\"");
getMember.addHeader("Accept", "application/n-triples");
final GraphStore memberGraph = getGraphStore(getMember);

assertTrue(memberGraph.contains(Node.ANY,
Node.ANY,
NodeFactory.createURI("http://www.openarchives.org/ore/terms/proxyFor"),
NodeFactory.createURI(serverAddress + pid2)));

assertTrue(memberGraph.contains(Node.ANY,
NodeFactory.createURI(serverAddress + pid1),
NodeFactory.createURI(memberRelation),
NodeFactory.createURI(serverAddress + pid2)));
}

@Test
public void testGetObjectGraphLacksUUID() throws Exception {
final HttpResponse createResponse = createObject("");
Expand Down
Expand Up @@ -203,9 +203,13 @@ public Iterator<Triple> apply(final FedoraResource child) {
return Iterators.transform(values, new Function<Value, Triple>() {
@Override
public Triple apply(final Value input) {
final RDFNode membershipResource = new ValueConverter(session(), translator())
.convert(input);
return create(subject(), memberRelation, membershipResource.asNode());
try {
final RDFNode membershipResource = new ValueConverter(resource().getNode().getSession(),
translator()).convert(input);
return create(subject(), memberRelation, membershipResource.asNode());
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}
});
} catch (final RepositoryException e) {
Expand Down
Expand Up @@ -15,25 +15,34 @@
*/
package org.fcrepo.kernel.impl.rdf.impl;

import com.google.common.collect.Iterators;
import com.hp.hpl.jena.graph.Triple;
import static org.fcrepo.kernel.impl.identifiers.NodeResourceConverter.nodeConverter;
import static javax.jcr.PropertyType.PATH;
import static javax.jcr.PropertyType.REFERENCE;
import static javax.jcr.PropertyType.WEAKREFERENCE;

import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.kernel.models.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.rdf.impl.mappings.PropertyToTriple;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;

import java.util.Iterator;

/**
* Accumulate inbound references to a given resource
*
* @author cabeer
* @author escowles
*/
public class ReferencesRdfContext extends NodeRdfContext {

private final PropertyToTriple property2triple;
private final Session session;

/**
* Add the inbound references from other nodes to this resource to the stream
Expand All @@ -48,21 +57,36 @@ public ReferencesRdfContext(final FedoraResource resource,
throws RepositoryException {
super(resource, idTranslator);
property2triple = new PropertyToTriple(resource.getNode().getSession(), idTranslator);
concat(putStrongReferencePropertiesIntoContext());
concat(putWeakReferencePropertiesIntoContext());
session = resource.getNode().getSession();
putReferencesIntoContext(resource.getNode().getWeakReferences());
putReferencesIntoContext(resource.getNode().getReferences());
}

private Iterator<Triple> putWeakReferencePropertiesIntoContext() throws RepositoryException {
final Iterator<Property> properties = resource().getNode().getWeakReferences();
private void putReferencesIntoContext(final Iterator<Property> properties) throws RepositoryException {
while (properties.hasNext()) {
final Property p = properties.next();
concat(property2triple.apply(p));

return Iterators.concat(Iterators.transform(properties, property2triple));
for ( final PropertyIterator it = p.getParent().getProperties(); it.hasNext(); ) {
final Property potentialProxy = it.nextProperty();
if (potentialProxy.isMultiple()) {
for ( Value v : potentialProxy.getValues() ) {
putProxyReferencesIntoContext(v);
}
} else {
putProxyReferencesIntoContext(potentialProxy.getValue());
}
}
}
}

private Iterator<Triple> putStrongReferencePropertiesIntoContext() throws RepositoryException {
final Iterator<Property> properties = resource().getNode().getReferences();

return Iterators.concat(Iterators.transform(properties, property2triple));

private void putProxyReferencesIntoContext(final Value v) throws RepositoryException {
if (v.getType() == PATH) {
putProxyReferencesIntoContext(session.getNode(v.getString()));
} else if (v.getType() == REFERENCE || v.getType() == WEAKREFERENCE) {
putProxyReferencesIntoContext(session.getNodeByIdentifier(v.getString()));
}
}
private void putProxyReferencesIntoContext(final Node n) throws RepositoryException {
concat(new LdpContainerRdfContext(nodeConverter.convert(n), translator()));
}

}
Expand Up @@ -24,6 +24,7 @@

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
Expand Down Expand Up @@ -71,6 +72,8 @@ public class ReferencesRdfContextTest {
@Mock
private Value mockStrongValue;

@Mock
private PropertyIterator mockPropertyIterator;

@Before
public void setUp() throws RepositoryException {
Expand Down Expand Up @@ -102,6 +105,9 @@ public void setUp() throws RepositoryException {
strongReferencesProperties = new TestPropertyIterator(mockStrongProperty);
when(mockNode.getReferences()).thenReturn(strongReferencesProperties);

when(mockPropertyParent.getProperties()).thenReturn(mockPropertyIterator);
when(mockPropertyIterator.hasNext()).thenReturn(false);

testObj = new ReferencesRdfContext(mockResource, translator);
}

Expand Down

0 comments on commit 56b8a4a

Please sign in to comment.