Skip to content

Commit

Permalink
Enable transparent hierarchy support for versions, locks, import, exp…
Browse files Browse the repository at this point in the history
…ort, batch, and fixity etc.

Resolves: https://www.pivotaltracker.com/story/show/71946314
  • Loading branch information
lsitu authored and Andrew Woods committed Jul 8, 2014
1 parent ab5139c commit 4c9625e
Show file tree
Hide file tree
Showing 23 changed files with 214 additions and 54 deletions.
Expand Up @@ -56,8 +56,11 @@ public Model createModelForResource(final FedoraResource resource,

if (resource.getNode().isNodeType(
FedoraJcrTypes.FEDORA_RESOURCE)) {
if (resource.getPath(graphSubjects) == null) {
throw new RepositoryException("resource.getPath(graphSubjects) is Null: " + resource.getPath());
}
final Map<String, String> pathMap =
singletonMap("path", resource.getPath().substring(1));
singletonMap("path", resource.getPath(graphSubjects).substring(1));
final Resource acl = model.createResource(uriInfo.getBaseUriBuilder().path(
AccessRoles.class).buildFromMap(pathMap).toASCIIString());
model.add(s, RdfLexicon.HAS_ACCESS_ROLES_SERVICE, acl);
Expand Down
Expand Up @@ -27,10 +27,10 @@
import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.http.commons.api.rdf.HttpIdentifierTranslator;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
Expand All @@ -48,7 +48,7 @@
public class AccessRolesResourcesTest {

@Mock
private IdentifierTranslator graphSubjects;
private HttpIdentifierTranslator graphSubjects;

@Mock
private FedoraResource fedoraResource;
Expand Down Expand Up @@ -99,12 +99,11 @@ public void testCreateModelForNonFedoraResource()

@Test
public void testCreateModelForResource() throws RepositoryException {

when(resourceNode.isNodeType(eq(FedoraJcrTypes.FEDORA_RESOURCE)))
.thenReturn(true);

when(fedoraResource.getPath()).thenReturn("/" + pathString);

when(resourceNode.getPath()).thenReturn("/" + pathString);
when(fedoraResource.getPath(graphSubjects)).thenReturn("/" + pathString);
final Model model =
resources.createModelForResource(fedoraResource, uriInfo,
graphSubjects);
Expand Down
Expand Up @@ -16,6 +16,7 @@
package org.fcrepo.http.api;

import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static com.sun.jersey.api.Responses.notAcceptable;
import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA;
Expand Down Expand Up @@ -151,10 +152,14 @@ public Response batchModify(@PathParam("path") final List<PathSegment> pathList,
throws RepositoryException, InvalidChecksumException, IOException, URISyntaxException {

final String path = toPath(pathList);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);

// TODO: this is ugly, but it works.
final PathFactory pathFactory = new ExecutionContext().getValueFactories().getPathFactory();
final org.modeshape.jcr.value.Path jcrPath = pathFactory.create(path);
final org.modeshape.jcr.value.Path jcrPathObj = pathFactory.create(jcrPath);

try {

Expand Down Expand Up @@ -222,14 +227,11 @@ public Response batchModify(@PathParam("path") final List<PathSegment> pathList,
pathName = partName;
}

final String objPath = pathFactory.create(jcrPath, pathName).getCanonicalPath().getString();
final String objPath = pathFactory.create(jcrPathObj, pathName).getCanonicalPath().getString();

switch (realContentDisposition) {
case INLINE:

final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

final FedoraResource resource;

if (nodeService.exists(session, objPath)) {
Expand Down Expand Up @@ -293,10 +295,7 @@ public Response batchModify(@PathParam("path") final List<PathSegment> pathList,
versionService.nodeUpdated(resource.getNode());
}

final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

return created(new URI(subjects.getSubject(path).getURI())).build();
return created(new URI(subjects.getSubject(path).getURI())).build();

} finally {
session.logout();
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package org.fcrepo.http.api;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static javax.ws.rs.core.Response.ok;
import static org.slf4j.LoggerFactory.getLogger;

Expand All @@ -35,6 +36,7 @@
import javax.ws.rs.core.StreamingOutput;

import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.HttpIdentifierTranslator;
import org.fcrepo.http.commons.session.InjectedSession;
import org.fcrepo.serialization.FedoraObjectSerializer;
import org.fcrepo.serialization.SerializerUtil;
Expand Down Expand Up @@ -68,15 +70,20 @@ public class FedoraExport extends AbstractResource {
* @param pathList
* @param format
* @return object in the given format
* @throws RepositoryException
*/
@GET
public Response exportObject(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("format") @DefaultValue("jcr/xml") final String format) {
@QueryParam("format") @DefaultValue("jcr/xml") final String format) throws RepositoryException {

final String path = toPath(pathList);

LOGGER.debug("Requested object serialization for {} using serialization format {}", path, format);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);

final FedoraObjectSerializer serializer =
serializers.getSerializer(format);
Expand All @@ -92,7 +99,7 @@ public void write(final OutputStream out)
LOGGER.debug("Selecting from serializer map: {}", serializers);
LOGGER.debug("Retrieved serializer for format: {}", format);
serializer.serialize(objectService.getObject(
session, path), out);
session, jcrPath), out);
LOGGER.debug("Successfully serialized object: {}", path);
} catch (final RepositoryException e) {
throw new WebApplicationException(e);
Expand Down
Expand Up @@ -25,6 +25,8 @@
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X;
import static org.slf4j.LoggerFactory.getLogger;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;

import java.util.List;

Expand All @@ -44,6 +46,7 @@
import org.fcrepo.http.commons.session.InjectedSession;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

Expand All @@ -60,6 +63,8 @@
@Path("/{path: .*}/fcr:fixity")
public class FedoraFixity extends AbstractResource {

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

@InjectedSession
protected Session session;

Expand All @@ -86,12 +91,14 @@ public RdfStream getDatastreamFixity(@PathParam("path")
final UriInfo uriInfo) throws RepositoryException {

final String path = toPath(pathList);
LOGGER.trace("Getting datastream fixity profile path {}", path);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);

final Datastream ds = datastreamService.getDatastream(session, path);

return datastreamService.getFixityResultsModel(
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo), ds)
.session(session);
final Datastream ds = datastreamService.getDatastream(session, jcrPath);
return datastreamService.getFixityResultsModel(subjects, ds).session(session);

}
}
Expand Up @@ -15,6 +15,7 @@
*/
package org.fcrepo.http.api;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.Status.CONFLICT;
import static javax.ws.rs.core.Response.status;
Expand Down Expand Up @@ -91,13 +92,14 @@ public Response importObject(@PathParam("path") final List<PathSegment> pathList

final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);
try {
serializers.getSerializer(format)
.deserialize(session, path, stream);
.deserialize(session, jcrPath, stream);
session.save();
return created(new URI(subjects.getSubject(path).getURI())).build();
} catch ( ItemExistsException ex ) {
return created(new URI(subjects.getSubject(jcrPath).getURI())).build();
} catch ( final ItemExistsException ex ) {
return status(CONFLICT).entity("Item already exists").build();
} finally {
session.logout();
Expand Down
30 changes: 23 additions & 7 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraLocks.java
Expand Up @@ -49,6 +49,7 @@
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
Expand Down Expand Up @@ -89,8 +90,13 @@ public class FedoraLocks extends AbstractResource implements FedoraJcrTypes {
public RdfStream getLock(@PathParam("path") final List<PathSegment> pathList) throws RepositoryException {

final String path = toPath(pathList);
final Node node = session.getNode(path);
final Lock lock = lockService.getLock(session, path);
LOGGER.trace("Getting lock profile for: {}", path);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);
final Node node = session.getNode(jcrPath);
final Lock lock = lockService.getLock(session, jcrPath);
return getLockRdfStream(node, lock);
}

Expand All @@ -108,11 +114,16 @@ public Response createLock(@PathParam("path") final List<PathSegment> pathList,
throws RepositoryException, URISyntaxException {
try {
final String path = toPath(pathList);
final Node node = session.getNode(path);
final Lock lock = lockService.acquireLock(session, path, isDeep);
LOGGER.trace("Creating lock for: {}", path);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);
final Node node = session.getNode(jcrPath);
final Lock lock = lockService.acquireLock(session, jcrPath, isDeep);
session.save();
final String location = getTranslator().getSubject(node.getPath()).getURI();
LOGGER.debug("Locked {} with lock token {}.", path, lock.getLockToken());
LOGGER.debug("Locked {} with lock token {}.", jcrPath, lock.getLockToken());
return created(new URI(location)).entity(location).header("Lock-Token", lock.getLockToken()).build();
} finally {
session.logout();
Expand All @@ -130,9 +141,14 @@ public Response deleteLock(@PathParam("path") final List<PathSegment> pathList)
throws RepositoryException, URISyntaxException {
try {
final String path = toPath(pathList);
lockService.releaseLock(session, path);
LOGGER.trace("Deleting lock for: {}", path);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);
lockService.releaseLock(session, jcrPath);
session.save();
LOGGER.debug("Unlocked {}.", path);
LOGGER.debug("Unlocked {}.", jcrPath);
return noContent().build();
} finally {
session.logout();
Expand Down
Expand Up @@ -54,6 +54,7 @@
import java.util.Collections;
import java.util.List;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
Expand Down Expand Up @@ -107,14 +108,18 @@ public RdfStream getVersionList(@PathParam("path") final List<PathSegment> pathL
@Context final UriInfo uriInfo) throws RepositoryException {
final String path = toPath(pathList);

LOGGER.trace("Getting versions list for: {}", path);
LOGGER.info("Getting versions list for: {}", path);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.info("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);

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

try {
return resource.getVersionTriples(nodeTranslator()).session(session).topic(
nodeTranslator().getSubject(resource.getNode().getPath()).asNode());
} catch ( UnsupportedRepositoryOperationException ex ) {
} catch ( final UnsupportedRepositoryOperationException ex ) {
throw new WebApplicationException( status(NOT_FOUND).entity("This resource is not versioned").build() );
}
}
Expand Down Expand Up @@ -153,8 +158,12 @@ public Response revertToVersion(@PathParam("path") final List<PathSegment> pathL
final String path = toPath(pathList);
LOGGER.info("Reverting {} to version {}.", path,
label);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.info("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);
try {
versionService.revertToVersion(session.getWorkspace(), path, label);
versionService.revertToVersion(session.getWorkspace(), jcrPath, label);
return noContent().build();
} finally {
session.logout();
Expand All @@ -174,10 +183,14 @@ public Response removeVersion(@PathParam("path") final List<PathSegment> pathLis
@PathParam("label") final String label) throws RepositoryException {
final String path = toPath(pathList);
LOGGER.info("Removing {} version {}.", path, label);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.info("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);
try {
versionService.removeVersion(session.getWorkspace(), path, label);
versionService.removeVersion(session.getWorkspace(), jcrPath, label);
return noContent().build();
} catch ( VersionException ex ) {
} catch ( final VersionException ex ) {
return status(BAD_REQUEST).entity(ex.getMessage()).build();
} finally {
session.logout();
Expand All @@ -196,10 +209,15 @@ public Response addVersion(@PathParam("path")

private Response addVersion(final String path, final String label) throws RepositoryException {
try {
LOGGER.info("Adding {} version {}.", path, label);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.info("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);
final FedoraResource resource =
nodeService.getObject(session, path);
nodeService.getObject(session, jcrPath);
versionService.createVersion(session.getWorkspace(),
Collections.singleton(path));
Collections.singleton(jcrPath));
if (label != null) {
resource.addVersionLabel(label);
}
Expand Down Expand Up @@ -235,6 +253,10 @@ public RdfStream getVersion(@PathParam("path")
final String path = toPath(pathList);
LOGGER.trace("Getting version profile for: {} at version: {}", path,
label);
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, this.getClass(), uriInfo);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("GET: Using auto hierarchy path {} to retrieve resource.", jcrPath);
final Node node = nodeTranslator().getNodeFromGraphSubjectForVersionNode(uriInfo.getRequestUri().toString());
if (node == null) {
throw new WebApplicationException(status(NOT_FOUND).build());
Expand Down

0 comments on commit 4c9625e

Please sign in to comment.