Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add multipart/form-data support for add/modify datastream API endpoints
  • Loading branch information
cbeer committed Feb 26, 2013
1 parent 4026728 commit 68c00c5
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
Expand Up @@ -28,6 +28,7 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
Expand All @@ -40,6 +41,7 @@
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.fcrepo.AbstractResource;
import org.fcrepo.Datastream;
import org.fcrepo.jaxb.responses.DatastreamHistory;
Expand Down Expand Up @@ -171,6 +173,32 @@ public Response addDatastream(@PathParam("pid")

}

/**
* Create a new datastream from a multipart/form-data request
*
* @param pid
* persistent identifier of the digital object
* @param dsid
* datastream identifier
* @param contentType
* Content-Type header
* @param file
* Binary blob
* @return 201 Created
* @throws RepositoryException
* @throws IOException
*/
@POST
@Consumes("multipart/form-data")
@Path("/{dsid}")
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 {
return addDatastream(pid, dsid, file.getContentType(), file.getDataHandler().getInputStream());
}

/**
* Modify an existing datastream's content
*
Expand Down Expand Up @@ -205,6 +233,34 @@ public Response modifyDatastream(@PathParam("pid")

}

/**
* Modify an existing datastream's content
*
* @param pid
* persistent identifier of the digital object
* @param dsid
* datastream identifier
* @param contentType
* Content-Type header
* @param file
* Binary blob
* @return 201 Created
* @throws RepositoryException
* @throws IOException
*/
@PUT
@Consumes("multipart/form-data")
@Path("/{dsid}")
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 {

return modifyDatastream(pid, dsid, file.getContentType(), file.getDataHandler().getInputStream());

}

private URI addDatastreamNode(final String pid, final String dsPath,
final MediaType contentType, final InputStream requestBodyStream,
final Session session) throws RepositoryException, IOException {
Expand Down
Expand Up @@ -40,6 +40,38 @@ public void testAddDatastream() throws Exception {
assertEquals(201, getStatus(method));
}

@Test
public void testAddMultipartFormDatastream() throws Exception {
final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest21");
assertEquals(201, getStatus(objMethod));

final HttpPost post =
new HttpPost(serverAddress + "objects/FedoraDatastreamsTest21/datastreams/ds1");

MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("file", new StringBody("asdfg"));

post.setEntity(multiPartEntity);

HttpResponse postResponse = client.execute(post);

assertEquals(201, postResponse.getStatusLine().getStatusCode());


final HttpGet method_test_get =
new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest21/datastreams/ds1/content");
assertEquals(200, getStatus(method_test_get));
String ds_content = EntityUtils.toString(client.execute(method_test_get)
.getEntity());

logger.debug("Got content:" + ds_content);
logger.debug("Returned from HTTP GET, now checking content...");
assertTrue("Got the wrong content back!", "asdfg"
.equals(ds_content));

}

@Test
public void testMutateDatastream() throws Exception {
final HttpPost createObjectMethod =
Expand All @@ -66,6 +98,47 @@ public void testMutateDatastream() throws Exception {
retrieveMutatedDataStreamMethod).getEntity())));
}


@Test
public void testMutateMultipartFormDatastream() throws Exception {

final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest31");
assertEquals(201, getStatus(objMethod));

final HttpPost createDataStreamMethod =
postDSMethod("FedoraDatastreamsTest31", "ds1", "foo");
assertEquals("Couldn't create a datastream!", 201,
getStatus(createDataStreamMethod));


final HttpPut mutateDataStreamMethod =
new HttpPut(serverAddress + "objects/FedoraDatastreamsTest31/datastreams/ds1");

MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("file", new StringBody("asdfg"));

mutateDataStreamMethod.setEntity(multiPartEntity);

HttpResponse putResponse = client.execute(mutateDataStreamMethod);

assertEquals(201, putResponse.getStatusLine().getStatusCode());


final HttpGet method_test_get =
new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest31/datastreams/ds1/content");
String ds_content = EntityUtils.toString(client.execute(method_test_get)
.getEntity());

logger.debug("Got content: " + ds_content);
assertEquals(200, getStatus(method_test_get));
logger.debug("Returned from HTTP GET, now checking content...");
assertTrue("Got the wrong content back!", "asdfg"
.equals(ds_content));

}


@Test
public void testGetDatastream() throws Exception {
client.execute(postObjMethod("FedoraDatastreamsTest4"));
Expand Down

0 comments on commit 68c00c5

Please sign in to comment.