Search in sources :

Example 91 with Session

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 92 with Session

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);
    }
}
Also used : ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) Event(javax.jcr.observation.Event) EventListener(javax.jcr.observation.EventListener) PathNotFoundException(javax.jcr.PathNotFoundException) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session)

Example 93 with Session

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()));
        }
    }
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) NodeIterator(javax.jcr.NodeIterator) AccessDeniedException(javax.jcr.AccessDeniedException) HashMap(java.util.HashMap) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) Value(javax.jcr.Value) Privilege(javax.jcr.security.Privilege) Session(javax.jcr.Session)

Example 94 with Session

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
    }
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) JackrabbitAccessControlManager(org.apache.jackrabbit.api.security.JackrabbitAccessControlManager) JackrabbitAccessControlManager(org.apache.jackrabbit.api.security.JackrabbitAccessControlManager) AccessDeniedException(javax.jcr.AccessDeniedException) Privilege(javax.jcr.security.Privilege) Principal(java.security.Principal) Session(javax.jcr.Session)

Example 95 with Session

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();
    }
}
Also used : JackrabbitAccessControlManager(org.apache.jackrabbit.api.security.JackrabbitAccessControlManager) Privilege(javax.jcr.security.Privilege) TestPrincipal(org.apache.jackrabbit.core.security.TestPrincipal) Principal(java.security.Principal) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Aggregations

Session (javax.jcr.Session)1177 Node (javax.jcr.Node)645 Test (org.junit.Test)359 RepositoryException (javax.jcr.RepositoryException)206 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)158 SimpleCredentials (javax.jcr.SimpleCredentials)86 Property (javax.jcr.Property)78 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)77 Privilege (javax.jcr.security.Privilege)76 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)64 Value (javax.jcr.Value)63 Query (javax.jcr.query.Query)58 NodeIterator (javax.jcr.NodeIterator)55 QueryManager (javax.jcr.query.QueryManager)53 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)50 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)48 AccessControlManager (javax.jcr.security.AccessControlManager)47 HashMap (java.util.HashMap)44 UserManager (org.apache.jackrabbit.api.security.user.UserManager)43 ArrayList (java.util.ArrayList)41