Skip to content

Commit

Permalink
Refactor CRUD operations in FedoraNodes and FedoraContent for transpa…
Browse files Browse the repository at this point in the history
…rent auto-hierarchy support.

- Need to reinstate tests in FedoraNodesTest and FedoraContentTest

Related to https://www.pivotaltracker.com/story/show/71230608
  • Loading branch information
lsitu authored and Andrew Woods committed May 14, 2014
1 parent 917851c commit 9b1ff36
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 88 deletions.
Expand Up @@ -48,6 +48,7 @@
import java.text.ParseException;
import java.util.List;

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.noContent;
import static javax.ws.rs.core.Response.status;
Expand Down Expand Up @@ -88,14 +89,19 @@ public Response create(@PathParam("path")
@HeaderParam("Content-Type") final MediaType requestContentType,
final InputStream requestBodyStream, @Context final HttpServletResponse servletResponse)
throws InvalidChecksumException, RepositoryException, URISyntaxException, ParseException {

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

final MediaType contentType = getSimpleContentType(requestContentType);


final String newDatastreamPath;
final String path = toPath(pathList);

if (nodeService.exists(session, path)) {
if ( nodeService.getObject(session, path).hasContent() ) {
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
if (nodeService.exists(session, jcrPath)) {
if ( nodeService.getObject(session, jcrPath).hasContent() ) {
return status(SC_CONFLICT)
.entity(path + " is an existing resource!").build();
}
Expand All @@ -107,10 +113,9 @@ public Response create(@PathParam("path")
} else {
pid = pidMinter.mintPid();
}

newDatastreamPath = path + "/" + pid;
newDatastreamPath = getJCRPath(createResource(uriInfo.getBaseUri() + path + "/" + pid), subjects);
} else {
newDatastreamPath = path;
newDatastreamPath = jcrPath;
}


Expand Down Expand Up @@ -146,10 +151,6 @@ public Response create(@PathParam("path")
contentType.toString(), originalFileName, requestBodyStream,
checksumURI);

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

session.save();
versionService.nodeUpdated(datastream.getNode());

Expand Down Expand Up @@ -186,18 +187,23 @@ public Response modifyContent(@PathParam("path") final List<PathSegment> pathLis
throws RepositoryException, InvalidChecksumException, URISyntaxException, ParseException {

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

final String path = toPath(pathList);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
final MediaType contentType = getSimpleContentType(requestContentType);

if (nodeService.exists(session, path)) {
if (nodeService.exists(session, jcrPath)) {

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

evaluateRequestPreconditions(request, ds);
}

LOGGER.debug("create Datastream {}", path);
LOGGER.debug("PUT: Create Datastream {}", jcrPath);

final URI checksumURI;

Expand All @@ -217,7 +223,7 @@ public Response modifyContent(@PathParam("path") final List<PathSegment> pathLis
}

final Datastream datastream =
datastreamService.createDatastream(session, path,
datastreamService.createDatastream(session, jcrPath,
contentType.toString(), originalFileName, requestBodyStream, checksumURI);

final boolean isNew = datastream.isNew();
Expand All @@ -226,10 +232,6 @@ public Response modifyContent(@PathParam("path") final List<PathSegment> pathLis

ResponseBuilder builder;
if (isNew) {
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class,
uriInfo);

builder = created(new URI(subjects.getSubject(
datastream.getContentNode().getPath()).getURI()));
} else {
Expand Down Expand Up @@ -259,14 +261,17 @@ public Response getContent(@PathParam("path") final List<PathSegment> pathList,
@Context final HttpServletResponse servletResponse)
throws RepositoryException, IOException {
try {
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class,
uriInfo);

final String path = toPath(pathList);
LOGGER.info("Attempting get of {}.", path);
final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.info("GET: Attempting get {} from hierarchy path {}.", path, jcrPath);

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

return getDatastreamContentResponse(ds, rangeValue, request, servletResponse,
subjects);

Expand Down
100 changes: 54 additions & 46 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -156,10 +156,12 @@ public Response head(@PathParam("path") final List<PathSegment> pathList,
final String path = toPath(pathList);
LOGGER.trace("Getting head for: {}", path);

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

final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, this.getClass(), uriInfo);
new HttpIdentifierTranslator(session, this.getClass(), uriInfo);

final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("Head: Using auto hierarchy path {} to retrieve resource.", jcrPath);
final FedoraResource resource = nodeService.getObject(session, jcrPath);

checkCacheControlHeaders(request, servletResponse, resource);

Expand Down Expand Up @@ -194,14 +196,14 @@ public RdfStream describe(@PathParam("path") final List<PathSegment> pathList,
@Context final UriInfo uriInfo) throws RepositoryException {
final String path = toPath(pathList);
LOGGER.trace("Getting profile for: {}", path);

final FedoraResource resource = nodeService.getObject(session, path);
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 FedoraResource resource = nodeService.getObject(session, jcrPath);

checkCacheControlHeaders(request, servletResponse, resource);

final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, this.getClass(), uriInfo);

final RdfStream rdfStream =
resource.getTriples(subjects).session(session)
.topic(subjects.getSubject(resource.getNode().getPath())
Expand Down Expand Up @@ -355,19 +357,21 @@ public Response updateSparql(@PathParam("path")
LOGGER.debug("Attempting to update path: {}", path);

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

final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("PATCH: Using auto hierarchy path {} to retrieve resource.", jcrPath);
LOGGER.trace("Converted incoming path {} to path: {}", path, jcrPath);
if (requestBodyStream != null) {

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

evaluateRequestPreconditions(request, resource);

final Dataset properties = resource.updatePropertiesDataset(new HttpIdentifierTranslator(
session, FedoraNodes.class, uriInfo), IOUtils
final Dataset properties = resource.updatePropertiesDataset(subjects, IOUtils
.toString(requestBodyStream));


final Model problems = properties.getNamedModel(PROBLEMS_MODEL_NAME);
if (!problems.isEmpty()) {
LOGGER.info(
Expand Down Expand Up @@ -424,36 +428,34 @@ public Response createOrReplaceObjectRdf(


final MediaType contentType = getSimpleContentType(requestContentType);
final HttpIdentifierTranslator idTranslator =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

if (nodeService.exists(session, path)) {
resource = nodeService.getObject(session, path);
final String jcrPath = idTranslator.getPathFromSubject(createResource(uriInfo.getBaseUri() + path));
LOGGER.trace("PUT: Using auto hierarchy path {} to retrieve resource.", jcrPath);
if (nodeService.exists(session, jcrPath)) {
resource = nodeService.getObject(session, jcrPath);
response = noContent();
} else {
final MediaType effectiveContentType
= requestBodyStream == null || requestContentType == null ? null : contentType;
resource = createFedoraResource(null, effectiveContentType, path);
final HttpIdentifierTranslator idTranslator =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

resource = createFedoraResource(null, effectiveContentType, jcrPath);
final URI location = new URI(idTranslator.getSubject(resource.getNode().getPath()).getURI());

response = created(location).entity(location.toString());
}

evaluateRequestPreconditions(request, resource);

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

if (requestContentType != null && requestBodyStream != null) {
final String format = contentTypeToLang(contentType.toString()).getName().toUpperCase();

final Model inputModel = createDefaultModel()
.read(requestBodyStream,
graphSubjects.getSubject(resource.getNode().getPath()).toString(),
idTranslator.getSubject(resource.getNode().getPath()).toString(),
format);

resource.replaceProperties(graphSubjects, inputModel);
resource.replaceProperties(idTranslator, inputModel);

}

Expand Down Expand Up @@ -492,19 +494,19 @@ public Response createObject(@PathParam("path")
final UriInfo uriInfo, final InputStream requestBodyStream)
throws RepositoryException, ParseException, IOException,
InvalidChecksumException, URISyntaxException {

String pid;
final String newObjectPath;
final String path = toPath(pathList);

LOGGER.trace("POST: Attempting to created object in path: {}", path);
final HttpIdentifierTranslator idTranslator =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

final MediaType contentType = getSimpleContentType(requestContentType);

final String contentTypeString = contentType.toString();

assertPathExists(path);
final String jcrPath = idTranslator.getPathFromSubject(createResource(uriInfo.getBaseUri() + path));
assertPathExists(jcrPath);

if (slug != null && !slug.isEmpty()) {
pid = slug;
Expand All @@ -515,15 +517,18 @@ public Response createObject(@PathParam("path")
LOGGER.trace("Using external identifier {} to create new resource.", pid);
LOGGER.trace("Using prefixed external identifier {} to create new resource.", uriInfo.getBaseUri() + "/"
+ pid);
pid = idTranslator.getPathFromSubject(createResource(uriInfo.getBaseUri() + "/" + pid));
newObjectPath = idTranslator.getPathFromSubject(createResource(uriInfo.getBaseUri() +
path + (path.equals("/") ? "" : "/") + pid));
LOGGER.trace("Using auto hierarchy path {} to create new resource.", newObjectPath);
// remove leading slash left over from translation
pid = pid.substring(1, pid.length());
LOGGER.trace("Using internal identifier {} to create new resource.", pid);
newObjectPath = path + "/" + pid;
//pid = pid.substring(1, pid.length());

//newObjectPath = path + "/" + pid;

assertPathMissing(newObjectPath);

final FedoraResource object = nodeService.getObject(session, path);
final FedoraResource object = nodeService.getObject(session,
idTranslator.getPathFromSubject(createResource(uriInfo.getBaseUri() + path)));

if (object.getModels().contains(FEDORA_DATASTREAM)) {
throw new WebApplicationException(conflict().entity("Object cannot have child nodes").build());
Expand Down Expand Up @@ -711,14 +716,17 @@ public Response deleteObject(@PathParam("path")
@Context final Request request) throws RepositoryException {

try {
final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, this.getClass(), uriInfo);

final String path = toPath(pathList);

final String jcrPath = getJCRPath(createResource(uriInfo.getBaseUri() + path), subjects);
LOGGER.trace("DELETE: Using auto hierarchy path {} to retrieve resource.", jcrPath);
final FedoraResource resource =
nodeService.getObject(session, path);
nodeService.getObject(session, jcrPath);
evaluateRequestPreconditions(request, resource);

nodeService.deleteObject(session, path);
nodeService.deleteObject(session, jcrPath);
session.save();
return noContent().build();
} catch (javax.jcr.ReferentialIntegrityException riex) {
Expand Down Expand Up @@ -757,19 +765,20 @@ public Response copyObject(@PathParam("path") final List<PathSegment> path,

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

if (!nodeService.exists(session, toPath(path))) {
String srcPath = subjects.getPathFromSubject(createResource(uriInfo.getBaseUri() + toPath(path)));
LOGGER.trace("COPY: Using auto hierarchy path {} to retrieve source.", srcPath);
if (!nodeService.exists(session, srcPath)) {
return status(SC_CONFLICT).entity("The source path does not exist").build();
}

final String destination =
subjects.getPathFromSubject(ResourceFactory.createResource(destinationUri));

LOGGER.trace("COPY: Using auto hierarchy path {} to retrieve destination resource.", destination);
if (destination == null) {
return status(SC_BAD_GATEWAY).entity("Destination was not a valid resource path").build();
}

nodeService.copyObject(session, toPath(path), destination);
nodeService.copyObject(session, srcPath, destination);
session.save();
versionService.nodeUpdated(session, destination);
return created(new URI(destinationUri)).build();
Expand All @@ -795,28 +804,27 @@ public Response moveObject(@PathParam("path") final List<PathSegment> pathList,
@HeaderParam("Destination") final String destinationUri,
@Context final Request request)
throws RepositoryException, URISyntaxException {

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

final String path = toPath(pathList);

if (!nodeService.exists(session, path)) {
String srcPath = subjects.getPathFromSubject(createResource(uriInfo.getBaseUri() + path));
LOGGER.trace("MOVE: Using auto hierarchy path {} to retrieve source resource.", srcPath);
if (!nodeService.exists(session, srcPath)) {
return status(SC_CONFLICT).entity("The source path does not exist").build();
}


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


evaluateRequestPreconditions(request, resource);

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

final String destination =
subjects.getPathFromSubject(ResourceFactory.createResource(destinationUri));

LOGGER.trace("MOVE: Using auto hierarchy path {} to retrieve destination resource.", destination);
if (destination == null) {
return status(SC_BAD_GATEWAY).entity("Destination was not a valid resource path").build();
}
Expand Down

0 comments on commit 9b1ff36

Please sign in to comment.