Skip to content

Commit

Permalink
Switched JAX-RS methods to use ServiceHelpers code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Mar 25, 2013
1 parent 3af43f8 commit c268085
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 98 deletions.
Expand Up @@ -10,6 +10,7 @@
import static javax.ws.rs.core.Response.noContent;
import static org.fcrepo.jaxb.responses.management.DatastreamProfile.DatastreamStates.A;
import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -57,15 +58,13 @@
import org.fcrepo.utils.DatastreamIterator;
import org.fcrepo.utils.FixityResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableSet.Builder;

@Path("/objects/{pid}/datastreams")
public class FedoraDatastreams extends AbstractResource {

final private Logger logger = LoggerFactory
.getLogger(FedoraDatastreams.class);
final private Logger logger = getLogger(FedoraDatastreams.class);

@Inject
DatastreamService datastreamService;
Expand Down
Expand Up @@ -8,6 +8,7 @@
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.jaxb.responses.access.ObjectProfile.ObjectStates.A;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;

Expand All @@ -29,13 +30,11 @@
import org.fcrepo.jaxb.responses.access.ObjectProfile;
import org.fcrepo.services.ObjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Path("/objects")
public class FedoraObjects extends AbstractResource {

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

@Inject
ObjectService objectService;
Expand Down
2 changes: 1 addition & 1 deletion fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -9,7 +9,7 @@
import static java.security.MessageDigest.getInstance;
import static org.fcrepo.services.LowLevelStorageService.getFixity;
import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.fcrepo.services.RepositoryService.getNodePropertySize;
import static org.fcrepo.services.ServiceHelpers.getNodePropertySize;
import static org.fcrepo.services.RepositoryService.metrics;
import static org.fcrepo.utils.FedoraTypesUtils.map;
import static org.fcrepo.utils.FedoraTypesUtils.value2string;
Expand Down
4 changes: 2 additions & 2 deletions fcrepo-kernel/src/main/java/org/fcrepo/FedoraObject.java
Expand Up @@ -4,6 +4,7 @@
import static com.google.common.base.Joiner.on;
import static com.yammer.metrics.MetricRegistry.name;
import static org.fcrepo.services.RepositoryService.metrics;
import static org.fcrepo.services.ServiceHelpers.getNodePropertySize;
import static org.fcrepo.utils.FedoraTypesUtils.isOwned;
import static org.fcrepo.utils.FedoraTypesUtils.map;
import static org.fcrepo.utils.FedoraTypesUtils.nodetype2name;
Expand All @@ -19,7 +20,6 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.services.RepositoryService;
import org.fcrepo.utils.FedoraJcrTypes;
import org.modeshape.jcr.api.JcrTools;

Expand Down Expand Up @@ -126,7 +126,7 @@ public Collection<String> getModels() throws RepositoryException {
* @throws RepositoryException
*/
static Long getObjectSize(Node obj) throws RepositoryException {
return RepositoryService.getNodePropertySize(obj) + getObjectDSSize(obj);
return getNodePropertySize(obj) + getObjectDSSize(obj);
}

/**
Expand Down
Expand Up @@ -51,7 +51,8 @@ public Node createDatastreamNode(final Session session,

public Node getDatastreamNode(final String pid, final String dsId)
throws RepositoryException {
logger.trace("Executing getDatastreamNode() with pid: {} and dsId: {}", pid, dsId);
logger.trace("Executing getDatastreamNode() with pid: {} and dsId: {}",
pid, dsId);
final Node dsNode = getDatastream(pid, dsId).getNode();
logger.trace("Retrieved datastream node: {}", dsNode.getName());
return dsNode;
Expand All @@ -64,29 +65,27 @@ public Datastream getDatastream(final String pid, final String dsId)

public void purgeDatastream(final Session session, final String pid,
final String dsId) throws RepositoryException {
Datastream ds = new Datastream(session, pid, dsId);
ds.purge();
new Datastream(session, pid, dsId).purge();
}

public DatastreamIterator getDatastreamsFor(final String pid,
final Session session) throws RepositoryException {
return new DatastreamIterator(
new FedoraObject(session, getObjectJcrNodePath(pid)).getNode()
.getNodes());
return new DatastreamIterator(new FedoraObject(session,
getObjectJcrNodePath(pid)).getNode().getNodes());
}

public DatastreamIterator getDatastreamsFor(final String pid)
throws RepositoryException {
return getDatastreamsFor(pid, readOnlySession);
}

public boolean exists(String pid, String dsId) throws RepositoryException {
return exists(pid, dsId, readOnlySession);
return exists(pid, dsId, readOnlySession);
}
public boolean exists(String pid, String dsId, Session session) throws RepositoryException {
String dspath = getDatastreamJcrNodePath(pid, dsId);
return session.nodeExists(dspath);

public boolean exists(String pid, String dsId, Session session)
throws RepositoryException {
return session.nodeExists(getDatastreamJcrNodePath(pid, dsId));
}

}
@@ -1,8 +1,9 @@

package org.fcrepo.services;

import static org.slf4j.LoggerFactory.getLogger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Convenience class for constructing Fedora-related paths in the JCR repository.
Expand All @@ -14,8 +15,7 @@ public class PathService {

public final static String OBJECT_PATH = "/objects";

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

/**
* @param pid
Expand Down
@@ -1,6 +1,5 @@
package org.fcrepo.services;

import static com.google.common.collect.ImmutableSet.copyOf;
import static javax.jcr.query.Query.JCR_SQL2;

import java.io.PrintStream;
Expand All @@ -11,7 +10,6 @@
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand Down Expand Up @@ -153,21 +151,4 @@ public void setRepository(Repository repository) {
getSession();
}

public static Long getNodePropertySize(Node node)
throws RepositoryException {
Long size = 0L;
PropertyIterator i = node.getProperties();
while (i.hasNext()) {
Property p = i.nextProperty();
if (p.isMultiple()) {
for (Value v : copyOf(p.getValues())) {
size = size + v.getBinary().getSize();
}
} else {
size = size + p.getBinary().getSize();
}
}
return size;
}

}
75 changes: 36 additions & 39 deletions fcrepo-kernel/src/main/java/org/fcrepo/services/ServiceHelpers.java
@@ -1,6 +1,6 @@

package org.fcrepo.services;

import static com.google.common.collect.ImmutableSet.copyOf;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

Expand All @@ -14,57 +14,54 @@
import javax.jcr.ValueFormatException;

public class ServiceHelpers {

public static Long getNodePropertySize(Node node)
throws RepositoryException {
Long size = 0L;
PropertyIterator i = node.getProperties();
while (i.hasNext()) {
Property p = i.nextProperty();
for (final PropertyIterator i = node.getProperties(); i.hasNext();) {
final Property p = i.nextProperty();
if (p.isMultiple()) {
for (Value v : copyOf(p.getValues())) {
size = size + v.getBinary().getSize();
for (Value v : p.getValues()) {
size += v.getBinary().getSize();
}
} else {
size = size + p.getBinary().getSize();
size += p.getBinary().getSize();
}
}
return size;
}

/**
* @param obj
* @return object size in bytes
* @throws RepositoryException
*/
public static Long getObjectSize(Node obj) throws RepositoryException {
return getNodePropertySize(obj) + getObjectDSSize(obj);
}
/**
* @param obj
* @return object size in bytes
* @throws RepositoryException
*/
public static Long getObjectSize(Node obj) throws RepositoryException {
return getNodePropertySize(obj) + getObjectDSSize(obj);
}

/**
* @param obj
* @return object's datastreams' total size in bytes
* @throws RepositoryException
*/
private static Long getObjectDSSize(Node obj) throws RepositoryException {
Long size = 0L;
NodeIterator i = obj.getNodes();
while (i.hasNext()) {
Node ds = i.nextNode();
size += getDatastreamSize(ds);
}
return size;
}
/**
* @param obj
* @return object's datastreams' total size in bytes
* @throws RepositoryException
*/
private static Long getObjectDSSize(Node obj) throws RepositoryException {
Long size = 0L;
for (final NodeIterator i = obj.getNodes(); i.hasNext();) {
size += getDatastreamSize(i.nextNode());
}
return size;
}

public static Long getDatastreamSize(Node ds) throws ValueFormatException,
PathNotFoundException, RepositoryException {
return getNodePropertySize(ds) + getContentSize(ds);
}
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();
}
public static Long getContentSize(Node ds) throws ValueFormatException,
PathNotFoundException, RepositoryException {
return ds.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getSize();
}

}
Expand Up @@ -10,10 +10,11 @@
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.api.legacy.FedoraObjects.getObjectSize;
import static org.fcrepo.jaxb.responses.management.DatastreamProfile.convertDateToXSDString;
import static org.fcrepo.jaxb.responses.management.DatastreamProfile.DatastreamStates.A;
import static org.fcrepo.jaxb.responses.management.DatastreamProfile.DatastreamControlGroup.M;
import static org.fcrepo.jaxb.responses.management.DatastreamProfile.DatastreamStates.A;
import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.fcrepo.services.PathService.getObjectJcrNodePath;
import static org.fcrepo.services.ServiceHelpers.getNodePropertySize;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

Expand Down Expand Up @@ -56,7 +57,6 @@
import org.fcrepo.jaxb.responses.management.DatastreamProfile;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.services.RepositoryService;
import org.modeshape.jcr.api.Binary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -68,7 +68,7 @@ public class FedoraDatastreams extends AbstractResource {

final private Logger logger = LoggerFactory
.getLogger(FedoraDatastreams.class);

@Inject
ObjectService objectService;

Expand Down Expand Up @@ -121,8 +121,9 @@ public Response addDatastreams(@PathParam("pid")
final String dsid =
a.getContentDisposition().getParameter("name");
final String dsPath = getDatastreamJcrNodePath(pid, dsid);
datastreamService.createDatastreamNode(session, dsPath, a.getDataHandler()
.getContentType(), a.getDataHandler().getInputStream());
datastreamService.createDatastreamNode(session, dsPath, a
.getDataHandler().getContentType(), a.getDataHandler()
.getInputStream());

}
session.save();
Expand Down Expand Up @@ -249,7 +250,8 @@ public Response addDatastream(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid, @HeaderParam("Content-Type")
MediaType contentType, @Multipart("file")
Attachment file) throws RepositoryException, IOException, InvalidChecksumException {
Attachment file) throws RepositoryException, IOException,
InvalidChecksumException {
return addDatastream(pid, dsid, file.getContentType(), file
.getDataHandler().getInputStream());
}
Expand Down Expand Up @@ -312,7 +314,8 @@ public Response modifyDatastream(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid, @HeaderParam("Content-Type")
MediaType contentType, @Multipart("file")
Attachment file) throws RepositoryException, IOException, InvalidChecksumException {
Attachment file) throws RepositoryException, IOException,
InvalidChecksumException {

return modifyDatastream(pid, dsid, file.getContentType(), file
.getDataHandler().getInputStream());
Expand All @@ -321,14 +324,15 @@ public Response modifyDatastream(@PathParam("pid")

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

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);
datastreamService.createDatastreamNode(session, dsPath, contentType
.toString(), requestBodyStream);
boolean created = session.nodeExists(dsPath);
session.save();
if (created) {
Expand Down Expand Up @@ -467,7 +471,8 @@ public Response deleteDatastream(@PathParam("pid")
final String dsPath = getDatastreamJcrNodePath(pid, dsid);
final Session session = repo.login();
final Node ds = session.getNode(dsPath);
datastreamService.updateRepositorySize(0L - getDatastreamSize(ds), session);
datastreamService.updateRepositorySize(0L - getDatastreamSize(ds),
session);
return deleteResource(ds);
}

Expand All @@ -493,7 +498,7 @@ private DatastreamProfile getDSProfile(Datastream ds)
// TODO do something about format URI, or deprecate it
dsProfile.dsFormatURI = URI.create("info:/nothing");
dsProfile.dsSize =
RepositoryService.getNodePropertySize(ds.getNode()) + ds.getContentSize();
getNodePropertySize(ds.getNode()) + ds.getContentSize();
dsProfile.dsCreateDate =
convertDateToXSDString(ds.getCreatedDate().getTime());
// TODO what _is_ a dsInfoType?
Expand All @@ -513,7 +518,7 @@ private String getDSMimeType(Node ds) throws ValueFormatException,

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

public static Long getContentSize(Node ds) throws ValueFormatException,
Expand Down

0 comments on commit c268085

Please sign in to comment.