Search in sources :

Example 11 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class QueryResultTest method testGetSize.

public void testGetSize() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000]";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        assertEquals("Wrong size of NodeIterator in result", INITIAL_NODE_NUM - i, result.getNodes().getSize());
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

Example 12 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class QueryResultTest method testGetSizeOrderByScore.

public void testGetSizeOrderByScore() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000] order by jcr:score()";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        assertEquals("Wrong size of NodeIterator in result", INITIAL_NODE_NUM - i, result.getNodes().getSize());
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

Example 13 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class QueryResultTest method testGetPosition.

public void testGetPosition() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000]";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        NodeIterator it = result.getNodes();
        assertEquals("Wrong position", 0, it.getPosition());
        int count = 0;
        while (it.hasNext()) {
            long position = it.getPosition();
            it.nextNode();
            assertEquals("Wrong position", count++, position);
        }
        try {
            it.next();
            fail("must throw NoSuchElementException");
        } catch (Exception e) {
        // correct
        }
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) RepositoryException(javax.jcr.RepositoryException) NoSuchElementException(java.util.NoSuchElementException)

Example 14 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class QueryResultTest method testIteratorNextOrderByScore.

public void testIteratorNextOrderByScore() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000] order by jcr:score()";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        int size = 0;
        for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
            it.nextNode();
            size++;
        }
        assertEquals("Wrong size of NodeIterator in result", INITIAL_NODE_NUM - i, size);
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

Example 15 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class ACLProvider method getEffectivePolicies.

/**
     * @see org.apache.jackrabbit.core.security.authorization.AccessControlProvider#getEffectivePolicies(org.apache.jackrabbit.spi.Path,org.apache.jackrabbit.core.security.authorization.CompiledPermissions)
     */
public AccessControlPolicy[] getEffectivePolicies(Path absPath, CompiledPermissions permissions) throws ItemNotFoundException, RepositoryException {
    if (absPath == null) {
        // TODO: JCR-2774
        log.warn("TODO: JCR-2774 - Repository level permissions.");
        return new AccessControlPolicy[0];
    }
    String jcrPath = session.getJCRPath(absPath);
    String pName = ISO9075.encode(session.getJCRName(ACLTemplate.P_NODE_PATH));
    int ancestorCnt = absPath.getAncestorCount();
    // search all ACEs whose rep:nodePath property equals the specified
    // absPath or any of it's ancestors
    StringBuilder stmt = new StringBuilder("/jcr:root");
    stmt.append(acRoot.getPath());
    stmt.append("//element(*,");
    stmt.append(session.getJCRName(NT_REP_ACE));
    stmt.append(")[");
    for (int i = 0; i <= ancestorCnt; i++) {
        String path = Text.getRelativeParent(jcrPath, i);
        if (i > 0) {
            stmt.append(" or ");
        }
        stmt.append("@");
        stmt.append(pName);
        stmt.append("='");
        stmt.append(path.replaceAll("'", "''"));
        stmt.append("'");
    }
    stmt.append("]");
    QueryResult result;
    try {
        QueryManager qm = session.getWorkspace().getQueryManager();
        Query q = qm.createQuery(stmt.toString(), Query.XPATH);
        result = q.execute();
    } catch (RepositoryException e) {
        log.error("Unexpected error while searching effective policies. {}", e.getMessage());
        throw new UnsupportedOperationException("Retrieve effective policies at absPath '" + jcrPath + "' not supported.", e);
    }
    /**
         * Loop over query results and verify that
         * - the corresponding ACE really takes effect on the specified absPath.
         * - the corresponding ACL can be read by the editing session.
         */
    Set<AccessControlPolicy> acls = new LinkedHashSet<AccessControlPolicy>();
    for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
        Node aceNode = it.nextNode();
        String accessControlledNodePath = Text.getRelativeParent(aceNode.getPath(), 2);
        Path acPath = session.getQPath(accessControlledNodePath);
        AccessControlPolicy[] policies = editor.getPolicies(accessControlledNodePath);
        if (policies.length > 0) {
            ACLTemplate acl = (ACLTemplate) policies[0];
            for (AccessControlEntry ace : acl.getAccessControlEntries()) {
                ACLTemplate.Entry entry = (ACLTemplate.Entry) ace;
                if (entry.matches(jcrPath)) {
                    if (permissions.grants(acPath, Permission.READ_AC)) {
                        acls.add(new UnmodifiableAccessControlList(acl));
                        break;
                    } else {
                        throw new AccessDeniedException("Access denied at " + accessControlledNodePath);
                    }
                }
            }
        }
    }
    return acls.toArray(new AccessControlPolicy[acls.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NodeIterator(javax.jcr.NodeIterator) Path(org.apache.jackrabbit.spi.Path) AccessControlPolicy(javax.jcr.security.AccessControlPolicy) AccessDeniedException(javax.jcr.AccessDeniedException) Query(javax.jcr.query.Query) Node(javax.jcr.Node) AccessControlEntry(javax.jcr.security.AccessControlEntry) RepositoryException(javax.jcr.RepositoryException) QueryResult(javax.jcr.query.QueryResult) AccessControlEntry(javax.jcr.security.AccessControlEntry) QueryManager(javax.jcr.query.QueryManager) UnmodifiableAccessControlList(org.apache.jackrabbit.core.security.authorization.UnmodifiableAccessControlList)

Aggregations

QueryManager (javax.jcr.query.QueryManager)102 Query (javax.jcr.query.Query)68 Node (javax.jcr.Node)60 QueryResult (javax.jcr.query.QueryResult)54 Session (javax.jcr.Session)53 NodeIterator (javax.jcr.NodeIterator)34 RowIterator (javax.jcr.query.RowIterator)27 Test (org.junit.Test)27 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)22 RepositoryException (javax.jcr.RepositoryException)17 Row (javax.jcr.query.Row)14 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)14 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 ValueFactory (javax.jcr.ValueFactory)7 NoSuchElementException (java.util.NoSuchElementException)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)3 Value (javax.jcr.Value)3 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2