Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
merge fcr:graph back into the FedoraNodes
  • Loading branch information
cbeer committed May 8, 2013
1 parent 4277229 commit 23c53a4
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 168 deletions.
153 changes: 0 additions & 153 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraGraph.java

This file was deleted.

114 changes: 101 additions & 13 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraNodes.java
Expand Up @@ -16,6 +16,7 @@
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -96,6 +97,59 @@ public Response describe(@PathParam("path")
}
}

@GET
@Produces({WebContent.contentTypeN3,
WebContent.contentTypeN3Alt1,
WebContent.contentTypeN3Alt2,
WebContent.contentTypeTurtle,
WebContent.contentTypeRDFXML,
WebContent.contentTypeRDFJSON,
WebContent.contentTypeNTriples})
public StreamingOutput describeRdf(@PathParam("path") final List<PathSegment> pathList, @Context Request request) throws RepositoryException, IOException {

final String path = toPath(pathList);
logger.trace("getting profile for {}", path);



List<Variant> possibleResponseVariants =
Variant.mediaTypes(new MediaType(WebContent.contentTypeN3.split("/")[0], WebContent.contentTypeN3.split("/")[1]),
new MediaType(WebContent.contentTypeN3Alt1.split("/")[0], WebContent.contentTypeN3Alt1.split("/")[1]),
new MediaType(WebContent.contentTypeN3Alt2.split("/")[0], WebContent.contentTypeN3Alt2.split("/")[1]),
new MediaType(WebContent.contentTypeTurtle.split("/")[0], WebContent.contentTypeTurtle.split("/")[1]),
new MediaType(WebContent.contentTypeRDFXML.split("/")[0], WebContent.contentTypeRDFXML.split("/")[1]),
new MediaType(WebContent.contentTypeRDFJSON.split("/")[0], WebContent.contentTypeRDFJSON.split("/")[1]),
new MediaType(WebContent.contentTypeNTriples.split("/")[0], WebContent.contentTypeNTriples.split("/")[1]),
new MediaType(WebContent.contentTypeTriG.split("/")[0], WebContent.contentTypeTriG.split("/")[1]),
new MediaType(WebContent.contentTypeNQuads.split("/")[0], WebContent.contentTypeNQuads.split("/")[1])
)
.add().build();
Variant bestPossibleResponse = request.selectVariant(possibleResponseVariants);


final String rdfWriterFormat = WebContent.contentTypeToLang(bestPossibleResponse.getMediaType().toString()).getName().toUpperCase();

return new StreamingOutput() {
@Override
public void write(final OutputStream out) throws IOException {

final Session session = getAuthenticatedSession();
try {
final FedoraObject object = objectService.getObject(session, path);
final GraphStore graphStore = object.getGraphStore();

graphStore.toDataset().getDefaultModel().write(out, rdfWriterFormat);
} catch (final RepositoryException e) {
throw new WebApplicationException(e);
} finally {
session.logout();
}
}

};

}

/**
* Returns an object profile.
*
Expand Down Expand Up @@ -202,6 +256,52 @@ public Response modifyObject(@PathParam("path")
}
}

/**
* Update an object using SPARQL-UPDATE
*
* @param pathList
* @return 201
* @throws RepositoryException
* @throws org.fcrepo.exception.InvalidChecksumException
* @throws IOException
*/
@POST
@Consumes({WebContent.contentTypeSPARQLUpdate})
@Timed
public Response updateSparql(
@PathParam("path") final List<PathSegment> pathList,
final InputStream requestBodyStream
) throws RepositoryException, IOException {

String path = toPath(pathList);
logger.debug("Attempting to ingest with path: {}", path);

final Session session = getAuthenticatedSession();

try {
if (objectService.exists(session, path)) {

if(requestBodyStream != null) {

final FedoraObject result = objectService.getObject(session, path);

result.updateGraph(IOUtils.toString(requestBodyStream));

session.save();

return Response.ok().build();
} else {
return Response.status(HttpStatus.SC_CONFLICT).entity(path + " is an existing resource").build();
}
} else {
return Response.status(HttpStatus.SC_NOT_FOUND).entity(path + " must be an existing resource").build();
}

} finally {
session.logout();
}
}

/**
* Creates a new object.
*
Expand Down Expand Up @@ -230,19 +330,7 @@ public Response createObject(

try {
if (objectService.exists(session, path)) {

if(requestBodyStream != null && requestContentType != null && requestContentType.toString().equals(WebContent.contentTypeSPARQLUpdate)) {

final FedoraObject result = objectService.getObject(session, path);

UpdateAction.parseExecute(IOUtils.toString(requestBodyStream), result.getGraphStore());

session.save();

return Response.ok().build();
} else {
return Response.status(HttpStatus.SC_CONFLICT).entity(path + " is an existing resource").build();
}
}

if (FedoraJcrTypes.FEDORA_OBJECT.equals(mixin)){
Expand All @@ -253,7 +341,7 @@ public Response createObject(
}

if(requestBodyStream != null && requestContentType != null && requestContentType.toString().equals(WebContent.contentTypeSPARQLUpdate)) {
UpdateAction.parseExecute(IOUtils.toString(requestBodyStream), result.getGraphStore());
result.updateGraph(IOUtils.toString(requestBodyStream));
}

}
Expand Down
57 changes: 57 additions & 0 deletions fcrepo-http-api/src/test/java/org/fcrepo/api/FedoraNodesTest.java
Expand Up @@ -14,19 +14,30 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.List;

import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Variant;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.update.UpdateAction;
import org.apache.commons.io.IOUtils;
import org.fcrepo.Datastream;
import org.fcrepo.FedoraObject;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.identifiers.UUIDPidMinter;
import org.fcrepo.services.DatastreamService;
Expand Down Expand Up @@ -169,5 +180,51 @@ public void testDescribeDatastream() throws RepositoryException, IOException {
verify(mockDatastreams).getDatastream(mockSession, path);
verify(mockSession, never()).save();
}

@Test
public void testDescribeRdfObject() throws RepositoryException, IOException {
final String pid = "FedoraObjectsRdfTest1";
final String path = "/" + pid;

final FedoraObject mockObject = mock(FedoraObject.class);

final GraphStore mockStore = mock(GraphStore.class);
final Dataset mockDataset = mock(Dataset.class);
final Model mockModel = mock(Model.class);
when(mockStore.toDataset()).thenReturn(mockDataset);
when(mockDataset.getDefaultModel()).thenReturn(mockModel);


when(mockObject.getGraphStore()).thenReturn(mockStore);
when(mockObjects.getObject(mockSession, path)).thenReturn(mockObject);
final Request mockRequest = mock(Request.class);

when(mockRequest.selectVariant(any(List.class))).thenReturn(new Variant(MediaType.valueOf("application/n-triples"), null, null));

final OutputStream mockStream = mock(OutputStream.class);
testObj.describeRdf(createPathList(pid), mockRequest).write(mockStream);

verify(mockModel).write(mockStream, "N-TRIPLES");

}

@Test
public void testSparqlUpdate() throws RepositoryException, IOException {
final String pid = "FedoraObjectsRdfTest1";
final String path = "/" + pid;

final FedoraObject mockObject = mock(FedoraObject.class);

final GraphStore mockStore = mock(GraphStore.class);
final InputStream mockStream = new ByteArrayInputStream("my-sparql-statement".getBytes());
when(mockObjects.getObject(mockSession, path)).thenReturn(mockObject);
when(mockObjects.exists(mockSession, path)).thenReturn(true);

testObj.updateSparql(createPathList(pid), mockStream);

verify(mockObject).updateGraph("my-sparql-statement");
verify(mockSession).save();
verify(mockSession).logout();
}

}

0 comments on commit 23c53a4

Please sign in to comment.