Search in sources :

Example 6 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(java.util.Set, CompiledPermissions)
     */
public AccessControlPolicy[] getEffectivePolicies(Set<Principal> principals, CompiledPermissions permissions) throws RepositoryException {
    String propName = ISO9075.encode(session.getJCRName(P_PRINCIPAL_NAME));
    StringBuilder stmt = new StringBuilder("/jcr:root");
    stmt.append("//element(*,");
    stmt.append(session.getJCRName(NT_REP_ACE));
    stmt.append(")[");
    int i = 0;
    for (Principal principal : principals) {
        if (i > 0) {
            stmt.append(" or ");
        }
        stmt.append("@");
        stmt.append(propName);
        stmt.append("='");
        stmt.append(principal.getName().replaceAll("'", "''"));
        stmt.append("'");
        i++;
    }
    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 for set of principals not supported.", e);
    }
    Set<AccessControlPolicy> acls = new LinkedHashSet<AccessControlPolicy>();
    for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
        NodeImpl aclNode = (NodeImpl) it.nextNode().getParent();
        Name aclName = aclNode.getQName();
        NodeImpl accessControlledNode = (NodeImpl) aclNode.getParent();
        if (N_POLICY.equals(aclName) && isAccessControlled(accessControlledNode)) {
            if (permissions.canRead(aclNode.getPrimaryPath(), aclNode.getNodeId())) {
                acls.add(getACL(accessControlledNode, N_POLICY, accessControlledNode.getPath()));
            } else {
                throw new AccessDeniedException("Access denied at " + Text.getRelativeParent(aclNode.getPath(), 1));
            }
        } else if (N_REPO_POLICY.equals(aclName) && isRepoAccessControlled(accessControlledNode)) {
            if (permissions.canRead(aclNode.getPrimaryPath(), aclNode.getNodeId())) {
                acls.add(getACL(accessControlledNode, N_REPO_POLICY, null));
            } else {
                throw new AccessDeniedException("Access denied at " + Text.getRelativeParent(aclNode.getPath(), 1));
            }
        }
    // else: not a regular policy node -> ignore.
    }
    return acls.toArray(new AccessControlPolicy[acls.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NodeIterator(javax.jcr.NodeIterator) AccessControlPolicy(javax.jcr.security.AccessControlPolicy) AccessDeniedException(javax.jcr.AccessDeniedException) Query(javax.jcr.query.Query) NodeImpl(org.apache.jackrabbit.core.NodeImpl) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) Principal(java.security.Principal)

Example 7 with QueryManager

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

the class QueryTest method testOak1096.

@Test
public void testOak1096() throws RepositoryException {
    Session writer = createAdminSession();
    Session reader = createAdminSession();
    try {
        Node rootNode = writer.getRootNode();
        Node node = rootNode.addNode("test", "nt:unstructured");
        node.setProperty("text", "find me");
        writer.save();
        QueryManager qm = reader.getWorkspace().getQueryManager();
        Query q = qm.createQuery("select * from 'nt:base' where contains(*, 'find me')", Query.JCR_SQL2);
        NodeIterator res = q.execute().getNodes();
        assertEquals("False amount of hits", 1, res.getSize());
    } finally {
        if (reader != null) {
            reader.logout();
        }
        if (writer != null) {
            writer.logout();
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 8 with QueryManager

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

the class QueryTest method traversalOption.

@Test
public void traversalOption() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    // for union queries:
    // both subqueries use an index
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where ischildnode('/') or [jcr:uuid] = 1 option(traversal fail)"));
    // no subquery uses an index
    assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [x] = 1 or [y] = 2 option(traversal fail)"));
    // first one does not, second one does
    assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [jcr:uuid] = 1 or [x] = 2 option(traversal fail)"));
    // first one does, second one does not
    assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [x] = 2 or [jcr:uuid] = 1 option(traversal fail)"));
    // queries that possibly use traversal (depending on the join order)
    assertTrue(isValidQuery(qm, "xpath", "/jcr:root/content//*/jcr:content[@jcr:uuid='1'] option(traversal fail)"));
    assertTrue(isValidQuery(qm, "xpath", "/jcr:root/content/*/jcr:content[@jcr:uuid='1'] option(traversal fail)"));
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(b, a) " + "where [a].[jcr:uuid] = 1 option(traversal fail)"));
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(a, b) " + "where [a].[jcr:uuid] = 1 option(traversal fail)"));
    // union with joins
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(a, b) " + "where ischildnode([a], '/') or [a].[jcr:uuid] = 1 option(traversal fail)"));
    assertFalse(isValidQuery(qm, "xpath", "//*[@test] option(traversal fail)"));
    assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal fail)"));
    assertTrue(isValidQuery(qm, "xpath", "//*[@test] option(traversal ok)"));
    assertTrue(isValidQuery(qm, "xpath", "//*[@test] option(traversal warn)"));
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal ok)"));
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal warn)"));
    // the following is not really traversal, it is just listing child nodes:
    assertTrue(isValidQuery(qm, "xpath", "/jcr:root/*[@test] option(traversal fail)"));
    // the following is not really traversal; it is just one node:
    assertTrue(isValidQuery(qm, "xpath", "/jcr:root/oak:index[@test] option(traversal fail)"));
}
Also used : QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 9 with QueryManager

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

the class QueryTest method skip.

@Test
public void skip() throws RepositoryException {
    Session session = getAdminSession();
    Node hello1 = session.getRootNode().addNode("hello1");
    hello1.setProperty("id", "1");
    hello1.setProperty("data", "x");
    session.save();
    Node hello3 = hello1.addNode("hello3");
    hello3.setProperty("id", "3");
    hello3.setProperty("data", "z");
    session.save();
    Node hello2 = hello3.addNode("hello2");
    hello2.setProperty("id", "2");
    hello2.setProperty("data", "y");
    session.save();
    ValueFactory vf = session.getValueFactory();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("select id from [nt:base] where data >= $data order by id", Query.JCR_SQL2);
    q.bindValue("data", vf.createValue("x"));
    for (int i = -1; i < 5; i++) {
        QueryResult r = q.execute();
        RowIterator it = r.getRows();
        assertEquals(3, r.getRows().getSize());
        assertEquals(3, r.getNodes().getSize());
        Row row;
        try {
            it.skip(i);
            assertTrue(i >= 0 && i <= 3);
        } catch (IllegalArgumentException e) {
            assertEquals(-1, i);
        } catch (NoSuchElementException e) {
            assertTrue(i >= 2);
        }
        if (i <= 0) {
            assertTrue(it.hasNext());
            row = it.nextRow();
            assertEquals("1", row.getValue("id").getString());
        }
        if (i <= 1) {
            assertTrue(it.hasNext());
            row = it.nextRow();
            assertEquals("2", row.getValue("id").getString());
        }
        if (i <= 2) {
            assertTrue(it.hasNext());
            row = it.nextRow();
            assertEquals("3", row.getValue("id").getString());
        }
        assertFalse(it.hasNext());
    }
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) ValueFactory(javax.jcr.ValueFactory) Row(javax.jcr.query.Row) NoSuchElementException(java.util.NoSuchElementException) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 10 with QueryManager

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

the class QueryFulltextTest method excerpt.

@Test
public void excerpt() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    Node n1 = testRootNode.addNode("node1");
    n1.setProperty("text", "hello world");
    n1.setProperty("desc", "description");
    Node n2 = testRootNode.addNode("node2");
    n2.setProperty("text", "Hello World");
    n2.setProperty("desc", "Description");
    session.save();
    Query q;
    RowIterator it;
    Row row;
    String s;
    String xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by @jcr:path";
    q = qm.createQuery(xpath, "xpath");
    it = q.execute().getRows();
    row = it.nextRow();
    String path = row.getPath();
    s = row.getValue("rep:excerpt(.)").getString();
    assertTrue(path + ":" + s + " (1)", s.indexOf("<strong>hello</strong> world") >= 0);
    assertTrue(path + ":" + s + " (2)", s.indexOf("description") >= 0);
    row = it.nextRow();
    path = row.getPath();
    s = row.getValue("rep:excerpt(.)").getString();
    // TODO is this expected?
    assertTrue(path + ":" + s + " (3)", s.indexOf("Hello World") >= 0);
    assertTrue(path + ":" + s + " (4)", s.indexOf("Description") >= 0);
    xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by @jcr:path";
    q = qm.createQuery(xpath, "xpath");
    it = q.execute().getRows();
    row = it.nextRow();
    path = row.getPath();
    s = row.getValue("rep:excerpt(text)").getString();
    assertTrue(path + ":" + s + " (5)", s.indexOf("<strong>hello</strong> world") >= 0);
    assertTrue(path + ":" + s + " (6)", s.indexOf("description") < 0);
    row = it.nextRow();
    path = row.getPath();
    s = row.getValue("rep:excerpt(text)").getString();
    // TODO is this expected?
    assertTrue(path + ":" + s + " (7)", s.indexOf("Hello World") >= 0);
    assertTrue(path + ":" + s + " (8)", s.indexOf("Description") < 0);
}
Also used : Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Row(javax.jcr.query.Row) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Aggregations

QueryManager (javax.jcr.query.QueryManager)103 Query (javax.jcr.query.Query)69 Node (javax.jcr.Node)61 QueryResult (javax.jcr.query.QueryResult)55 Session (javax.jcr.Session)54 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)18 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