Skip to content

Commit

Permalink
retrieve all the content in a multipart response
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 22, 2013
1 parent 2e7fe72 commit 76af2fc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;

import javax.jcr.Node;
Expand All @@ -39,6 +40,7 @@
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.fcrepo.AbstractResource;
import org.fcrepo.Datastream;
import org.fcrepo.jaxb.responses.DatastreamHistory;
Expand Down Expand Up @@ -127,6 +129,28 @@ public Response addDatastreams(@PathParam("pid")
return created(uriInfo.getAbsolutePath()).build();
}

@GET
@Path("/__content__")
@Produces("multipart/mixed")
public MultipartBody getDatastreamsContents(@PathParam("pid") final String pid) throws RepositoryException, IOException {

final Session session = repo.login();

List<Attachment> atts = new LinkedList<Attachment>();
try {
NodeIterator i = getObjectNode(pid).getNodes();
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()));
}
} finally {
session.logout();
}

return new MultipartBody(atts, true);
}

/**
* Create a new datastream
*
Expand Down
Expand Up @@ -182,5 +182,38 @@ public void testAddMultipleDatastreams() throws Exception {



}

@Test
public void testRetrieveMultipartDatastreams() throws Exception {

final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest9");
assertEquals(201, getStatus(objMethod));
final HttpPost post =
new HttpPost(serverAddress + "objects/FedoraDatastreamsTest9/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/FedoraDatastreamsTest9/datastreams/__content__");
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());
assertTrue("Didn't find the second datastream!", compile(
"qwerty", DOTALL).matcher(content).find());

}
}

0 comments on commit 76af2fc

Please sign in to comment.