Search in sources :

Example 91 with QueryResult

use of javax.jcr.query.QueryResult 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)

Example 92 with QueryResult

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

the class ShareableNodeTest method testSearch.

/**
     * Verify that a descendant of a shareable node appears once in the
     * result set (6.13.23)
     */
public void testSearch() throws Exception {
    // setup parent nodes and first child
    Node a1 = testRootNode.addNode("a1");
    Node a2 = testRootNode.addNode("a2");
    Node b1 = a1.addNode("b1");
    testRootNode.getSession().save();
    // add mixin
    ensureMixinType(b1, mixShareable);
    b1.save();
    // clone
    Workspace workspace = b1.getSession().getWorkspace();
    workspace.clone(workspace.getName(), b1.getPath(), a2.getPath() + "/b2", false);
    // add new referenceable child
    Node c = b1.addNode("c");
    ensureMixinType(c, mixReferenceable);
    b1.save();
    String sql = "SELECT * FROM nt:unstructured WHERE jcr:uuid = '" + c.getUUID() + "'";
    QueryResult res = workspace.getQueryManager().createQuery(sql, Query.SQL).execute();
    List<Node> list = new ArrayList<Node>();
    NodeIterator iter = res.getNodes();
    while (iter.hasNext()) {
        list.add(iter.nextNode());
    }
    assertEquals(1, list.size());
    assertTrue(list.get(0).isSame(c));
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) Workspace(javax.jcr.Workspace)

Example 93 with QueryResult

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

the class TextExtractionQueryTest method testScoreWithoutFulltext.

public void testScoreWithoutFulltext() throws Exception {
    System.out.println(Query.JCR_SQL2);
    QueryResult r = executeSQL2Query("select [jcr:path] from [nt:base] order by [jcr:score]");
    RowIterator it = r.getRows();
    while (it.hasNext()) {
        it.nextRow();
    }
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator)

Example 94 with QueryResult

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

the class FacetTest method testFacetRetrievalNumberOfFacetsConfiguredLowerThanDefault.

public void testFacetRetrievalNumberOfFacetsConfiguredLowerThanDefault() throws RepositoryException {
    Node facetsConfig = superuser.getNode(FACET_CONFING_NODE_PATH);
    facetsConfig.setProperty(LuceneIndexConstants.PROP_FACETS_TOP_CHILDREN, 7);
    markIndexForReindex();
    superuser.save();
    superuser.refresh(true);
    Session session = superuser;
    Node n1 = testRootNode.addNode("node1");
    String pn = "jcr:title";
    n1.setProperty(pn, "hello 1");
    Node n2 = testRootNode.addNode("node2");
    n2.setProperty(pn, "hallo 2");
    Node n3 = testRootNode.addNode("node3");
    n3.setProperty(pn, "hallo 3");
    Node n4 = testRootNode.addNode("node4");
    n4.setProperty(pn, "hallo 4");
    Node n5 = testRootNode.addNode("node5");
    n5.setProperty(pn, "hallo 5");
    Node n6 = testRootNode.addNode("node6");
    n6.setProperty(pn, "hallo 6");
    Node n7 = testRootNode.addNode("node7");
    n7.setProperty(pn, "hallo 7");
    Node n8 = testRootNode.addNode("node8");
    n8.setProperty(pn, "hallo 8");
    Node n9 = testRootNode.addNode("node9");
    n9.setProperty(pn, "hallo 9");
    Node n10 = testRootNode.addNode("node10");
    n10.setProperty(pn, "hallo 10");
    Node n11 = testRootNode.addNode("node11");
    n11.setProperty(pn, "hallo 11");
    Node n12 = testRootNode.addNode("node12");
    n12.setProperty(pn, "hallo 12");
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    String sql2 = "select [jcr:path], [rep:facet(" + pn + ")] from [nt:base] " + "where contains([" + pn + "], 'hallo') order by [jcr:path]";
    Query q = qm.createQuery(sql2, Query.JCR_SQL2);
    QueryResult result = q.execute();
    FacetResult facetResult = new FacetResult(result);
    assertNotNull(facetResult);
    assertNotNull(facetResult.getDimensions());
    assertEquals(1, facetResult.getDimensions().size());
    assertTrue(facetResult.getDimensions().contains(pn));
    List<FacetResult.Facet> facets = facetResult.getFacets(pn);
    assertNotNull(facets);
    assertEquals(7, facets.size());
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) FacetResult(org.apache.jackrabbit.oak.query.facet.FacetResult) Session(javax.jcr.Session)

Example 95 with QueryResult

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

the class LuceneIndexSuggestionTest method suggestNodeName.

//OAK-3825
@Test
public void suggestNodeName() throws Exception {
    final String nodeType = "nt:unstructured";
    createSuggestIndex("lucene-suggest", nodeType, LuceneIndexConstants.PROPDEF_PROP_NODE_NAME);
    root.addNode("indexedNode", nodeType);
    session.save();
    String suggQuery = createSuggestQuery(nodeType, "indexedn");
    QueryManager queryManager = session.getWorkspace().getQueryManager();
    QueryResult result = queryManager.createQuery(suggQuery, Query.JCR_SQL2).execute();
    RowIterator rows = result.getRows();
    String value = null;
    while (rows.hasNext()) {
        Row firstRow = rows.nextRow();
        value = firstRow.getValue("suggestion").getString();
        break;
    }
    assertEquals("Node name should be suggested", "indexedNode", value);
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Row(javax.jcr.query.Row) Test(org.junit.Test)

Aggregations

QueryResult (javax.jcr.query.QueryResult)202 Node (javax.jcr.Node)109 Query (javax.jcr.query.Query)98 QueryManager (javax.jcr.query.QueryManager)55 NodeIterator (javax.jcr.NodeIterator)52 RowIterator (javax.jcr.query.RowIterator)46 Session (javax.jcr.Session)36 Test (org.junit.Test)32 Row (javax.jcr.query.Row)21 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)21 RepositoryException (javax.jcr.RepositoryException)15 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)15 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 Value (javax.jcr.Value)8 NoSuchElementException (java.util.NoSuchElementException)7 ArrayList (java.util.ArrayList)6 ValueFactory (javax.jcr.ValueFactory)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)5 JackrabbitQueryResult (org.apache.jackrabbit.api.query.JackrabbitQueryResult)5 TreeSet (java.util.TreeSet)4