use of javax.jcr.Session in project jackrabbit by apache.
the class GCConcurrentTest method testGC.
public void testGC() throws Exception {
Node root = testRootNode;
Session session = root.getSession();
GCThread gc = new GCThread(session);
Thread gcThread = new Thread(gc, "Datastore Garbage Collector");
int len = 10 * getTestScale();
boolean started = false;
for (int i = 0; i < len; i++) {
if (!started && i > 5 + len / 100) {
started = true;
gcThread.start();
}
Node n = node(root, "test" + i);
ValueFactory vf = session.getValueFactory();
n.setProperty("data", vf.createBinary(randomInputStream(i)));
session.save();
LOG.debug("saved: " + i);
}
Thread.sleep(10);
for (int i = 0; i < len; i++) {
Node n = root.getNode("test" + i);
Property p = n.getProperty("data");
InputStream in = p.getBinary().getStream();
InputStream expected = randomInputStream(i);
checkStreams(expected, in);
n.remove();
LOG.debug("removed: " + i);
session.save();
}
Thread.sleep(10);
gc.setStop(true);
Thread.sleep(10);
gcThread.join();
gc.throwException();
}
use of javax.jcr.Session in project jackrabbit by apache.
the class CachingHierarchyManagerConsistencyTest method testObservation.
public void testObservation() throws Exception {
final List<Exception> exceptions = new ArrayList<Exception>();
Thread writer = new Thread(new Runnable() {
public void run() {
try {
long end = System.currentTimeMillis() + TEST_DURATION * 1000;
Session s = getHelper().getSuperuserSession();
try {
log.info("Starting to replace nodes");
int i = 0;
while (System.currentTimeMillis() < end) {
replaceNodes(s, i++);
}
} finally {
s.logout();
}
} catch (RepositoryException e) {
exceptions.add(e);
}
}
});
List<EventListener> listeners = new ArrayList<EventListener>();
for (int i = 0; i < NUM_LISTENERS; i++) {
final Session session = getHelper().getSuperuserSession();
listeners.add(new EventListener() {
public void onEvent(EventIterator events) {
while (events.hasNext()) {
Event event = events.nextEvent();
String path = "n/a";
try {
if (event.getType() == Event.NODE_ADDED || event.getType() == Event.PROPERTY_ADDED) {
path = event.getPath();
session.getItem(path);
}
} catch (PathNotFoundException e) {
// ignore
} catch (RepositoryException e) {
log.error(e.toString() + " Unable to get item with path: " + path);
exceptions.add(e);
}
}
}
});
}
for (EventListener listener : listeners) {
superuser.getWorkspace().getObservationManager().addEventListener(listener, ALL_EVENTS, "/", true, null, null, false);
}
writer.start();
writer.join();
for (EventListener listener : listeners) {
superuser.getWorkspace().getObservationManager().removeEventListener(listener);
}
log.info("" + exceptions.size() + " exception(s) occurred.");
if (!exceptions.isEmpty()) {
throw exceptions.get(0);
}
}
use of javax.jcr.Session in project jackrabbit by apache.
the class AcReadWriteTest method testReadAccessControl.
public void testReadAccessControl() throws NotExecutableException, RepositoryException {
/* precondition:
testuser must have READ-only permission on test-node and below
*/
checkReadOnly(path);
/* give 'testUser' jcr:readAccessControl privileges at subtree below
path excluding the node at path itself. */
Privilege[] privileges = privilegesFromNames(new String[] { Privilege.JCR_READ_ACCESS_CONTROL });
Map<String, Value> restrictions = new HashMap<String, Value>(getRestrictions(superuser, path));
restrictions.put(AccessControlConstants.P_GLOB.toString(), vf.createValue("/" + nodeName2));
JackrabbitAccessControlList tmpl = givePrivileges(path, privileges, restrictions);
/*
testuser must not be allowed to read AC content at the target node;
however, retrieving potential AC content at 'childPath' is granted.
*/
Session testSession = getTestSession();
AccessControlManager testAcMgr = getTestACManager();
assertFalse(testAcMgr.hasPrivileges(path, privileges));
try {
testAcMgr.getPolicies(path);
fail("AccessDeniedException expected");
} catch (AccessDeniedException e) {
// success.
}
assertTrue(testAcMgr.hasPrivileges(childNPath, privileges));
assertEquals(0, testAcMgr.getPolicies(childNPath).length);
/* similarly reading the corresponding AC items at 'path' must be forbidden */
String aclNodePath = null;
Node n = superuser.getNode(path);
for (NodeIterator itr = n.getNodes(); itr.hasNext(); ) {
Node child = itr.nextNode();
if (child.isNodeType("rep:Policy")) {
aclNodePath = child.getPath();
}
}
if (aclNodePath == null) {
fail("Expected node at " + path + " to have an ACL child node.");
}
assertFalse(testSession.nodeExists(aclNodePath));
for (NodeIterator aceNodes = superuser.getNode(aclNodePath).getNodes(); aceNodes.hasNext(); ) {
Node aceNode = aceNodes.nextNode();
String aceNodePath = aceNode.getPath();
assertFalse(testSession.nodeExists(aceNodePath));
for (PropertyIterator it = aceNode.getProperties(); it.hasNext(); ) {
assertFalse(testSession.propertyExists(it.nextProperty().getPath()));
}
}
}
use of javax.jcr.Session in project jackrabbit by apache.
the class EffectivePolicyTest method testGetEffectivePoliciesByPrincipal.
public void testGetEffectivePoliciesByPrincipal() throws Exception {
/*
precondition:
testuser must have READ-only permission on test-node and below
*/
checkReadOnly(path);
// give 'testUser' READ_AC privileges at 'path'
Privilege[] privileges = privilegesFromNames(new String[] { Privilege.JCR_READ_ACCESS_CONTROL });
givePrivileges(path, privileges, getRestrictions(superuser, path));
Session testSession = getTestSession();
AccessControlManager testAcMgr = getTestACManager();
// effective policies for testPrinicpal only on path -> must succeed.
((JackrabbitAccessControlManager) testAcMgr).getEffectivePolicies(Collections.singleton(testUser.getPrincipal()));
// effective policies for a combination of principals -> must fail since
// policy for 'everyone' at root node cannot be read by testuser
Set<Principal> principals = ((SessionImpl) testSession).getSubject().getPrincipals();
try {
((JackrabbitAccessControlManager) testAcMgr).getEffectivePolicies(principals);
fail();
} catch (AccessDeniedException e) {
// success
}
withdrawPrivileges(childNPath, privileges, getRestrictions(superuser, childNPath));
// the denied acl at 'childNPath' -> must fail
try {
((JackrabbitAccessControlManager) testAcMgr).getEffectivePolicies(Collections.singleton(testUser.getPrincipal()));
fail();
} catch (AccessDeniedException e) {
// success
}
}
use of javax.jcr.Session in project jackrabbit by apache.
the class EntryCollectorTest method testPermissions.
public void testPermissions() throws Exception {
Session superuser2 = getHelper().getSuperuserSession();
try {
JackrabbitAccessControlManager acM = (JackrabbitAccessControlManager) acMgr;
JackrabbitAccessControlManager acM2 = (JackrabbitAccessControlManager) superuser2.getAccessControlManager();
Set<Principal> principals = Collections.singleton(testGroup.getPrincipal());
// --- test1 : add an ACE at path ----------------------------------
Privilege[] privs = privilegesFromName(Privilege.JCR_LOCK_MANAGEMENT);
modifyPrivileges(path, testGroup.getPrincipal(), privs, true);
assertTrue(acM.hasPrivileges(path, principals, privs));
assertTrue(acM2.hasPrivileges(path, principals, privs));
assertTrue(acM.hasPrivileges(childNPath, principals, privs));
assertTrue(acM2.hasPrivileges(childNPath, principals, privs));
// --- test2: modify the policy at 'path' ------------------------------
modifyPrivileges(path, testGroup.getPrincipal(), privilegesFromName(Privilege.JCR_WRITE), true);
privs = privilegesFromNames(new String[] { Privilege.JCR_LOCK_MANAGEMENT, Privilege.JCR_WRITE });
assertTrue(acM.hasPrivileges(path, principals, privs));
assertTrue(acM2.hasPrivileges(path, principals, privs));
assertTrue(acM.hasPrivileges(childNPath, principals, privs));
assertTrue(acM2.hasPrivileges(childNPath, principals, privs));
// --- test3: add an policy at childNPath ------------------------------
modifyPrivileges(childNPath, testGroup.getPrincipal(), privilegesFromName(Privilege.JCR_ADD_CHILD_NODES), false);
privs = privilegesFromNames(new String[] { Privilege.JCR_LOCK_MANAGEMENT, Privilege.JCR_WRITE });
assertTrue(acM.hasPrivileges(path, principals, privs));
assertTrue(acM2.hasPrivileges(path, principals, privs));
privs = privilegesFromNames(new String[] { Privilege.JCR_LOCK_MANAGEMENT, Privilege.JCR_MODIFY_PROPERTIES, Privilege.JCR_REMOVE_CHILD_NODES, Privilege.JCR_REMOVE_NODE });
assertTrue(acM.hasPrivileges(childNPath, principals, privs));
assertTrue(acM2.hasPrivileges(childNPath, principals, privs));
// --- test4: modify policy at childNPath --------------------------
modifyPrivileges(childNPath, testGroup.getPrincipal(), privilegesFromName(Privilege.JCR_REMOVE_CHILD_NODES), false);
privs = privilegesFromNames(new String[] { Privilege.JCR_LOCK_MANAGEMENT, Privilege.JCR_WRITE });
assertTrue(acM.hasPrivileges(path, principals, privs));
assertTrue(acM2.hasPrivileges(path, principals, privs));
privs = privilegesFromNames(new String[] { Privilege.JCR_LOCK_MANAGEMENT, Privilege.JCR_MODIFY_PROPERTIES, Privilege.JCR_REMOVE_NODE });
assertTrue(acM.hasPrivileges(childNPath, principals, privs));
assertTrue(acM2.hasPrivileges(childNPath, principals, privs));
// --- test4: remove policy at childNPath --------------------------
acMgr.removePolicy(childNPath, acMgr.getPolicies(childNPath)[0]);
superuser.save();
privs = privilegesFromNames(new String[] { Privilege.JCR_LOCK_MANAGEMENT, Privilege.JCR_WRITE });
assertTrue(acM.hasPrivileges(path, principals, privs));
assertTrue(acM2.hasPrivileges(path, principals, privs));
assertTrue(acM.hasPrivileges(childNPath, principals, privs));
assertTrue(acM2.hasPrivileges(childNPath, principals, privs));
} finally {
superuser2.logout();
}
}
Aggregations