Search in sources :

Example 56 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class TokenBasedLoginTest method testConcurrentLoginOfDifferentUsers.

/**
     * Tests concurrent login of 3 different users on the Repository including
     * token creation.
     * Test copied and slightly adjusted from org.apache.jackrabbit.core.ConcurrentLoginTest
     */
public void testConcurrentLoginOfDifferentUsers() throws RepositoryException, NotExecutableException {
    final Exception[] exception = new Exception[1];
    List<Thread> testRunner = new ArrayList<Thread>();
    for (int i = 0; i < 10; i++) {
        testRunner.add(new Thread(new Runnable() {

            public void run() {
                for (int i = 0; i < 100; i++) {
                    try {
                        SimpleCredentials c;
                        double rand = 3 * Math.random();
                        int index = (int) Math.floor(rand);
                        switch(index) {
                            case 0:
                                c = new SimpleCredentials(testuser.getID(), testuser.getID().toCharArray());
                                break;
                            case 1:
                                c = new SimpleCredentials(getHelper().getProperty(RepositoryStub.PROP_PREFIX + "." + RepositoryStub.PROP_SUPERUSER_NAME), getHelper().getProperty(RepositoryStub.PROP_PREFIX + "." + RepositoryStub.PROP_SUPERUSER_PWD).toCharArray());
                                break;
                            default:
                                c = new SimpleCredentials(getHelper().getProperty(RepositoryStub.PROP_PREFIX + "." + RepositoryStub.PROP_READONLY_NAME), getHelper().getProperty(RepositoryStub.PROP_PREFIX + "." + RepositoryStub.PROP_READONLY_PWD).toCharArray());
                                break;
                        }
                        c.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE, "");
                        Session s = getHelper().getRepository().login(c);
                        try {
                            Set<TokenCredentials> tcs = ((SessionImpl) s).getSubject().getPublicCredentials(TokenCredentials.class);
                            assertFalse(tcs.isEmpty());
                        } finally {
                            s.logout();
                        }
                    } catch (Exception e) {
                        exception[0] = e;
                        break;
                    }
                }
            }
        }));
    }
    // start threads
    for (Object aTestRunner : testRunner) {
        ((Thread) aTestRunner).start();
    }
    // join threads
    for (Object aTestRunner : testRunner) {
        try {
            ((Thread) aTestRunner).join();
        } catch (InterruptedException e) {
            fail(e.toString());
        }
    }
    if (exception[0] != null) {
        fail(exception[0].toString());
    }
}
Also used : ArrayList(java.util.ArrayList) LoginException(javax.jcr.LoginException) RepositoryException(javax.jcr.RepositoryException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) SimpleCredentials(javax.jcr.SimpleCredentials) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)

Example 57 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class AccessManagerTest method testIsGrantedOnNewNode.

public void testIsGrantedOnNewNode() throws RepositoryException, NotExecutableException {
    Session s = getHelper().getReadWriteSession();
    try {
        AccessManager acMgr = getAccessManager(s);
        Node newNode = ((Node) s.getItem(testRoot)).addNode(nodeName2, testNodeType);
        NodeId id = (NodeId) getItemId(newNode);
        assertTrue(acMgr.isGranted(id, AccessManager.READ));
        assertTrue(acMgr.isGranted(id, AccessManager.WRITE));
        assertTrue(acMgr.isGranted(id, AccessManager.WRITE | AccessManager.REMOVE));
    } finally {
        s.logout();
    }
}
Also used : Node(javax.jcr.Node) NodeId(org.apache.jackrabbit.core.id.NodeId) Session(javax.jcr.Session)

Example 58 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class AccessManagerTest method testCanAccessDeniedWorkspace.

public void testCanAccessDeniedWorkspace() throws RepositoryException, NotExecutableException {
    Session s = getHelper().getReadOnlySession();
    try {
        Set<String> allAccessibles = new HashSet<String>(Arrays.asList(superuser.getWorkspace().getAccessibleWorkspaceNames()));
        Set<String> sWorkspaceNames = new HashSet<String>(Arrays.asList(s.getWorkspace().getAccessibleWorkspaceNames()));
        if (!allAccessibles.removeAll(sWorkspaceNames) || allAccessibles.isEmpty()) {
            throw new NotExecutableException("No workspace name found that exists but is not accessible for ReadOnly session.");
        }
        String notAccessibleName = allAccessibles.iterator().next();
        assertFalse(getAccessManager(s).canAccess(notAccessibleName));
    } finally {
        s.logout();
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Session(javax.jcr.Session) HashSet(java.util.HashSet)

Example 59 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class AccessManagerTest method testCanAccess.

public void testCanAccess() throws RepositoryException, NotExecutableException {
    Session s = getHelper().getReadOnlySession();
    try {
        String wspName = s.getWorkspace().getName();
        assertTrue(getAccessManager(s).canAccess(wspName));
    } finally {
        s.logout();
    }
}
Also used : Session(javax.jcr.Session)

Example 60 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class AccessManagerTest method testCanReadNewId.

public void testCanReadNewId() throws Exception {
    Session s = getHelper().getReadOnlySession();
    try {
        NodeImpl n = (NodeImpl) testRootNode.addNode(nodeName1);
        PropertyImpl p = (PropertyImpl) n.setProperty(propertyName1, "somevalue");
        try {
            AccessManager acMgr = getAccessManager(s);
            acMgr.canRead(null, n.getId());
            fail("expected repositoryexception");
        } catch (RepositoryException e) {
        // success
        }
        try {
            AccessManager acMgr = getAccessManager(s);
            acMgr.canRead(null, p.getId());
            fail("expected repositoryexception");
        } catch (RepositoryException e) {
        // success
        }
    } finally {
        s.logout();
    }
}
Also used : NodeImpl(org.apache.jackrabbit.core.NodeImpl) PropertyImpl(org.apache.jackrabbit.core.PropertyImpl) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

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