Skip to content

Commit

Permalink
add etag/last-modified headers to the datastream content request. use…
Browse files Browse the repository at this point in the history
… the sha1 as the etag
  • Loading branch information
cbeer committed Mar 8, 2013
1 parent c00ad05 commit 1c2dd50
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -39,8 +40,13 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
Expand Down Expand Up @@ -351,10 +357,24 @@ public DatastreamProfile getDatastream(@PathParam("pid")
@Path("/{dsid}/content")
public Response getDatastreamContent(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException {
final String dsid, @Context Request request) throws RepositoryException {

final Datastream ds = DatastreamService.getDatastream(pid, dsid);
return ok(ds.getContent(), ds.getMimeType()).build();

EntityTag etag = new EntityTag(ds.getContentDigest().toString());
Date date = ds.getLastModifiedDate();
ResponseBuilder builder = request.evaluatePreconditions(date, etag);


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

if(builder == null) {
builder = Response.ok(ds.getContent(), ds.getMimeType());
}

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

/**
Expand Down
Expand Up @@ -7,6 +7,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import junit.framework.Assert;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
Expand Down Expand Up @@ -117,10 +119,14 @@ public void testGetDatastreamContent() throws Exception {
new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest6/datastreams/ds1/content");
assertEquals(200, getStatus(method_test_get));
final HttpResponse response = client.execute(method_test_get);
logger.debug("Returned from HTTP GET, now checking content...");
assertTrue("Got the wrong content back!", "marbles for everyone"
.equals(EntityUtils.toString(client.execute(method_test_get)
.equals(EntityUtils.toString(response
.getEntity())));

assertEquals("ETag: \"urn:sha1:ba6cb22191300aebcfcfb83de9635d6b224677df\"", response.getFirstHeader("ETag").toString());

logger.debug("Content was correct.");
}

Expand Down
4 changes: 4 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -214,4 +214,8 @@ public Date getCreatedDate() throws RepositoryException {
.getTimeInMillis());
}

public Date getLastModifiedDate() throws RepositoryException {
return new Date(node.getProperty("jcr:lastModified").getDate().getTimeInMillis());
}

}

0 comments on commit 1c2dd50

Please sign in to comment.