Search in sources :

Example 31 with NodeIterator

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

the class UserManagerImpl method findAuthorizables.

/**
     * @see UserManager#findAuthorizables(String,String, int)
     */
public Iterator<Authorizable> findAuthorizables(String relPath, String value, int searchType) throws RepositoryException {
    if (searchType < SEARCH_TYPE_USER || searchType > SEARCH_TYPE_AUTHORIZABLE) {
        throw new IllegalArgumentException("Invalid search type " + searchType);
    }
    Path path = session.getQPath(relPath);
    NodeIterator nodes;
    if (relPath.indexOf('/') == -1) {
        // search for properties somewhere below an authorizable node
        nodes = authResolver.findNodes(path, value, searchType, true, Long.MAX_VALUE);
    } else {
        path = path.getNormalizedPath();
        if (path.getLength() == 1) {
            // only search below the authorizable node
            Name ntName;
            switch(searchType) {
                case SEARCH_TYPE_GROUP:
                    ntName = NT_REP_GROUP;
                    break;
                case SEARCH_TYPE_USER:
                    ntName = NT_REP_USER;
                    break;
                default:
                    ntName = NT_REP_AUTHORIZABLE;
            }
            nodes = authResolver.findNodes(path.getName(), value, ntName, true);
        } else {
            // search below authorizable nodes but take some path constraints
            // into account.
            nodes = authResolver.findNodes(path, value, searchType, true, Long.MAX_VALUE);
        }
    }
    return new AuthorizableIterator(nodes);
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeIterator(javax.jcr.NodeIterator) Name(org.apache.jackrabbit.spi.Name)

Example 32 with NodeIterator

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

the class ConcurrentQueriesWithUpdatesTest method testQueriesWithUpdates.

public void testQueriesWithUpdates() throws Exception {
    final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());
    final AtomicBoolean running = new AtomicBoolean(true);
    // track executed queries and do updates at most at the given rate
    final BlockingQueue<Object> queryExecuted = new LinkedBlockingQueue<Object>();
    Thread queries = new Thread(new Runnable() {

        public void run() {
            try {
                runTask(new Task() {

                    public void execute(Session session, Node test) throws RepositoryException {
                        QueryManager qm = session.getWorkspace().getQueryManager();
                        while (running.get()) {
                            Query q = qm.createQuery(testPath + "//element(*, nt:unstructured) order by @jcr:score descending", Query.XPATH);
                            NodeIterator nodes = q.execute().getNodes();
                            assertEquals("wrong result set size", numNodes, nodes.getSize());
                            queryExecuted.offer(new Object());
                        }
                    }
                }, 5, testRootNode.getPath());
            } catch (RepositoryException e) {
                exceptions.add(e);
            }
        }
    });
    queries.start();
    Thread update = new Thread(new Runnable() {

        public void run() {
            try {
                runTask(new Task() {

                    public void execute(Session session, Node test) throws RepositoryException {
                        Random rand = new Random();
                        QueryManager qm = session.getWorkspace().getQueryManager();
                        for (int i = 0; i < NUM_UPDATES; i++) {
                            try {
                                // wait at most 10 seconds
                                queryExecuted.poll(10, TimeUnit.SECONDS);
                            } catch (InterruptedException e) {
                            // ignore
                            }
                            Query q = qm.createQuery(testPath + "//node" + rand.nextInt(numNodes) + " order by @jcr:score descending", Query.XPATH);
                            NodeIterator nodes = q.execute().getNodes();
                            if (nodes.hasNext()) {
                                Node n = nodes.nextNode();
                                n.setProperty("foo", "bar");
                                session.save();
                            }
                        }
                    }
                }, 1, testRootNode.getPath());
            } catch (RepositoryException e) {
                exceptions.add(e);
            }
        }
    });
    update.start();
    update.join();
    running.set(false);
    queries.join();
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) RepositoryException(javax.jcr.RepositoryException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session)

Example 33 with NodeIterator

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

the class AxisQueryTest method testExecuteQueries.

public void testExecuteQueries() throws RepositoryException {
    Node testLocation = superuser.getRootNode().getNode(relTestLocation);
    long end = System.currentTimeMillis() + 1000 * RUN_NUM_SECONDS;
    while (end > System.currentTimeMillis()) {
        StringBuffer statement = new StringBuffer(relTestLocation);
        StringBuffer regexp = new StringBuffer(absTestLocation);
        // at least one step
        int numSteps = rand.nextInt(NUM_LEVELS) + 1;
        while (numSteps-- > 0) {
            String axis = getRandomAxis();
            String name = getRandomName();
            statement.append(axis).append(name);
            if (axis.equals("//")) {
                regexp.append("/(.*/)?");
            } else {
                regexp.append("/");
            }
            if (name.equals("*")) {
                regexp.append("[^/]*");
            } else {
                regexp.append(name);
            }
        }
        long time = System.currentTimeMillis();
        NodeIterator nodes = executeQuery(statement.toString()).getNodes();
        nodes.hasNext();
        time = System.currentTimeMillis() - time;
        log.print(statement + "\t" + nodes.getSize() + "\t" + time);
        if (CHECK_RESULT) {
            Set paths = new HashSet();
            time = System.currentTimeMillis();
            traversalEvaluate(testLocation, Pattern.compile(regexp.toString()), paths);
            time = System.currentTimeMillis() - time;
            log.println("\t" + time);
            assertEquals("wrong number of results", paths.size(), nodes.getSize());
            while (nodes.hasNext()) {
                String path = nodes.nextNode().getPath();
                assertTrue(path + " is not part of the result set", paths.contains(path));
            }
        } else {
            log.println();
        }
        log.flush();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) HashSet(java.util.HashSet) Set(java.util.Set) Node(javax.jcr.Node) HashSet(java.util.HashSet)

Example 34 with NodeIterator

use of javax.jcr.NodeIterator 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 35 with NodeIterator

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

the class NodeDiscoveringNodeTypesTest method setUp.

/**
     * Sets up the fixture for the test cases.
     */
protected void setUp() throws Exception {
    isReadOnly = true;
    super.setUp();
    session = getHelper().getReadOnlySession();
    testRootNode = session.getRootNode().getNode(testPath);
    NodeIterator nodes = testRootNode.getNodes();
    try {
        childNode = nodes.nextNode();
    } catch (NoSuchElementException e) {
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NodeIterator (javax.jcr.NodeIterator)307 Node (javax.jcr.Node)214 Session (javax.jcr.Session)55 QueryResult (javax.jcr.query.QueryResult)52 RepositoryException (javax.jcr.RepositoryException)40 Query (javax.jcr.query.Query)40 Test (org.junit.Test)36 QueryManager (javax.jcr.query.QueryManager)34 PropertyIterator (javax.jcr.PropertyIterator)30 ArrayList (java.util.ArrayList)26 Property (javax.jcr.Property)24 Version (javax.jcr.version.Version)23 NoSuchElementException (java.util.NoSuchElementException)19 Value (javax.jcr.Value)19 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)19 HashSet (java.util.HashSet)13 PathNotFoundException (javax.jcr.PathNotFoundException)12 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)11 NodeImpl (org.apache.jackrabbit.core.NodeImpl)11 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)11