Skip to content

Commit

Permalink
added simple create object method within tx bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
fasseg committed May 8, 2013
1 parent c8aad59 commit f95b2ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
@@ -1,6 +1,7 @@
package org.fcrepo.api;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -12,16 +13,24 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;

import org.fcrepo.AbstractResource;
import org.fcrepo.FedoraObject;
import org.fcrepo.Transaction;
import org.fcrepo.Transaction.State;
import org.fcrepo.jaxb.responses.access.ObjectProfile;
import org.fcrepo.services.ObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Path("/rest/fcr:tx")
public class FedoraTransactions extends AbstractResource {

@Autowired
private ObjectService objectService;

/* TODO: since transactions have to be available on all nodes, they have to be either persisted or written to a */
/* distributed map or sth, not just this plain hashmap that follows */
Expand Down Expand Up @@ -73,9 +82,16 @@ public Transaction rollback(@PathParam("txid") final String txid) throws Reposit
}

@POST
@Path("/{txid}/{path: .*}")
public Response doInTransaction(@PathParam("txid") final String txid, @PathParam("path") final String path) {
return Response.seeOther(URI.create("/rest/" + path)).build();
@Path("/{txid}/{path: .*}/fcr:newhack")
@Produces({MediaType.TEXT_PLAIN})
public Response createObjectInTransaction(@PathParam("txid") final String txid, @PathParam("path") final List<PathSegment> pathlist)throws RepositoryException {
Transaction tx = transactions.get(txid);
if (tx == null) {
throw new RepositoryException("Transaction with id " + txid + " is not available");
}
final String path = toPath(pathlist);
final FedoraObject obj = objectService.createObject(tx.getSession(), path);
return Response.ok(path).build();
}

}
Expand Up @@ -4,6 +4,8 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import javax.validation.constraints.AssertTrue;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
Expand Down Expand Up @@ -49,12 +51,26 @@ public void testCreateAndCommitTransaction() throws Exception {
assertNotNull(tx.getState());
assertNotNull(tx.getCreated());
assertTrue(tx.getState() == State.NEW);

/* create a new object inside the tx */
HttpPost postNew = new HttpPost(serverAddress + "fcr:tx/" + tx.getId() + "/objects/testobj1/fcr:newhack");
resp = execute(postNew);
assertTrue(resp.getStatusLine().getStatusCode() == 200);

/* check if the object is already there, before the commit */
HttpGet getObj = new HttpGet(serverAddress + "/objects/testobj1");
resp = execute(getObj);
assertTrue(resp.getStatusLine().toString(),resp.getStatusLine().getStatusCode() == 404);

/* commit the tx */
HttpPost commitTx = new HttpPost(serverAddress + "fcr:tx/" + tx.getId() + "/fcr:commit");
resp = execute(commitTx);
Transaction committed = mapper.readValue(resp.getEntity().getContent(), Transaction.class);
assertEquals(committed.getState(), State.COMMITED);

/* check if the object is there, after the commit */
resp = execute(getObj);
assertTrue(resp.getStatusLine().toString(),resp.getStatusLine().getStatusCode() == 200);
}

@Test
Expand Down

0 comments on commit f95b2ff

Please sign in to comment.