Skip to content

Commit

Permalink
added method to check if a workspace is accessible by a session, sinc…
Browse files Browse the repository at this point in the history
…e deletion seems no to remove the possibility to use Repository.login to a deleted workspace
  • Loading branch information
fasseg committed May 7, 2013
1 parent a0b190d commit 0076fa4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
25 changes: 19 additions & 6 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraWorkspaces.java
Expand Up @@ -19,16 +19,17 @@
import org.apache.commons.lang.RandomStringUtils;
import org.fcrepo.AbstractResource;
import org.modeshape.jcr.JcrRepositoryFactory;
import org.modeshape.jcr.cache.WorkspaceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Path("/rest/fcr:workspaces")
public class FedoraWorkspaces extends AbstractResource {

@Autowired
private RepositoryFactory jcrFactory;

@POST
@Produces(MediaType.TEXT_PLAIN)
public Response createWorkspace() throws RepositoryException {
Expand All @@ -41,20 +42,22 @@ public Response createWorkspace() throws RepositoryException {

@GET
@Path("{name}")
@Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_XML})
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
public Response getWorkspace(@PathParam("name") final String name) throws RepositoryException {
final Session sess = getAuthenticatedSession();
if (!isWorkspaceAccessible(sess, name)) {
throw new WorkspaceNotFoundException("Unable to access Workspace " + name);
}
final Session wsSess = sess.getRepository().login(name);
String current = wsSess.getWorkspace().getName();
return Response.ok(current).build();
}



@DELETE
@Path("{name}")
public Response deleteWorkspace(@PathParam("name") final String name) throws RepositoryException {
final Session sess = getAuthenticatedSession();
if (name.equals("default")){
if (name.equals("default")) {
throw new RepositoryException("Unable to delete the default workspace");
}
sess.getWorkspace().deleteWorkspace(name);
Expand All @@ -71,4 +74,14 @@ public Response mergeWorkspace(@PathParam("name") final String name) throws Repo
vm.merge("/", name, false);
return Response.ok().build();
}

private boolean isWorkspaceAccessible(Session sess, String name) throws RepositoryException {
for (String wsName : sess.getWorkspace().getAccessibleWorkspaceNames()) {
if (wsName.equals(name)) {
return true;
}
}
return false;

}
}
Expand Up @@ -21,10 +21,18 @@ public void testWorkspaceCreate() throws Exception {

HttpGet getWs = new HttpGet(serverAddress + "fcr:workspaces/" + name);
resp = execute(getWs);
assertTrue(resp.getStatusLine().getStatusCode() == 200);
assertTrue(resp.getStatusLine().toString(),resp.getStatusLine().getStatusCode() == 200);
assertEquals(name, IOUtils.toString(resp.getEntity().getContent()));
}

@Test
public void testInvalidWorkspaceGet() throws Exception {
String name = "invalidws";
HttpGet getWs = new HttpGet(serverAddress + "fcr:workspaces/" + name);
HttpResponse resp = execute(getWs);
assertTrue(resp.getStatusLine().toString(),resp.getStatusLine().getStatusCode() == 500);
}

@Test
public void testWorkspaceDelete() throws Exception {
HttpPost createWs = new HttpPost(serverAddress + "fcr:workspaces");
Expand All @@ -40,7 +48,7 @@ public void testWorkspaceDelete() throws Exception {
/* TODO: this should fail but the repo still returns the Workspace after the deletion */
HttpGet getWs = new HttpGet(serverAddress + "fcr:workspaces/" + name);
resp = execute(getWs);
assertTrue("Repository returned: " + resp.getStatusLine(), resp.getStatusLine().getStatusCode() == 404);
assertTrue("Repository returned: " + resp.getStatusLine(), resp.getStatusLine().getStatusCode() == 500);
}

@Test
Expand Down

0 comments on commit 0076fa4

Please sign in to comment.