Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add the datastreams/__content__ endpoint to be filtered by dsid
  • Loading branch information
cbeer committed Feb 22, 2013
1 parent 76af2fc commit 819a1a0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -29,13 +30,15 @@
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

Expand Down Expand Up @@ -132,17 +135,29 @@ public Response addDatastreams(@PathParam("pid")
@GET
@Path("/__content__")
@Produces("multipart/mixed")
public MultipartBody getDatastreamsContents(@PathParam("pid") final String pid) throws RepositoryException, IOException {
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 = getObjectNode(pid).getNodes();
while(ni.hasNext()) {
dsids.add(ni.nextNode().getName());
}
}

List<Attachment> atts = new LinkedList<Attachment>();
try {
NodeIterator i = getObjectNode(pid).getNodes();
Iterator<String> i = dsids.iterator();
while (i.hasNext()) {
final Node n = i.nextNode();
final Datastream ds = DatastreamService.getDatastream(pid, n.getName());
atts.add(new Attachment(n.getName(), ds.getMimeType(), ds.getContent()));
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();
Expand Down
Expand Up @@ -3,6 +3,7 @@

import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -216,4 +217,37 @@ public void testRetrieveMultipartDatastreams() throws Exception {
"qwerty", DOTALL).matcher(content).find());

}

@Test
public void testRetrieveFIlteredMultipartDatastreams() throws Exception {

final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest10");
assertEquals(201, getStatus(objMethod));
final HttpPost post =
new HttpPost(serverAddress + "objects/FedoraDatastreamsTest10/datastreams/");

MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("ds1", new StringBody("asdfg"));
multiPartEntity.addPart("ds2", new StringBody("qwerty"));

post.setEntity(multiPartEntity);

HttpResponse postResponse = client.execute(post);
assertEquals(201, postResponse.getStatusLine().getStatusCode());


// TODO: we should actually evaluate the multipart response for the things we're expecting
final HttpGet getDSesMethod =
new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest10/datastreams/__content__?dsid=ds1");
HttpResponse response = client.execute(getDSesMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(response.getEntity());

assertTrue("Didn't find the first datastream!", compile("asdfg",
DOTALL).matcher(content).find());
assertFalse("Didn't expect to find the second datastream!", compile(
"qwerty", DOTALL).matcher(content).find());

}
}

0 comments on commit 819a1a0

Please sign in to comment.