Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add a 'batch' change API endpoint, which accepts multipart posts and …
…adds each part as a datastream using the content disposition name
  • Loading branch information
cbeer committed Feb 19, 2013
1 parent 1b32008 commit c70656f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
12 changes: 12 additions & 0 deletions fcrepo-legacy-api/pom.xml
Expand Up @@ -72,6 +72,7 @@
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
Expand All @@ -90,6 +91,17 @@
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.0.1</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
Expand All @@ -38,6 +39,8 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.fcrepo.AbstractResource;
import org.fcrepo.jaxb.responses.DatastreamHistory;
import org.fcrepo.jaxb.responses.DatastreamProfile;
Expand Down Expand Up @@ -104,6 +107,42 @@ public Response getDatastreams(@PathParam("pid")
}
}

@POST
@Path("/")
public Response addDatastreams(@PathParam("pid") final String pid, final List<Attachment> attachmentList) throws RepositoryException, IOException {

final Session session = repo.login();

Long oldObjectSize = getObjectSize(session.getNode("/objects/" + pid));


for(final Attachment a : attachmentList) {
final String dsid = a.getContentDisposition().getParameter("name");
final String dsPath = "/objects/" + pid + "/" + dsid;
if (session.hasPermission(dsPath, "add_node")) {
new DatastreamService().createDatastreamNode(session, dsPath,
a.getDataHandler().getContentType(), a.getDataHandler().getInputStream());
}
}

session.save();

/*
* we save before updating the repo size because the act of
* persisting session state creates new system-curated nodes and
* properties which contribute to the footprint of this resource
*/
updateRepositorySize(getObjectSize(session.getNode("/objects/" +
pid)) -
oldObjectSize, session);
// now we save again to persist the repo size
session.save();

session.logout();

return created(uriInfo.getAbsolutePath()).build();
}

/**
* Create a new datastream
*
Expand Down
Expand Up @@ -12,6 +12,8 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

Expand Down Expand Up @@ -147,4 +149,38 @@ public void testMultipleDatastreams() throws Exception {
assertTrue("Didn't find the second datastream!", compile(
"dsid=\"ds2\"", DOTALL).matcher(content).find());
}


@Test
public void testAddMultipleDatastreams() throws Exception {
final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest8");
assertEquals(201, getStatus(objMethod));
final HttpPost post =
new HttpPost(serverAddress + "objects/FedoraDatastreamsTest8/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());


final HttpGet getDSesMethod =
new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest8/datastreams");
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("dsid=\"ds1\"",
DOTALL).matcher(content).find());
assertTrue("Didn't find the second datastream!", compile(
"dsid=\"ds2\"", DOTALL).matcher(content).find());



}
}

0 comments on commit c70656f

Please sign in to comment.