Skip to content

Commit

Permalink
unit testing fcrepo-http-api/org.fcrepo.api.FedoraDatastreams
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Mar 24, 2013
1 parent 29150da commit df87e11
Show file tree
Hide file tree
Showing 19 changed files with 712 additions and 371 deletions.
174 changes: 57 additions & 117 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraDatastreams.java
Expand Up @@ -8,7 +8,7 @@
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.noContent;
import static org.fcrepo.api.FedoraObjects.getObjectSize;
import static javax.ws.rs.core.Response.serverError;
import static org.fcrepo.jaxb.responses.management.DatastreamProfile.DatastreamStates.A;
import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.fcrepo.services.PathService.getObjectJcrNodePath;
Expand Down Expand Up @@ -60,7 +60,7 @@
import org.fcrepo.jaxb.responses.management.DatastreamHistory;
import org.fcrepo.jaxb.responses.management.DatastreamProfile;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.utils.DatastreamIterator;
import org.fcrepo.utils.FixityResult;
import org.modeshape.jcr.api.Binary;
import org.slf4j.Logger;
Expand All @@ -74,9 +74,6 @@ public class FedoraDatastreams extends AbstractResource {
final private Logger logger = LoggerFactory
.getLogger(FedoraDatastreams.class);

@Inject
ObjectService objectService;

@Inject
DatastreamService datastreamService;

Expand All @@ -100,11 +97,13 @@ public ObjectDatastreams getDatastreams(@PathParam("pid")
final ObjectDatastreams objectDatastreams = new ObjectDatastreams();
final Builder<DatastreamElement> datastreams = builder();

NodeIterator i = datastreamService.getDatastreamsFor(pid);
DatastreamIterator i = datastreamService.getDatastreamsFor(pid);
while (i.hasNext()) {
final Node ds = i.nextNode();
datastreams.add(new DatastreamElement(ds.getName(), ds.getName(),
getDSMimeType(ds)));
final Datastream ds = i.nextDatastream();
datastreams.add(new DatastreamElement(
ds.getDsId(),
ds.getDsId(),
ds.getMimeType()));
}
objectDatastreams.datastreams = datastreams.build();
return objectDatastreams;
Expand All @@ -120,9 +119,6 @@ public Response modifyDatastreams(@PathParam("pid")

final Session session = repo.login();
try {
Long oldObjectSize =
getObjectSize(session.getNode(getObjectJcrNodePath(pid)));

for (String dsid : dsidList) {
logger.debug("purging datastream " + dsid);
datastreamService.purgeDatastream(session, pid, dsid);
Expand All @@ -138,17 +134,6 @@ public Response modifyDatastreams(@PathParam("pid")

}
session.save();

/*
* we save before updating the repo size because the act of
* persisting session state creates new system-curated nodes and
* properties which contribute to the footprint of this resource
*/
updateRepositorySize(getObjectSize(session
.getNode(getObjectJcrNodePath(pid))) -
oldObjectSize, session);
// now we save again to persist the repo size
session.save();
} finally {
session.logout();
}
Expand Down Expand Up @@ -182,8 +167,6 @@ public MultipartBody getDatastreamsContents(@PathParam("pid")
final String pid, @QueryParam("dsid")
List<String> dsids) throws RepositoryException, IOException {

final Session session = repo.login();

if (dsids.isEmpty()) {
NodeIterator ni = objectService.getObjectNode(pid).getNodes();
while (ni.hasNext()) {
Expand All @@ -192,22 +175,19 @@ public MultipartBody getDatastreamsContents(@PathParam("pid")
}

List<Attachment> atts = new LinkedList<Attachment>();
try {
Iterator<String> i = dsids.iterator();
while (i.hasNext()) {
final String dsid = i.next();

try {
final Datastream ds =
datastreamService.getDatastream(pid, dsid);
atts.add(new Attachment(ds.getNode().getName(), ds
.getMimeType(), ds.getContent()));
} catch (PathNotFoundException e) {

}
}
} finally {
session.logout();

Iterator<String> i = dsids.iterator();
while (i.hasNext()) {
final String dsid = i.next();

try {
final Datastream ds =
datastreamService.getDatastream(pid, dsid);
atts.add(new Attachment(ds.getDsId(), ds
.getMimeType(), ds.getContent()));
} catch (PathNotFoundException e) {

}
}

return new MultipartBody(atts, true);
Expand All @@ -227,6 +207,7 @@ public MultipartBody getDatastreamsContents(@PathParam("pid")
* @return 201 Created
* @throws RepositoryException
* @throws IOException
* @throws InvalidChecksumException
*/
@POST
@Path("/{dsid}")
Expand All @@ -236,25 +217,28 @@ public Response addDatastream(@PathParam("pid")
final String checksum, @PathParam("dsid")
final String dsid, @HeaderParam("Content-Type")
MediaType contentType, InputStream requestBodyStream)
throws RepositoryException, IOException {
final Session session = repo.login();

contentType =
contentType != null ? contentType
: APPLICATION_OCTET_STREAM_TYPE;
String dspath = getDatastreamJcrNodePath(pid, dsid);

if (!session.nodeExists(dspath)) {
return created(
addDatastreamNode(pid, dspath, contentType,
requestBodyStream, session, checksumType, checksum)).build();
} else {
session.getNode(dspath).remove();
throws IOException, InvalidChecksumException {
if (contentType == null) contentType = APPLICATION_OCTET_STREAM_TYPE;

String dsPath = getDatastreamJcrNodePath(pid, dsid);
logger.info("addDatastream {}", dsPath);
try{
final Session session = repo.login();
if (!datastreamService.exists(pid, dsid, session)) {
datastreamService.createDatastreamNode(
session, dsPath, contentType.toString(),
requestBodyStream, checksumType, checksum);
} else {
datastreamService.createDatastreamNode(
session, dsPath, contentType.toString(),
requestBodyStream, checksumType, checksum);
}
session.save();
return created(
addDatastreamNode(pid, dspath, contentType,
requestBodyStream, session, checksumType, checksum)).build();
} catch (RepositoryException e) {
logger.error(e.getMessage(), e);
return serverError().build();
}
return created(uriInfo.getAbsolutePath()).build();

}

Expand All @@ -272,14 +256,15 @@ public Response addDatastream(@PathParam("pid")
* @return 201 Created
* @throws RepositoryException
* @throws IOException
* @throws InvalidChecksumException
*/
@POST
@Path("/{dsid}")
public Response addDatastream(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid, @HeaderParam("Content-Type")
MediaType contentType, InputStream requestBodyStream)
throws RepositoryException, IOException {
throws RepositoryException, IOException, InvalidChecksumException {

return addDatastream(pid, null, null, dsid, contentType, requestBodyStream);

Expand All @@ -299,61 +284,28 @@ public Response addDatastream(@PathParam("pid")
* @return 201 Created
* @throws RepositoryException
* @throws IOException
* @throws InvalidChecksumException
*/
@PUT
@Path("/{dsid}")
public Response modifyDatastream(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid, @HeaderParam("Content-Type")
MediaType contentType, InputStream requestBodyStream)
throws RepositoryException, IOException {
throws RepositoryException, IOException, InvalidChecksumException {
final Session session = repo.login();
contentType =
contentType != null ? contentType
: APPLICATION_OCTET_STREAM_TYPE;
String dspath = getDatastreamJcrNodePath(pid, dsid);
String dsPath = getDatastreamJcrNodePath(pid, dsid);

return created(
addDatastreamNode(pid, dspath, contentType, requestBodyStream,
session, null, null)).build();
datastreamService.createDatastreamNode(session, dsPath,
contentType.toString(), requestBodyStream);
session.save();
return created(uriInfo.getAbsolutePath()).build();

}

private URI addDatastreamNode(final String pid, final String dsPath,
final MediaType contentType, final InputStream requestBodyStream,
final Session session, String checksumType, String checksum) throws RepositoryException, IOException {

Long oldObjectSize =
getObjectSize(session.getNode(getObjectJcrNodePath(pid)));
logger.debug("Attempting to add datastream node at path: " + dsPath);
try {
datastreamService.createDatastreamNode(session, dsPath, contentType.toString(),
requestBodyStream, checksumType, checksum);
boolean created = session.nodeExists(dsPath);
session.save();
if (created) {
/*
* we save before updating the repo size because the act of
* persisting session state creates new system-curated nodes and
* properties which contribute to the footprint of this resource
*/
updateRepositorySize(getObjectSize(session
.getNode(getObjectJcrNodePath(pid))) -
oldObjectSize, session);
// now we save again to persist the repo size
session.save();
}
logger.debug("Finished adding datastream node at path: " + dsPath);
} catch (InvalidChecksumException e) {
logger.error("Checksum Mismatch Exception");
logger.debug("No datastream has been added");
session.logout();
} finally {
session.logout();
}
return uriInfo.getAbsolutePath();
}

/**
* Get the datastream profile of a datastream
*
Expand All @@ -374,7 +326,7 @@ public DatastreamProfile getDatastream(@PathParam("pid")
final String dsId) throws RepositoryException, IOException {
logger.trace("Executing getDatastream() with dsId: " + dsId);
Datastream ds = datastreamService.getDatastream(pid, dsId);
logger.debug("Retrieved dsNode: " + ds.getNode().getName());
logger.debug("Retrieved dsNode: " + ds.getDsId());
return getDSProfile(ds);

}
Expand Down Expand Up @@ -503,11 +455,11 @@ public DatastreamFixity getDatastreamFixity(@PathParam("pid")
public Response deleteDatastream(@PathParam("pid")
String pid, @PathParam("dsid")
String dsid) throws RepositoryException {
final String dsPath = getDatastreamJcrNodePath(pid, dsid);
final Session session = repo.login();
final Node ds = session.getNode(dsPath);
updateRepositorySize(0L - getDatastreamSize(ds), session);
return deleteResource(ds);
datastreamService.purgeDatastream(session, pid, dsid);
session.save();
session.logout();
return noContent().build();
}

private DatastreamProfile getDSProfile(Datastream ds)
Expand All @@ -525,8 +477,7 @@ private DatastreamProfile getDSProfile(Datastream ds)
dsProfile.dsChecksum = ds.getContentDigest();
dsProfile.dsState = A;
dsProfile.dsMIME = ds.getMimeType();
dsProfile.dsSize =
getNodePropertySize(ds.getNode()) + ds.getContentSize();
dsProfile.dsSize = ds.getSize();
dsProfile.dsCreateDate = ds.getCreatedDate().toString();
return dsProfile;
}
Expand All @@ -539,15 +490,4 @@ private String getDSMimeType(Node ds) throws ValueFormatException,
return b.getMimeType();
}

public static Long getDatastreamSize(Node ds) throws ValueFormatException,
PathNotFoundException, RepositoryException {
return getNodePropertySize(ds) + getContentSize(ds);
}

public static Long getContentSize(Node ds) throws ValueFormatException,
PathNotFoundException, RepositoryException {
return ds.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getSize();
}

}
32 changes: 12 additions & 20 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraObjects.java
Expand Up @@ -6,10 +6,9 @@
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.api.FedoraDatastreams.getContentSize;
import static org.fcrepo.jaxb.responses.access.ObjectProfile.ObjectStates.A;
import static org.fcrepo.services.PathService.getObjectJcrNodePath;
import static org.fcrepo.utils.FedoraJcrTypes.DC_TITLE;
import static org.fcrepo.utils.FedoraTypesUtils.map;
import static org.fcrepo.utils.FedoraTypesUtils.nodetype2name;
Expand All @@ -34,9 +33,11 @@
import javax.ws.rs.core.Response;

import org.fcrepo.AbstractResource;
import org.fcrepo.Datastream;
import org.fcrepo.FedoraObject;
import org.fcrepo.jaxb.responses.access.ObjectProfile;
import org.fcrepo.services.ObjectService;
import org.fcrepo.services.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -113,21 +114,13 @@ public Response modify(@PathParam("pid")
public Response ingest(@PathParam("pid")
final String pid) throws RepositoryException {

logger.debug("Attempting to ingest with pid: " + pid);
logger.debug("Attempting to ingest with pid: {}", pid);

final Session session = repo.login();
try {
final Node obj = objectService.createObjectNode(session, pid);
objectService.createObjectNode(session, pid);
session.save();
/*
* we save before updating the repo size because the act of
* persisting session state creates new system-curated nodes and
* properties which contribute to the footprint of this resource
*/
updateRepositorySize(getObjectSize(obj), session);
// now we save again to persist the repo size
session.save();
logger.debug("Finished ingest with pid: " + pid);
logger.debug("Finished ingest with pid: {}", pid);
return created(uriInfo.getAbsolutePath()).entity(pid).build();

} finally {
Expand Down Expand Up @@ -188,9 +181,9 @@ public ObjectProfile getObject(@PathParam("pid")
public Response deleteObject(@PathParam("pid")
final String pid) throws RepositoryException {
final Session session = repo.login();
final Node obj = session.getNode(getObjectJcrNodePath(pid));
updateRepositorySize(0L - getObjectSize(obj), session);
return deleteResource(obj);
objectService.deleteObject(pid, session);
session.save();
return noContent().build();
}

/**
Expand All @@ -199,7 +192,7 @@ public Response deleteObject(@PathParam("pid")
* @throws RepositoryException
*/
static Long getObjectSize(Node obj) throws RepositoryException {
return getNodePropertySize(obj) + getObjectDSSize(obj);
return RepositoryService.getNodePropertySize(obj) + getObjectDSSize(obj);
}

/**
Expand All @@ -211,9 +204,8 @@ private static Long getObjectDSSize(Node obj) throws RepositoryException {
Long size = 0L;
NodeIterator i = obj.getNodes();
while (i.hasNext()) {
Node ds = i.nextNode();
size = size + getNodePropertySize(ds);
size = size + getContentSize(ds);
Datastream ds = new Datastream(i.nextNode());
size += ds.getSize();
}
return size;
}
Expand Down

0 comments on commit df87e11

Please sign in to comment.