Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth roles common validation and initialization cleanup #259

Merged
merged 2 commits into from
Feb 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ public Response post(@PathParam("path")

try {
validatePOST(data);
} catch (final IllegalArgumentException e) {
throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).build());
}

try {
final FedoraResource resource =
nodeService.getObject(session, path);
this.getAccessRolesProvider().postRoles(resource.getNode(), data);
Expand All @@ -150,6 +146,9 @@ public Response post(@PathParam("path")
response =
Response.created(getUriInfo().getBaseUriBuilder()
.path(path).path("fcr:accessroles").build());

} catch (final IllegalArgumentException e) {
throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).build());
} finally {
session.logout();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ public class AccessRolesTypes {
.getLogger(AccessRolesTypes.class);

@Autowired
private SessionFactory sessionFactory = null;

private static boolean registered = false;

private static final Object mutex = new Object();
private final SessionFactory sessionFactory = null;

/**
* Initialize, register role assignment node types.
Expand All @@ -57,38 +53,31 @@ public class AccessRolesTypes {
@PostConstruct
public void setUpRepositoryConfiguration() throws RepositoryException,
IOException {
if (!registered) {
registerNodeTypes(sessionFactory);
}
registerNodeTypes(sessionFactory);
}

private static void registerNodeTypes(final SessionFactory sessions)
private void registerNodeTypes(final SessionFactory sessions)
throws RepositoryException, IOException {
synchronized (mutex) {
if (!registered) {
Session session = null;
try {
session = sessions.getInternalSession();
final NodeTypeManager mgr =
(NodeTypeManager) session.getWorkspace()
.getNodeTypeManager();
final URL cnd =
AccessRoles.class
.getResource("/cnd/access-control.cnd");
final NodeTypeIterator nti =
mgr.registerNodeTypes(cnd, true);
while (nti.hasNext()) {
final NodeType nt = nti.nextNodeType();
LOGGER.debug("registered node type: {}", nt.getName());
}
session.save();
registered = true;
LOGGER.debug("Registered access role node types");
} finally {
if (session != null) {
session.logout();
}
}
Session session = null;
try {
session = sessions.getInternalSession();
final NodeTypeManager mgr =
(NodeTypeManager) session.getWorkspace()
.getNodeTypeManager();
final URL cnd =
AccessRoles.class
.getResource("/cnd/access-control.cnd");
final NodeTypeIterator nti =
mgr.registerNodeTypes(cnd, true);
while (nti.hasNext()) {
final NodeType nt = nti.nextNodeType();
LOGGER.debug("registered node type: {}", nt.getName());
}
session.save();
LOGGER.debug("Registered access role node types");
} finally {
if (session != null) {
session.logout();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ private void invalidPost(final Map<String, Set<String>> data)
Matchers.<Map<String, Set<String>>> any());
// Verify no changes saved
verify(session, never()).save();
// Logout does not currently happen when the post is invalid
verify(session, never()).logout();
verify(session).logout();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;

import javax.jcr.RepositoryException;
Expand Down Expand Up @@ -90,18 +88,6 @@ public void setUp() throws RepositoryException, NoSuchFieldException,
setField(accessRolesTypes, "sessionFactory", sessionFactory);

when(sessionFactory.getInternalSession()).thenReturn(session);

// Ugly zone, reset private static flag between tests
final Field registeredField =
AccessRolesTypes.class.getDeclaredField("registered");
registeredField.setAccessible(true);
try {
registeredField.setBoolean(registeredField, false);
} catch (final IllegalArgumentException e) {
LOGGER.error("Reflection error", e);
} catch (final IllegalAccessException e) {
LOGGER.error("Reflection error", e);
}
}

@Test(expected = RepositoryException.class)
Expand Down Expand Up @@ -145,21 +131,4 @@ public void testSetupRepoConfig() throws RepositoryException, IOException {
verify(session).save();
verify(session).logout();
}

@Test
public void testSetupRepoConfigAlreadyRegistered()
throws RepositoryException, IOException {
accessRolesTypes.setUpRepositoryConfiguration();

// Clear verify counts
reset(nodeTypeManager);
reset(session);

accessRolesTypes.setUpRepositoryConfiguration();

verify(nodeTypeManager, never()).registerNodeTypes(any(URL.class),
anyBoolean());
verify(session, never()).save();
verify(session, never()).logout();
}
}