Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #5 from fcrepo4/ldp-resources
Update auth-roles-common to work with ldp-resources branch

Related to: https://www.pivotaltracker.com/story/show/79973150
  • Loading branch information
Andrew Woods committed Oct 13, 2014
2 parents 98afece + 212ec84 commit 325402c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 73 deletions.
Expand Up @@ -16,6 +16,7 @@
package org.fcrepo.auth.roles.common;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.fcrepo.jcr.FedoraJcrTypes.FCR_METADATA;

import java.util.List;
import java.util.Map;
Expand All @@ -25,7 +26,7 @@
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
Expand All @@ -36,13 +37,21 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import com.google.common.annotations.VisibleForTesting;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.DatastreamImpl;
import org.fcrepo.kernel.impl.FedoraBinaryImpl;
import org.fcrepo.kernel.impl.FedoraObjectImpl;
import org.jvnet.hk2.annotations.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -56,22 +65,47 @@
* @author Gregory Jansen
* @since Sep 5, 2013
*/
@Scope("prototype")
@Scope("request")
@Path("/{path: .*}/fcr:accessroles")
public class AccessRoles extends AbstractResource {

private static final Logger LOGGER = LoggerFactory
.getLogger(AccessRoles.class);

protected IdentifierConverter<Resource,Node> identifierTranslator;


@Inject
protected Session session;

@Inject
@Optional
private AccessRolesProvider accessRolesProvider;

@Context
protected HttpServletRequest request;
@Context protected Request request;
@Context protected HttpServletResponse servletResponse;
@Context protected UriInfo uriInfo;

protected FedoraResource resource;

@PathParam("path") protected String externalPath;


/**
* Default JAX-RS entry point
*/
public AccessRoles() {
super();
}

/**
* Create a new FedoraNodes instance for a given path
* @param externalPath
*/
@VisibleForTesting
public AccessRoles(final String externalPath) {
this.externalPath = externalPath;
}


/**
Expand All @@ -84,21 +118,25 @@ private AccessRolesProvider getAccessRolesProvider() {
/**
* Retrieve the roles assigned to each principal on this specific path.
*
* @param pathList
* @return JSON representation of assignment map
* @throws RepositoryException
*/
@GET
@Produces(APPLICATION_JSON)
@Timed
public Response get(@PathParam("path") final List<PathSegment> pathList,
@QueryParam("effective") final String effective) {
final String path = toPath(pathList);
LOGGER.debug("Get access roles for: {}", path);
public Response get(@QueryParam("effective") final String effective) {
LOGGER.debug("Get access roles for: {}", externalPath);
LOGGER.debug("effective: {}", effective);
Response.ResponseBuilder response;
try {
final Node node = nodeService.getObject(session, path).getNode();
final Node node;

if (resource instanceof FedoraBinary) {
node = ((FedoraBinary) resource()).getDescription().getNode();
} else {
node = resource().getNode();
}

final Map<String, List<String>> data =
this.getAccessRolesProvider().getRoles(node,
(effective != null));
Expand All @@ -117,32 +155,33 @@ public Response get(@PathParam("path") final List<PathSegment> pathList,
/**
* Apply new role assignments at the specified node.
*
* @param pathList
* @param data
* @return response
* @throws RepositoryException
*/
@POST
@Consumes(APPLICATION_JSON)
@Timed
public Response post(@PathParam("path")
final List<PathSegment> pathList, final Map<String, Set<String>> data)
public Response post(final Map<String, Set<String>> data)
throws RepositoryException {
final String path = toPath(pathList);
LOGGER.debug("POST Received request param: {}", request);
Response.ResponseBuilder response;

try {
validatePOST(data);

final FedoraResource resource =
nodeService.getObject(session, path);
this.getAccessRolesProvider().postRoles(resource.getNode(), data);
final FedoraResource resource = resource();

if (resource instanceof FedoraBinary) {
this.getAccessRolesProvider().postRoles(((FedoraBinary) resource).getDescription().getNode(), data);
} else {
this.getAccessRolesProvider().postRoles(resource.getNode(), data);
}
session.save();
LOGGER.debug("Saved access roles {}", data);
response =
Response.created(getUriInfo().getBaseUriBuilder()
.path(path).path("fcr:accessroles").build());
.path(externalPath).path("fcr:accessroles").build());

} catch (final IllegalArgumentException e) {
throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).build());
Expand Down Expand Up @@ -184,11 +223,17 @@ private void validatePOST(final Map<String, Set<String>> data) {
*/
@DELETE
@Timed
public Response deleteNodeType(@PathParam("path")
final List<PathSegment> pathList) throws RepositoryException {
final String path = toPath(pathList);
public Response deleteNodeType() throws RepositoryException {
try {
final Node node = nodeService.getObject(session, path).getNode();

final Node node;

if (resource instanceof FedoraBinary) {
node = ((FedoraBinary) resource()).getDescription().getNode();
} else {
node = resource().getNode();
}

this.getAccessRolesProvider().deleteRoles(node);
session.save();
return Response.noContent().build();
Expand All @@ -201,4 +246,45 @@ private UriInfo getUriInfo() {
return this.uriInfo;
}

protected FedoraResource resource() {
if (resource == null) {
resource = getResourceFromPath(externalPath);
}

return resource;
}


protected IdentifierConverter<Resource,Node> translator() {
if (identifierTranslator == null) {
identifierTranslator = new UriAwareIdentifierConverter(session,
uriInfo.getBaseUriBuilder().clone().path("{path: .*}"));
}

return identifierTranslator;
}

private FedoraResource getResourceFromPath(final String externalPath) {
final FedoraResource resource;
final boolean metadata = externalPath != null
&& externalPath.endsWith(FCR_METADATA);

final Node node = translator().convert(translator().toDomain(externalPath));

if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);

if (metadata) {
resource = datastream;
} else {
resource = datastream.getBinary();
}
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
} else {
resource = new FedoraObjectImpl(node);
}
return resource;
}

}
Expand Up @@ -223,7 +223,7 @@ public Map<String, List<String>> findRolesForPath(final Path absPath,
}
break;
} catch (final PathNotFoundException e) {
LOGGER.debug("Cannot find node: {}", p, e);
LOGGER.trace("Cannot find node: {}, trying parent.", p, e);
}
}
return this.getRoles(node, true);
Expand Down
Expand Up @@ -19,6 +19,7 @@

import java.util.Map;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriInfo;

Expand All @@ -27,7 +28,7 @@
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.rdf.model.Model;
Expand All @@ -50,11 +51,11 @@ public class AccessRolesResources implements UriAwareResourceModelFactory {
*/
@Override
public Model createModelForResource(final FedoraResource resource,
final UriInfo uriInfo, final IdentifierTranslator graphSubjects) {
final UriInfo uriInfo, final IdentifierConverter<Resource, Node> graphSubjects) {
final Model model = ModelFactory.createDefaultModel();

try {
final Resource s = graphSubjects.getSubject(resource.getNode().getPath());
final Resource s = graphSubjects.reverse().convert(resource.getNode());

if (resource.getNode().isNodeType(
FedoraJcrTypes.FEDORA_RESOURCE)) {
Expand Down
Expand Up @@ -25,15 +25,16 @@

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.rdf.impl.DefaultIdentifierTranslator;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mock;

import com.hp.hpl.jena.rdf.model.Model;
Expand All @@ -47,17 +48,17 @@
*/
public class AccessRolesResourcesTest {

@Mock
private IdentifierTranslator graphSubjects;
private IdentifierConverter<Resource, Node> graphSubjects;

@Mock
private FedoraResource fedoraResource;

private Resource graphResource;

@Mock
private Node resourceNode;

@Mock
private Session mockSession;

private Model model;

private UriInfo uriInfo;
Expand All @@ -74,11 +75,10 @@ public void setUp() throws RepositoryException {

pathString = "path";
model = ModelFactory.createDefaultModel();
graphResource = model.createResource("/" + pathString);
graphSubjects = new DefaultIdentifierTranslator(mockSession);

when(graphSubjects.getSubject(Matchers.anyString())).thenReturn(
graphResource);
when(fedoraResource.getNode()).thenReturn(resourceNode);
when(resourceNode.getPath()).thenReturn("/" + pathString);

uriInfo = getUriInfoImpl();
}
Expand Down

0 comments on commit 325402c

Please sign in to comment.