Skip to content

Commit

Permalink
build graph subjects from UriInfo when working with the http api
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed May 15, 2013
1 parent ca58e52 commit cfb76c4
Show file tree
Hide file tree
Showing 21 changed files with 601 additions and 158 deletions.
Expand Up @@ -27,7 +27,11 @@ public void testJcrPropertiesBasedOaiDc() throws Exception {
final HttpPost post = postObjMethod("DublinCoreTest1");
post.setHeader("Content-Type", "application/sparql-update");
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream("INSERT { <info:fedora/objects/DublinCoreTest1> <http://purl.org/dc/terms/identifier> \"this is an identifier\" } WHERE {}".getBytes()));
String subjectURI = serverAddress + "objects/DublinCoreTest1";
entity.setContent(new ByteArrayInputStream(
("INSERT { <" + subjectURI +
"> <http://purl.org/dc/terms/identifier> \"this is an identifier\" } WHERE {}")
.getBytes()));
post.setEntity(entity);
assertEquals(204, getStatus( post));
final HttpGet getWorstCaseOaiMethod =
Expand Down
61 changes: 37 additions & 24 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraNodes.java
Expand Up @@ -22,6 +22,7 @@
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.Consumes;
Expand All @@ -41,6 +42,7 @@
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;

import org.apache.commons.io.IOUtils;
Expand All @@ -49,8 +51,10 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.FedoraObject;
import org.fcrepo.FedoraResource;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.provider.GraphStreamingOutput;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.services.ObjectService;
Expand All @@ -68,15 +72,16 @@
public class FedoraNodes extends AbstractResource {

private static final Logger logger = getLogger(FedoraNodes.class);

@Autowired
private LowLevelStorageService llStoreService;

@Autowired
private LowLevelStorageService llStoreService;
@GET
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public Response describeRdf(@PathParam("path")
final List<PathSegment> pathList, @Context
final Request request) throws RepositoryException, IOException {
public Response describeRdf(
@PathParam("path") final List<PathSegment> pathList,
@Context final Request request,
@Context UriInfo uriInfo) throws RepositoryException, IOException {

final String path = toPath(pathList);
logger.trace("Getting profile for {}", path);
Expand All @@ -98,7 +103,8 @@ public Response describeRdf(@PathParam("path")

if (builder == null) {
builder =
ok(new GraphStreamingOutput(resource.getGraphStore(),
ok(new GraphStreamingOutput(resource.getGraphStore(
new HttpGraphSubjects(uriInfo)),
bestPossibleResponse.getMediaType()));
}

Expand All @@ -124,8 +130,10 @@ public Response describeRdf(@PathParam("path")
@PUT
@Consumes({contentTypeSPARQLUpdate})
@Timed
public Response modifyObject(@PathParam("path")
final List<PathSegment> pathList, final InputStream requestBodyStream)
public Response modifyObject(
@PathParam("path") final List<PathSegment> pathList,
@Context UriInfo uriInfo,
final InputStream requestBodyStream)
throws RepositoryException, IOException {
final Session session = getAuthenticatedSession();
final String path = toPath(pathList);
Expand All @@ -137,7 +145,7 @@ public Response modifyObject(@PathParam("path")

if (requestBodyStream != null) {
UpdateAction.parseExecute(IOUtils.toString(requestBodyStream),
result.getGraphStore());
result.getGraphStore(new HttpGraphSubjects(uriInfo)));
}
session.save();

Expand All @@ -159,8 +167,9 @@ public Response modifyObject(@PathParam("path")
@POST
@Consumes({contentTypeSPARQLUpdate})
@Timed
public Response updateSparql(@PathParam("path")
final List<PathSegment> pathList, final InputStream requestBodyStream)
public Response updateSparql(
@PathParam("path") final List<PathSegment> pathList,
@Context UriInfo uriInfo, final InputStream requestBodyStream)
throws RepositoryException, IOException {

final String path = toPath(pathList);
Expand All @@ -175,7 +184,8 @@ public Response updateSparql(@PathParam("path")
final FedoraResource result =
nodeService.getObject(session, path);

result.updateGraph(IOUtils.toString(requestBodyStream));
result.updateGraph(new HttpGraphSubjects(uriInfo),
IOUtils.toString(requestBodyStream));
final Problems problems = result.getGraphProblems();
if (problems != null && problems.hasProblems()) {
logger.info(
Expand Down Expand Up @@ -210,13 +220,14 @@ public Response updateSparql(@PathParam("path")
*/
@POST
@Timed
public Response createObject(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("mixin")
@DefaultValue(FedoraJcrTypes.FEDORA_OBJECT)
final String mixin, @QueryParam("checksumType")
final String checksumType, @QueryParam("checksum")
final String checksum, @HeaderParam("Content-Type")
final MediaType requestContentType, final InputStream requestBodyStream)
public Response createObject(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("mixin") @DefaultValue(FedoraJcrTypes.FEDORA_OBJECT) final String mixin,
@QueryParam("checksumType") final String checksumType,
@QueryParam("checksum") final String checksum,
@HeaderParam("Content-Type") final MediaType requestContentType,
@Context UriInfo uriInfo,
final InputStream requestBodyStream)
throws RepositoryException, IOException, InvalidChecksumException {

final String path = toPath(pathList);
Expand All @@ -238,7 +249,8 @@ public Response createObject(@PathParam("path")
requestContentType != null &&
requestContentType.toString().equals(
WebContent.contentTypeSPARQLUpdate)) {
result.updateGraph(IOUtils.toString(requestBodyStream));
result.updateGraph(new HttpGraphSubjects(uriInfo),
IOUtils.toString(requestBodyStream));
}

}
Expand Down Expand Up @@ -269,8 +281,9 @@ public Response createObject(@PathParam("path")
*/
@DELETE
@Timed
public Response deleteObject(@PathParam("path")
final List<PathSegment> path) throws RepositoryException {
public Response deleteObject(
@PathParam("path") final List<PathSegment> path)
throws RepositoryException {
final Session session = getAuthenticatedSession();

try {
Expand Down
Expand Up @@ -15,6 +15,7 @@

import org.fcrepo.AbstractResource;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -66,7 +67,7 @@ public MultivaluedMap<String, String> getMatrixParameters() {
try {
return objectsResource.createObject(
segments.build(),
FedoraJcrTypes.FEDORA_OBJECT, null, null, null, null);
FedoraJcrTypes.FEDORA_OBJECT, null, null, null, TestHelpers.getUriInfoImpl(), null);
} catch (IOException e) {
throw new RepositoryException(e.getMessage(), e);
} catch (InvalidChecksumException e) {
Expand Down
26 changes: 17 additions & 9 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraVersions.java
Expand Up @@ -25,10 +25,12 @@
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;

import org.fcrepo.AbstractResource;
import org.fcrepo.FedoraResource;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.provider.GraphStreamingOutput;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
Expand All @@ -41,8 +43,10 @@ public class FedoraVersions extends AbstractResource {

@GET
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public Response getVersionList(@PathParam("path")
final List<PathSegment> pathList, @Context Request request) throws RepositoryException {
public Response getVersionList(
@PathParam("path") final List<PathSegment> pathList,
@Context Request request,
@Context UriInfo uriInfo) throws RepositoryException {
final String path = toPath(pathList);

LOGGER.trace("getting versions list for {}", path);
Expand All @@ -53,8 +57,9 @@ public Response getVersionList(@PathParam("path")
try {
final FedoraResource resource = nodeService.getObject(session, path);

return Response.ok(new GraphStreamingOutput(resource.getVersionGraphStore(),
bestPossibleResponse.getMediaType())).build();
return Response.ok(new GraphStreamingOutput(
resource.getVersionGraphStore(new HttpGraphSubjects(uriInfo)),
bestPossibleResponse.getMediaType())).build();

} finally {
session.logout();
Expand Down Expand Up @@ -82,9 +87,11 @@ public Response addVersionLabel(@PathParam("path")
@Path("/{versionLabel}")
@GET
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public Response getVersion(@PathParam("path")
final List<PathSegment> pathList, @PathParam("versionLabel")
final String versionLabel, @Context Request request) throws RepositoryException, IOException {
public Response getVersion(
@PathParam("path") final List<PathSegment> pathList,
@PathParam("versionLabel") final String versionLabel,
@Context Request request,
@Context UriInfo uriInfo) throws RepositoryException, IOException {
final String path = toPath(pathList);
LOGGER.trace("getting version profile for {} at version {}", path, versionLabel);

Expand All @@ -98,8 +105,9 @@ public Response getVersion(@PathParam("path")
return Response.status(Response.Status.NOT_FOUND).build();
} else {

return Response.ok(new GraphStreamingOutput(resource.getGraphStore(),
bestPossibleResponse.getMediaType())).build();
return Response.ok(new GraphStreamingOutput(
resource.getGraphStore(new HttpGraphSubjects(uriInfo)),
bestPossibleResponse.getMediaType())).build();
}

} finally {
Expand Down
@@ -0,0 +1,88 @@
package org.fcrepo.api.rdf;

import static org.fcrepo.utils.FedoraJcrTypes.FCR_CONTENT;

import java.net.URI;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.api.FedoraNodes;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.utils.FedoraJcrTypes;
import org.modeshape.jcr.api.JcrConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableBiMap;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;

public class HttpGraphSubjects implements GraphSubjects {

private static final Logger LOGGER =
LoggerFactory.getLogger(HttpGraphSubjects.class);

private final UriInfo uris;

private final UriBuilder nodesBuilder;

private final String basePath;

private final int pathIx;

public HttpGraphSubjects(UriInfo uris) {
this.uris = uris;
this.nodesBuilder = uris.getBaseUriBuilder().path(FedoraNodes.class);
String basePath = nodesBuilder.build("").toString();
if (!basePath.endsWith("/")) basePath = basePath + "/";
this.basePath = basePath;
this.pathIx = basePath.length() - 1;
LOGGER.debug("Resolving graph subjects to a base URI of \"{}\"", basePath);
}

@Override
public Resource getGraphSubject(Node node) throws RepositoryException {
URI result = nodesBuilder
.buildFromMap(getPathMap(node));
return ResourceFactory.createResource(result.toString());
}

@Override
public Node getNodeFromGraphSubject(Session session, Resource subject)
throws RepositoryException {
if (!isFedoraGraphSubject(subject)) {
return null;
}

final String absPath = subject.getURI().substring(pathIx);

if (absPath.endsWith(FCR_CONTENT)) {
return session.getNode(absPath.replace(FedoraJcrTypes.FCR_CONTENT, JcrConstants.JCR_CONTENT));
} else if (session.nodeExists(absPath)) {
return session.getNode(absPath);
} else {
return null;
}

}

@Override
public boolean isFedoraGraphSubject(Resource subject) {
return subject.isURIResource() && subject.getURI().startsWith(basePath);
}

private static Map<String, String> getPathMap(Node node)
throws RepositoryException {
// the path param value doesn't start with a slash
String path = node.getPath().substring(1);
if (path.endsWith(JcrConstants.JCR_CONTENT)) {
path = path.replace(JcrConstants.JCR_CONTENT, FedoraJcrTypes.FCR_CONTENT);
}
return ImmutableBiMap.of("path", path);
}
}

0 comments on commit cfb76c4

Please sign in to comment.