Skip to content

Commit

Permalink
More minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed May 15, 2013
1 parent 4dfadc1 commit 0646968
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 296 deletions.
163 changes: 75 additions & 88 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraDatastreams.java
Expand Up @@ -37,14 +37,12 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.Datastream;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.jaxb.responses.access.ObjectDatastreams.DatastreamElement;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.utils.ContentDigest;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
import com.google.common.base.Function;
import com.sun.jersey.multipart.BodyPart;
import com.sun.jersey.multipart.BodyPartEntity;
import com.sun.jersey.multipart.MultiPart;
Expand All @@ -57,25 +55,25 @@ public class FedoraDatastreams extends AbstractResource {

@POST
@Timed
public Response modifyDatastreams(@PathParam("path") final List<PathSegment> pathList,
@QueryParam("delete") final List<String> dsidList,
final MultiPart multipart)
public Response modifyDatastreams(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("delete")
final List<String> dsidList, final MultiPart multipart)
throws RepositoryException, IOException, InvalidChecksumException {

final Session session = getAuthenticatedSession();
final String path = toPath(pathList);
try {
for (final String dsid : dsidList) {
logger.debug("Purging datastream: " + dsid);
nodeService.deleteObject(session, path + "/" + dsid);
nodeService.deleteObject(session, path + "/" + dsid);
}

for (final BodyPart part : multipart.getBodyParts()) {
final String dsid =
part.getContentDisposition().getParameters()
.get("name");
logger.debug("Adding datastream: " + dsid);
final String dsPath = path + "/" + dsid;
final String dsPath = path + "/" + dsid;
final Object obj = part.getEntity();
InputStream src = null;
if (obj instanceof BodyPartEntity) {
Expand All @@ -95,19 +93,18 @@ public Response modifyDatastreams(@PathParam("path") final List<PathSegment> pat
session.logout();
}
}

@DELETE
@Timed
public Response deleteDatastreams(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("dsid") final List<String> dsidList
) throws RepositoryException {
public Response deleteDatastreams(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("dsid")
final List<String> dsidList) throws RepositoryException {
final Session session = getAuthenticatedSession();
try {
String path = toPath(pathList);
final String path = toPath(pathList);
for (final String dsid : dsidList) {
logger.debug("purging datastream {}", path + "/" + dsid);
nodeService.deleteObject(session, path + "/" + dsid);
logger.debug("purging datastream {}", path + "/" + dsid);
nodeService.deleteObject(session, path + "/" + dsid);
}
session.save();
return noContent().build();
Expand All @@ -120,103 +117,93 @@ public Response deleteDatastreams(
@Path("/__content__")
@Produces("multipart/mixed")
@Timed
public Response getDatastreamsContents(@PathParam("path") List<PathSegment> pathList,
@QueryParam("dsid") final List<String> requestedDsids,
@Context final Request request) throws RepositoryException, IOException, NoSuchAlgorithmException {

final Session session = getAuthenticatedSession();

final ArrayList<Datastream> datastreams = new ArrayList<Datastream>();
public Response getDatastreamsContents(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("dsid")
final List<String> requestedDsids, @Context
final Request request) throws RepositoryException, IOException,
NoSuchAlgorithmException {

try {
String path = toPath(pathList);
// TODO: wrap some of this JCR logic in an fcrepo abstraction;

final Node node = nodeService.getObject(session, path).getNode();
final Session session = getAuthenticatedSession();

final ArrayList<Datastream> datastreams = new ArrayList<Datastream>();

Date date = new Date();
try {
final String path = toPath(pathList);
// TODO: wrap some of this JCR logic in an fcrepo abstraction;

final MessageDigest digest = MessageDigest.getInstance("SHA-1");
final Node node = nodeService.getObject(session, path).getNode();

Date date = new Date();

final NodeIterator ni;
final MessageDigest digest = MessageDigest.getInstance("SHA-1");

if (requestedDsids.isEmpty()) {
ni = node.getNodes();
} else {
ni = node.getNodes(requestedDsids.toArray(new String[requestedDsids.size()]));
}
final NodeIterator ni;

// transform the nodes into datastreams, and calculate cache header data
while (ni.hasNext()) {
if (requestedDsids.isEmpty()) {
ni = node.getNodes();
} else {
ni =
node.getNodes(requestedDsids
.toArray(new String[requestedDsids.size()]));
}

final Node dsNode = ni.nextNode();
final Datastream ds = datastreamService.asDatastream(dsNode);
// transform the nodes into datastreams, and calculate cache header data
while (ni.hasNext()) {

if (!ds.hasContent()) {
continue;
}
final Node dsNode = ni.nextNode();
final Datastream ds = datastreamService.asDatastream(dsNode);

digest.update(ds.getContentDigest().toString().getBytes());
if (!ds.hasContent()) {
continue;
}

if (ds.getLastModifiedDate().after(date)) {
date = ds.getLastModifiedDate();
}
digest.update(ds.getContentDigest().toString().getBytes());

datastreams.add(ds);
}
if (ds.getLastModifiedDate().after(date)) {
date = ds.getLastModifiedDate();
}

datastreams.add(ds);
}

final URI digestURI = ContentDigest.asURI(digest.getAlgorithm(), digest.digest());
final EntityTag etag = new EntityTag(digestURI.toString());
final URI digestURI =
ContentDigest.asURI(digest.getAlgorithm(), digest.digest());
final EntityTag etag = new EntityTag(digestURI.toString());

final Date roundedDate = new Date();
roundedDate.setTime(date.getTime() - date.getTime() % 1000);
final Date roundedDate = new Date();
roundedDate.setTime(date.getTime() - date.getTime() % 1000);

Response.ResponseBuilder builder = request.evaluatePreconditions(roundedDate, etag);
Response.ResponseBuilder builder =
request.evaluatePreconditions(roundedDate, etag);

final CacheControl cc = new CacheControl();
cc.setMaxAge(0);
cc.setMustRevalidate(true);
final CacheControl cc = new CacheControl();
cc.setMaxAge(0);
cc.setMustRevalidate(true);

if (builder == null) {
final MultiPart multipart = new MultiPart();
if (builder == null) {
final MultiPart multipart = new MultiPart();

for (Datastream ds : datastreams) {
multipart.bodyPart(ds.getContent(), MediaType.valueOf(ds.getMimeType()));
}
for (final Datastream ds : datastreams) {
multipart.bodyPart(ds.getContent(), MediaType.valueOf(ds
.getMimeType()));
}

builder = Response.ok(multipart, MULTIPART_FORM_DATA);
}
builder = Response.ok(multipart, MULTIPART_FORM_DATA);
}

return builder.cacheControl(cc).lastModified(date).tag(etag).build();
return builder.cacheControl(cc).lastModified(date).tag(etag)
.build();

} finally {
session.logout();
}
} finally {
session.logout();
}
}

private Function<Datastream, DatastreamElement> ds2dsElement =
new Function<Datastream, DatastreamElement>() {

@Override
public DatastreamElement apply(final Datastream ds) {
try {
return new DatastreamElement(ds.getDsId(),
ds.getDsId(), ds.getMimeType());
} catch (final RepositoryException e) {
throw new IllegalStateException(e);
}
}
};


public DatastreamService getDatastreamService() {
return datastreamService;
}
public DatastreamService getDatastreamService() {
return datastreamService;
}

public void setDatastreamService(final DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}
public void setDatastreamService(final DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}
}
30 changes: 15 additions & 15 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraRepository.java
Expand Up @@ -8,6 +8,7 @@
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.ok;
import static org.fcrepo.services.RepositoryService.getRepositoryNamespaces;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class FedoraRepository extends AbstractResource {

@GET
@Path("modeshape")
@Timed
@Timed
public Response describeModeshape() throws IOException, RepositoryException {
final Session session = getAuthenticatedSession();
logger.debug("Repository name: " + repo.getDescriptor(REP_NAME_DESC));
Expand All @@ -63,7 +64,7 @@ public Response describeModeshape() throws IOException, RepositoryException {

// add in node namespaces
final Builder<String, String> namespaces = builder();
namespaces.putAll(objectService.getRepositoryNamespaces(session));
namespaces.putAll(getRepositoryNamespaces(session));
repoproperties.put("node.namespaces", namespaces.build());

// add in node types
Expand All @@ -79,23 +80,23 @@ public Response describeModeshape() throws IOException, RepositoryException {
}

@GET
@Timed
@Timed
@Produces({TEXT_XML, APPLICATION_XML, APPLICATION_JSON})
public DescribeRepository describe() throws RepositoryException {

final Session session = getAuthenticatedSession();
final DescribeRepository description = new DescribeRepository();
description.repositoryBaseURL = uriInfo.getBaseUri();
description.sampleOAIURL =
uriInfo.getBaseUriBuilder().path("/123/oai_dc")
.build();
uriInfo.getBaseUriBuilder().path("/123/oai_dc").build();
description.repositorySize = objectService.getRepositorySize();
description.numberOfObjects =
objectService.getRepositoryObjectCount(session);

Map<String, String> clusterConfig = getClusterConfig();
if(clusterConfig != null) {
description.setClusterConfiguration(new DescribeCluster(clusterConfig));
final Map<String, String> clusterConfig = getClusterConfig();
if (clusterConfig != null) {
description.setClusterConfiguration(new DescribeCluster(
clusterConfig));
}

session.logout();
Expand All @@ -107,14 +108,14 @@ public DescribeRepository describe() throws RepositoryException {
* @return
*/
private Map<String, String> getClusterConfig() {
//get infinispan binarystore and cachemanager to set cluster configuration information
GetClusterConfiguration getClusterConfig =
new GetClusterConfiguration();
return getClusterConfig.apply(repo);
//get infinispan binarystore and cachemanager to set cluster configuration information
final GetClusterConfiguration getClusterConfig =
new GetClusterConfiguration();
return getClusterConfig.apply(repo);
}

@GET
@Timed
@Timed
@Produces(TEXT_HTML)
public String describeHtml() throws RepositoryException {

Expand All @@ -130,8 +131,7 @@ public void setRepository(final Repository repo) {
this.repo = repo;
}


public void setObjectService(ObjectService objectService) {
public void setObjectService(final ObjectService objectService) {
this.objectService = objectService;
}

Expand Down

0 comments on commit 0646968

Please sign in to comment.