Skip to content

Commit

Permalink
Update based on impact of fcrepo shift from checked to runtime except…
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Woods committed Sep 16, 2014
1 parent 5cd7dbb commit 2b2f694
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 56 deletions.
Expand Up @@ -24,7 +24,6 @@

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

Expand Down Expand Up @@ -172,12 +171,8 @@ private boolean canRemoveChildrenRecursive(final Session userSession,
final Node n = ni.nextNode();
// are there unique roles?
final Set<String> roles;
Map<String, List<String>> acl = null;
try {
acl = accessRolesProvider.getRoles(n, false);
} catch (final PathNotFoundException ignored) {
LOGGER.trace("Path not found when removing roles", ignored);
}
final Map<String, List<String>> acl = accessRolesProvider.getRoles(n, false);

if (acl != null) {
roles = resolveUserRoles(acl, allPrincipals);
} else {
Expand Down
Expand Up @@ -92,9 +92,8 @@ private AccessRolesProvider getAccessRolesProvider() {
@GET
@Produces(APPLICATION_JSON)
@Timed
public Response get(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("effective")
final String effective) throws RepositoryException {
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);
LOGGER.debug("effective: {}", effective);
Expand Down
Expand Up @@ -30,6 +30,7 @@
import javax.jcr.Value;

import org.fcrepo.auth.roles.common.Constants.JcrName;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.modeshape.jcr.value.Path;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -66,34 +67,39 @@ public class AccessRolesProvider {
* @param effective if true then search for effective roles
* @return a set of roles for each principal
*/
public Map<String, List<String>> getRoles(final Node node, final boolean effective) throws RepositoryException {
public Map<String, List<String>> getRoles(final Node node, final boolean effective) {
final Map<String, List<String>> data = new HashMap<>();
final Session session = node.getSession();
registerPrefixes(session);
if (node.isNodeType(rbaclAssignable.getQualified())) {
getAssignments(node, data);
return data;
}
if (effective) { // look up the tree
try {
for (Node n = node.getParent(); n != null; n = n.getParent()) {
if (n.isNodeType(rbaclAssignable.getQualified())) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("effective roles are assigned at node: {}", n.getPath());
}
getAssignments(n, data);
if (LOGGER.isDebugEnabled()) {
for (final Map.Entry<String, List<String>> entry : data.entrySet()) {
LOGGER.debug("{} has role(s) {}", entry.getKey(), entry.getValue());
try {

final Session session = node.getSession();
registerPrefixes(session);
if (node.isNodeType(rbaclAssignable.getQualified())) {
getAssignments(node, data);
return data;
}
if (effective) { // look up the tree
try {
for (Node n = node.getParent(); n != null; n = n.getParent()) {
if (n.isNodeType(rbaclAssignable.getQualified())) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("effective roles are assigned at node: {}", n.getPath());
}
getAssignments(n, data);
if (LOGGER.isDebugEnabled()) {
for (final Map.Entry<String, List<String>> entry : data.entrySet()) {
LOGGER.debug("{} has role(s) {}", entry.getKey(), entry.getValue());
}
}
return data;
}
return data;
}
} catch (final ItemNotFoundException e) {
LOGGER.trace("Subject not found, using default access roles", e);
return DEFAULT_ACCESS_ROLES;
}
} catch (final ItemNotFoundException e) {
LOGGER.trace("Subject not found, using default access roles", e);
return DEFAULT_ACCESS_ROLES;
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
return null;
}
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.springframework.stereotype.Component;

Expand All @@ -49,18 +50,22 @@ public class AccessRolesResources implements UriAwareResourceModelFactory {
*/
@Override
public Model createModelForResource(final FedoraResource resource,
final UriInfo uriInfo, final IdentifierTranslator graphSubjects)
throws RepositoryException {
final UriInfo uriInfo, final IdentifierTranslator graphSubjects) {
final Model model = ModelFactory.createDefaultModel();
final Resource s = graphSubjects.getSubject(resource.getNode().getPath());

if (resource.getNode().isNodeType(
FedoraJcrTypes.FEDORA_RESOURCE)) {
final Map<String, String> pathMap =
singletonMap("path", resource.getPath().substring(1));
final Resource acl = model.createResource(uriInfo.getBaseUriBuilder().path(
AccessRoles.class).buildFromMap(pathMap).toASCIIString());
model.add(s, RdfLexicon.HAS_ACCESS_ROLES_SERVICE, acl);
try {
final Resource s = graphSubjects.getSubject(resource.getNode().getPath());

if (resource.getNode().isNodeType(
FedoraJcrTypes.FEDORA_RESOURCE)) {
final Map<String, String> pathMap =
singletonMap("path", resource.getPath().substring(1));
final Resource acl = model.createResource(uriInfo.getBaseUriBuilder().path(
AccessRoles.class).buildFromMap(pathMap).toASCIIString());
model.add(s, RdfLexicon.HAS_ACCESS_ROLES_SERVICE, acl);
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
return model;
}
Expand Down
Expand Up @@ -25,6 +25,7 @@
import javax.jcr.nodetype.NodeTypeIterator;

import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -51,13 +52,11 @@ public class AccessRolesTypes {
* @throws IOException
*/
@PostConstruct
public void setUpRepositoryConfiguration() throws RepositoryException,
IOException {
public void setUpRepositoryConfiguration() throws IOException {
registerNodeTypes(sessionFactory);
}

private void registerNodeTypes(final SessionFactory sessions)
throws RepositoryException, IOException {
private void registerNodeTypes(final SessionFactory sessions) throws IOException {
Session session = null;
try {
session = sessions.getInternalSession();
Expand All @@ -75,6 +74,9 @@ private void registerNodeTypes(final SessionFactory sessions)
}
session.save();
LOGGER.debug("Registered access role node types");

} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
} finally {
if (session != null) {
session.logout();
Expand Down
Expand Up @@ -46,6 +46,7 @@
import javax.ws.rs.core.Response;

import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.services.NodeService;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -141,11 +142,11 @@ public void testGetData() throws RepositoryException {
verify(accessRolesProvider).getRoles(any(Node.class), anyBoolean());
}

@Test(expected = RepositoryException.class)
public void testGetException() throws RepositoryException {
@Test(expected = RepositoryRuntimeException.class)
public void testGetException() {

when(nodeService.getObject(any(Session.class), anyString())).thenThrow(
new RepositoryException());
new RepositoryRuntimeException("expected"));

try {
accessRoles.get(paths, "");
Expand Down
Expand Up @@ -34,6 +34,7 @@
import javax.jcr.nodetype.NodeTypeIterator;

import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand Down Expand Up @@ -89,12 +90,10 @@ public void setUp() throws RepositoryException, IOException {
when(sessionFactory.getInternalSession()).thenReturn(session);
}

@Test(expected = RepositoryException.class)
public void testSetupRepoConfigGetSessionException()
throws RepositoryException,
IOException {
@Test(expected = RepositoryRuntimeException.class)
public void testSetupRepoConfigGetSessionException() throws Exception {
when(sessionFactory.getInternalSession()).thenThrow(
new RepositoryException());
new RepositoryRuntimeException("expected"));

try {
accessRolesTypes.setUpRepositoryConfiguration();
Expand All @@ -106,9 +105,9 @@ public void testSetupRepoConfigGetSessionException()
}
}

@Test(expected = RepositoryException.class)
@Test(expected = RepositoryRuntimeException.class)
public void testSetupRepoConfigGetNodeTypeManagerException()
throws RepositoryException, IOException {
throws Exception {
when(workspace.getNodeTypeManager()).thenThrow(
new RepositoryException());

Expand Down

0 comments on commit 2b2f694

Please sign in to comment.