Skip to content

Commit

Permalink
Added PUT method to /rest/objects/{pid} endpoint for #14
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 13, 2013
1 parent 5f4ab72 commit 0c48e63
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 18 deletions.
Expand Up @@ -8,6 +8,8 @@ public class FedoraJcrTypes {
public static final String FEDORA_OBJECT = "fedora:object";

public static final String FEDORA_OWNED = "fedora:owned";


public static final String DC_TITLE = "dc:title";

public static final String DC_IDENTIFER = "dc:identifier";
}
1 change: 1 addition & 0 deletions fcrepo-kernel/src/main/resources/fedora-node-types.cnd
Expand Up @@ -38,6 +38,7 @@
* See: http://dublincore.org/documents/dcmi-terms/#elements-identifier
*/
- dc:identifier (STRING) multiple COPY
- dc:title (STRING) COPY


/*
Expand Down
14 changes: 7 additions & 7 deletions fcrepo-legacy-api/pom.xml
Expand Up @@ -16,11 +16,17 @@
<name>fcrepo 3.x compatible REST API</name>

<dependencies>

<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-kernel</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-commons</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
Expand Down Expand Up @@ -48,12 +54,6 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
@@ -1,24 +1,37 @@

package org.fcrepo.api.legacy;

import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.copyOf;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.CONFLICT;
import static org.fcrepo.api.legacy.FedoraDatastreams.getContentSize;
import static org.fcrepo.jaxb.responses.ObjectProfile.ObjectStates.A;
import static org.fcrepo.utils.FedoraJcrTypes.DC_TITLE;

import java.io.IOException;
import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.jcr.*;
import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
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;
Expand All @@ -27,6 +40,7 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.jaxb.responses.ObjectProfile;
import org.fcrepo.services.ObjectService;
import org.modeshape.common.SystemFailureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -72,6 +86,31 @@ public Response ingestAndMint() throws RepositoryException {
return ingest(pidMinter.mintPid());
}

@PUT
@Path("/{pid}")
@Consumes({TEXT_XML, APPLICATION_JSON})
public Response modify(@PathParam("pid")
final String pid, final ObjectProfile objProfile)
throws RepositoryException {

final String objPath = "/objects/" + pid;
final Session session = repo.login();

if (!session.nodeExists(objPath)) {
session.logout();
return status(CONFLICT).entity("No such object").build();
}
final Node obj = session.getNode(objPath);
obj.setProperty(DC_TITLE, objProfile.objLabel);
if (objProfile.objModels != null)
for (String model : objProfile.objModels) {
obj.addMixin(model);
}
session.save();
session.logout();
return created(uriInfo.getAbsolutePath()).build();
}

@POST
@Path("/{pid}")
public Response ingest(@PathParam("pid")
Expand Down Expand Up @@ -116,7 +155,16 @@ public Response getObject(@PathParam("pid")
final ObjectProfile objectProfile = new ObjectProfile();

objectProfile.pid = pid;
objectProfile.objLabel = obj.getName();
if (obj.hasProperty(DC_TITLE)) {
Property dcTitle = obj.getProperty(DC_TITLE);
if (!dcTitle.isMultiple())
objectProfile.objLabel =
obj.getProperty(DC_TITLE).getString();
else {
objectProfile.objLabel =
on('/').join(map(dcTitle.getValues(), value2string));
}
}
objectProfile.objOwnerId =
obj.getProperty("fedora:ownerId").getString();
objectProfile.objCreateDate =
Expand All @@ -129,14 +177,7 @@ public Response getObject(@PathParam("pid")
.build();
objectProfile.objState = A;
objectProfile.objModels =
transform(copyOf(obj.getMixinNodeTypes()),
new Function<NodeType, String>() {

@Override
public String apply(NodeType type) {
return type.getName();
}
});
map(obj.getMixinNodeTypes(), nodetype2string);
return ok(objectProfile).build();
} else {
return four04;
Expand Down Expand Up @@ -178,4 +219,32 @@ private static Long getObjectDSSize(Node obj) throws RepositoryException {
return size;
}

private Function<Value, String> value2string =
new Function<Value, String>() {

@Override
public String apply(Value v) {
try {
return v.getString();
} catch (RepositoryException e) {
throw new SystemFailureException(e);
} catch (IllegalStateException e) {
throw new SystemFailureException(e);
}
}
};

private static <From, To> Collection<To> map(From[] input,
Function<From, To> f) {
return transform(copyOf(input), f);
}

private Function<NodeType, String> nodetype2string =
new Function<NodeType, String>() {

@Override
public String apply(NodeType type) {
return type.getName();
}
};
}
Expand Up @@ -2,13 +2,16 @@
package org.fcrepo.api.legacy;

import static java.util.regex.Pattern.compile;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

Expand Down Expand Up @@ -56,4 +59,29 @@ public void testDeleteObject() throws Exception {
getStatus(new HttpGet(serverAddress +
"objects/FedoraObjectsTest3")));
}

@Test
public void testModifyObject() throws Exception {
final String label = "Muy especial!";
assertEquals(201, getStatus(postObjMethod("FedoraObjectsTest4")));

HttpPut modifyMethod =
new HttpPut(serverAddress + "objects/FedoraObjectsTest4");
modifyMethod
.setEntity(new StringEntity(
"<objectProfile xmlns=\"http://www.fedora.info/definitions/1/0/access/\">" +
"<objLabel>" +
label +
"</objLabel>" +
"</objectProfile>"));
modifyMethod.setHeader("Content-type", TEXT_XML);
assertEquals(201, getStatus(modifyMethod));
final HttpGet getMethod =
new HttpGet(serverAddress + "objects/FedoraObjectsTest4");
final HttpResponse response = client.execute(getMethod);
final String content = EntityUtils.toString(response.getEntity());
logger.debug("Retrieved object profile:\n" + content);
assertTrue("Object wasn't updated!", compile(label).matcher(content)
.find());
}
}

0 comments on commit 0c48e63

Please sign in to comment.