Skip to content

Commit

Permalink
Merge pull request #475 from fcrepo4/getTriples
Browse files Browse the repository at this point in the history
Create getTriples member for converting a resource to RDF
  • Loading branch information
Andrew Woods committed Sep 19, 2014
2 parents b0fe421 + b599d77 commit 0d9b24c
Show file tree
Hide file tree
Showing 42 changed files with 687 additions and 1,755 deletions.
Expand Up @@ -238,7 +238,7 @@ public void testGetFederatedContent() throws RepositoryException {
public void testFixity() throws RepositoryException, IOException, NoSuchAlgorithmException {
final Session session = repo.login();

checkFixity(nodeService.getObject(session, testFilePath() + "/jcr:content").getNode());
checkFixity(datastreamService.getDatastream(session, testFilePath()));

session.save();
session.logout();
Expand All @@ -248,14 +248,14 @@ public void testFixity() throws RepositoryException, IOException, NoSuchAlgorith
public void testChangedFileFixity() throws RepositoryException, IOException, NoSuchAlgorithmException {
final Session session = repo.login();

final Node node = nodeService.getObject(session, testFilePath() + "/jcr:content").getNode();
final Datastream ds = datastreamService.getDatastream(session, testFilePath());

final String originalFixity = checkFixity(node);
final String originalFixity = checkFixity(ds);

final File file = fileForNode(null);
appendToFile(file, " ");

final String newFixity = checkFixity(node);
final String newFixity = checkFixity(ds);

assertNotEquals("Checksum is expected to have changed!", originalFixity, newFixity);

Expand All @@ -269,23 +269,23 @@ private static void appendToFile(final File f, final String data) throws IOExcep
}
}

private String checkFixity(final Node node) throws IOException, NoSuchAlgorithmException, RepositoryException {
assertNotNull(node);
private String checkFixity(final Datastream ds) throws IOException, NoSuchAlgorithmException, RepositoryException {
assertNotNull(ds);

final File file = fileForNode(null);
final byte[] hash = getHash(SHA_1, file);

final URI calculatedChecksum = asURI(SHA_1.toString(), hash);

final Collection<FixityResult> results = datastreamService.getFixity(node, calculatedChecksum, file.length());
final Collection<FixityResult> results = ds.getFixity(repo, SHA_1.toString());
assertNotNull(results);

assertFalse("Found no results!", results.isEmpty());

final Iterator<FixityResult> resultIterator = results.iterator();
while (resultIterator.hasNext()) {
final FixityResult result = resultIterator.next();
assertTrue(result.isSuccess());
assertTrue(result.matches(file.length(), calculatedChecksum));
}
return calculatedChecksum.toString();
}
Expand Down
Expand Up @@ -72,6 +72,7 @@
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.InvalidChecksumException;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext;
import org.fcrepo.kernel.utils.ContentDigest;
import org.modeshape.jcr.ExecutionContext;
import org.modeshape.jcr.value.PathFactory;
Expand Down Expand Up @@ -250,7 +251,8 @@ public Response batchModify(@PathParam("path") final List<PathSegment> pathList,
createDefaultModel().read(src,
subjects.getSubject(resource.getPath()).toString(), format);

resource.replaceProperties(subjects, inputModel);
resource.replaceProperties(subjects, inputModel,
resource.getTriples(subjects, PropertiesRdfContext.class));
} else {
throw new WebApplicationException(notAcceptable()
.entity("Invalid Content Type " + contentTypeString).build());
Expand Down
41 changes: 31 additions & 10 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -62,6 +62,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Iterator;
import java.util.List;

import javax.jcr.ItemExistsException;
Expand Down Expand Up @@ -107,7 +108,11 @@
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.InvalidChecksumException;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.rdf.HierarchyRdfContextOptions;
import org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ParentRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;
Expand Down Expand Up @@ -231,7 +236,7 @@ public RdfStream describe(@PathParam("path") final List<PathSegment> pathList,
new HttpIdentifierTranslator(session, this.getClass(), uriInfo);

final RdfStream rdfStream =
resource.getTriples(subjects).session(session)
resource.getTriples(subjects, PropertiesRdfContext.class).session(session)
.topic(subjects.getSubject(resource.getPath()).asNode());

final PreferTag returnPreference;
Expand Down Expand Up @@ -291,14 +296,28 @@ public RdfStream describe(@PathParam("path") final List<PathSegment> pathList,

final boolean references = !contains(omits, INBOUND_REFERENCES.toString());

final HierarchyRdfContextOptions hierarchyRdfContextOptions
= new HierarchyRdfContextOptions(limit, offset, membership, containment);

if (references) {
rdfStream.concat(resource.getReferencesTriples(subjects));
rdfStream.concat(resource.getTriples(subjects, ReferencesRdfContext.class));
}

rdfStream.concat(resource.getTriples(subjects, ParentRdfContext.class));

if (membership || containment) {
rdfStream.concat(resource.getTriples(subjects, ChildrenRdfContext.class));
}

if (containment) {

final Iterator<FedoraResource> children = resource.getChildren();

while (children.hasNext()) {
final FedoraResource child = children.next();
rdfStream.concat(child.getTriples(subjects, PropertiesRdfContext.class));
}

}

rdfStream.concat(resource.getHierarchyTriples(subjects, hierarchyRdfContextOptions));
rdfStream.concat(resource.getTriples(subjects, ContainerRdfContext.class));

servletResponse.addHeader("Preference-Applied", "return=representation");

Expand Down Expand Up @@ -501,7 +520,8 @@ public Response createOrReplaceObjectRdf(
graphSubjects.getSubject(resource.getPath()).toString(),
format);

resource.replaceProperties(graphSubjects, inputModel);
resource.replaceProperties(graphSubjects, inputModel,
resource.getTriples(graphSubjects, PropertiesRdfContext.class));

} else if (preexisting) {
return status(SC_CONFLICT).entity("No RDF provided and the resource already exists!").build();
Expand Down Expand Up @@ -624,7 +644,8 @@ public Response createObject(@PathParam("path")
createDefaultModel().read(requestBodyStream,
idTranslator.getSubject(result.getPath()).toString(), format);

result.replaceProperties(idTranslator, inputModel);
result.replaceProperties(idTranslator, inputModel,
result.getTriples(idTranslator, PropertiesRdfContext.class));
response = created(location).entity(location.toString());
} else if (result instanceof Datastream) {
LOGGER.trace("Created a datastream and have a binary payload.");
Expand Down Expand Up @@ -681,7 +702,7 @@ private FedoraResource createFedoraResource(final String requestMixin,
result = objectService.createObject(session, path);
break;
case FEDORA_DATASTREAM:
result = datastreamService.createDatastream(session, path);
result = datastreamService.getDatastream(session, path);
break;
default:
throw new WebApplicationException(clientError().entity(
Expand Down
Expand Up @@ -25,7 +25,10 @@
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.exception.RepositoryVersionRuntimeException;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
import org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.VersionsRdfContext;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -113,7 +116,11 @@ public RdfStream getVersionList(@PathParam("path") final List<PathSegment> pathL

final FedoraResource resource = nodeService.getObject(session, path);

return resource.getVersionTriples(nodeTranslator())
if (!resource.hasType("mix:versionable")) {
throw new RepositoryVersionRuntimeException("This operation requires that the node be versionable");
}

return resource.getTriples(nodeTranslator(), VersionsRdfContext.class)
.session(session)
.topic(nodeTranslator().getSubject(resource.getPath()).asNode());
}
Expand Down Expand Up @@ -242,7 +249,7 @@ public RdfStream getVersion(@PathParam("path")
}
final FedoraResource resource = new FedoraResourceImpl(node);
checkCacheControlHeaders(request, servletResponse, resource, session);
return resource.getTriples(nodeTranslator()).session(session).topic(
return resource.getTriples(nodeTranslator(), PropertiesRdfContext.class).session(session).topic(
nodeTranslator().getSubject(resource.getNode().getPath()).asNode());
}

Expand Down
Expand Up @@ -68,6 +68,7 @@
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.VersionService;

import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -202,7 +203,7 @@ public void testBatchRdfPost() throws Exception {

testObj.batchModify(createPathList(pid), multipart);
final ArgumentCaptor<Model> captor = ArgumentCaptor.forClass(Model.class);
verify(mockObject).replaceProperties(any(IdentifierTranslator.class), captor.capture());
verify(mockObject).replaceProperties(any(IdentifierTranslator.class), captor.capture(), any(RdfStream.class));
final Model capturedModel = captor.getValue();
assertTrue(capturedModel.contains(capturedModel.createResource("http://localhost/fcrepo/" + pid),
capturedModel.createProperty("info:a"),
Expand Down
Expand Up @@ -50,6 +50,7 @@
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

Expand All @@ -74,10 +75,15 @@
import org.fcrepo.http.commons.domain.Prefer;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraObject;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
import org.fcrepo.kernel.identifiers.PidMinter;
import org.fcrepo.kernel.rdf.HierarchyRdfContextOptions;
import org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ParentRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.fcrepo.kernel.services.DatastreamService;
import org.fcrepo.kernel.services.NodeService;
Expand Down Expand Up @@ -284,7 +290,7 @@ public void testCreateObjectWithDatastream() throws Exception {
when(mockPidMinter.mintPid()).thenReturn("a");
final Node contentNode = mock(Node.class);
final Datastream mockDatastream = mock(Datastream.class);
when(mockDatastreams.createDatastream(mockSession, path)).thenReturn(mockDatastream);
when(mockDatastreams.getDatastream(mockSession, path)).thenReturn(mockDatastream);
when(mockDatastream.getNode()).thenReturn(mockNode);
when(mockNode.getPath()).thenReturn(path);
when(mockDatastream.getContentNode()).thenReturn(contentNode);
Expand All @@ -303,7 +309,7 @@ public void testCreateObjectWithDatastream() throws Exception {
MediaType.valueOf("image/tiff"), null, mockResponse, getUriInfoImpl(), mockStream);
assertNotNull(actual);
assertEquals(CREATED.getStatusCode(), actual.getStatus());
verify(mockDatastreams).createDatastream(mockSession, path);
verify(mockDatastreams).getDatastream(mockSession, path);
verify(mockSession).save();
}

Expand Down Expand Up @@ -358,14 +364,19 @@ public void testDescribeObject() throws RepositoryException {
when(mockDataset.getContext()).thenReturn(mockContext);
when(mockObject.getLastModifiedDate()).thenReturn(mockDate);
when(mockObject.getEtagValue()).thenReturn("");
when(mockObject.getTriples(any(IdentifierTranslator.class))).thenReturn(
mockRdfStream);
when(mockObject.getHierarchyTriples(any(IdentifierTranslator.class),
any(HierarchyRdfContextOptions.class))).thenReturn(
mockRdfStream2);
when(mockObject.getReferencesTriples(any(IdentifierTranslator.class))).thenReturn(mockRdfStream3);
when(mockObject.getTriples(any(IdentifierTranslator.class), eq(PropertiesRdfContext.class)))
.thenReturn(mockRdfStream);
when(mockObject.getTriples(any(IdentifierTranslator.class), eq(ParentRdfContext.class)))
.thenReturn(mockRdfStream2);
when(mockObject.getTriples(any(IdentifierTranslator.class), eq(ChildrenRdfContext.class)))
.thenReturn(mockRdfStream2);
when(mockObject.getTriples(any(IdentifierTranslator.class), eq(ContainerRdfContext.class)))
.thenReturn(mockRdfStream2);
when(mockObject.getTriples(any(IdentifierTranslator.class),
eq(ReferencesRdfContext.class))).thenReturn(mockRdfStream3);
when(mockNodes.getObject(isA(Session.class), isA(String.class)))
.thenReturn(mockObject);
when(mockObject.getChildren()).thenReturn(Collections.<FedoraResource>emptyIterator());
final Request mockRequest = mock(Request.class);
final RdfStream rdfStream =
testObj.describe(createPathList(path), 0, -2, null, mockRequest,
Expand All @@ -387,11 +398,16 @@ public void testDescribeObjectNoInlining() throws RepositoryException, ParseExce

when(mockObject.getEtagValue()).thenReturn("");
when(mockObject.getLastModifiedDate()).thenReturn(mockDate);
when(mockObject.getTriples(any(IdentifierTranslator.class))).thenReturn(
when(mockObject.getTriples(any(IdentifierTranslator.class), eq(PropertiesRdfContext.class))).thenReturn(
mockRdfStream);
when(mockObject.getHierarchyTriples(any(IdentifierTranslator.class),
any(HierarchyRdfContextOptions.class))).thenReturn(mockRdfStream2);
when(mockObject.getReferencesTriples(any(IdentifierTranslator.class))).thenReturn(mockRdfStream3);

when(mockObject.getTriples(any(IdentifierTranslator.class), eq(ParentRdfContext.class)))
.thenReturn(mockRdfStream2);
when(mockObject.getTriples(any(IdentifierTranslator.class), eq(ContainerRdfContext.class)))
.thenReturn(mockRdfStream2);

when(mockObject.getTriples(any(IdentifierTranslator.class),
eq(ReferencesRdfContext.class))).thenReturn(mockRdfStream3);
when(mockNodes.getObject(isA(Session.class), isA(String.class)))
.thenReturn(mockObject);
final Request mockRequest = mock(Request.class);
Expand Down Expand Up @@ -492,7 +508,7 @@ public void testReplaceRdf() throws Exception {
mockStream,
mockRequest,
mockResponse);
verify(mockObject).replaceProperties(any(IdentifierTranslator.class), any(Model.class));
verify(mockObject).replaceProperties(any(IdentifierTranslator.class), any(Model.class), any(RdfStream.class));
}

@Test
Expand Down
Expand Up @@ -24,6 +24,7 @@
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -46,6 +47,7 @@
import org.fcrepo.http.commons.api.rdf.HttpIdentifierTranslator;
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
import org.fcrepo.kernel.impl.rdf.impl.VersionsRdfContext;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.VersionService;
import org.fcrepo.kernel.utils.iterators.RdfStream;
Expand Down Expand Up @@ -120,8 +122,9 @@ public void testGetVersionList() throws RepositoryException {
mockVariant);
when(mockNodes.getObject(any(Session.class), anyString())).thenReturn(
mockResource);
when(mockResource.getVersionTriples(any(HttpIdentifierTranslator.class)))
when(mockResource.getTriples(any(HttpIdentifierTranslator.class), eq(VersionsRdfContext.class)))
.thenReturn(mockRdfStream);
when(mockResource.hasType("mix:versionable")).thenReturn(true);
when(mockVariant.getMediaType()).thenReturn(
new MediaType("text", "turtle"));
final RdfStream response =
Expand Down
Expand Up @@ -681,9 +681,9 @@ public void testGetObjectOmitContainment() throws Exception {

assertFalse("Didn't expect contained member resources",
compile(
"<"
"^<"
+ serverAddress
+ pid + "> <" + CONTAINS + ">",
+ pid + "/a>",
DOTALL).matcher(content).find());

}
Expand Down Expand Up @@ -1603,6 +1603,7 @@ public void testFederatedMoveWithProperties() throws Exception {
}

@Test
@Ignore("https://www.pivotaltracker.com/story/show/59240160")
public void testPaging() throws Exception {
// create a node with 4 children
final String pid = getRandomUniquePid();
Expand Down

0 comments on commit 0d9b24c

Please sign in to comment.