Search in sources :

Example 1 with ResultRow

use of org.apache.jackrabbit.oak.api.ResultRow in project jackrabbit-oak by apache.

the class IdentifierManager method getReferences.

/**
     * Searches all reference properties to the specified {@code tree} that match
     * the given {@code propertyName} and the specified, mandatory node type
     * constraint ({@code ntName}). In contrast to {@link #getReferences} this
     * method requires all parameters to be specified, which eases the handling
     * of the result set and doesn't require the trees associated with the
     * result set to be resolved.
     *
     * @param tree The tree for which references should be searched.
     * @param propertyName The name of the reference properties.
     * @param ntName The node type name to be used for the query.
     * @param weak if {@code true} only weak references are returned. Otherwise on hard references are returned.
     * @return A set of oak paths of those reference properties referring to the
     *         specified {@code tree} and matching the constraints.
     */
@Nonnull
public Iterable<String> getReferences(@Nonnull Tree tree, @Nonnull final String propertyName, @Nonnull String ntName, boolean weak) {
    if (!nodeTypeManager.isNodeType(tree, JcrConstants.MIX_REFERENCEABLE)) {
        // shortcut
        return Collections.emptySet();
    }
    final String uuid = getIdentifier(tree);
    String reference = weak ? PropertyType.TYPENAME_WEAKREFERENCE : PropertyType.TYPENAME_REFERENCE;
    Map<String, ? extends PropertyValue> bindings = Collections.singletonMap("uuid", PropertyValues.newString(uuid));
    try {
        String escapedPropName = QueryUtils.escapeForQuery(propertyName);
        Result result = root.getQueryEngine().executeQuery("SELECT * FROM [" + ntName + "] WHERE PROPERTY([" + escapedPropName + "], '" + reference + "') = $uuid" + QueryEngine.INTERNAL_SQL2_QUERY, Query.JCR_SQL2, bindings, NO_MAPPINGS);
        Iterable<String> resultPaths = Iterables.transform(result.getRows(), new Function<ResultRow, String>() {

            @Override
            public String apply(ResultRow row) {
                return PathUtils.concat(row.getPath(), propertyName);
            }
        });
        return Iterables.filter(resultPaths, new Predicate<String>() {

            @Override
            public boolean apply(String path) {
                return !path.startsWith(VersionConstants.VERSION_STORE_PATH);
            }
        });
    } catch (ParseException e) {
        log.error("query failed", e);
        return Collections.emptySet();
    }
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) ParseException(java.text.ParseException) Result(org.apache.jackrabbit.oak.api.Result) Nonnull(javax.annotation.Nonnull)

Example 2 with ResultRow

use of org.apache.jackrabbit.oak.api.ResultRow in project jackrabbit-oak by apache.

the class AbstractQueryTest method executeQuery.

protected List<String> executeQuery(String query, String language, boolean pathsOnly, boolean skipSort) {
    long time = System.currentTimeMillis();
    List<String> lines = new ArrayList<String>();
    try {
        Result result = executeQuery(query, language, NO_BINDINGS);
        for (ResultRow row : result.getRows()) {
            String r = readRow(row, pathsOnly);
            if (query.startsWith("explain ")) {
                r = formatPlan(r);
            }
            lines.add(r);
        }
        if (!query.contains("order by") && !skipSort) {
            Collections.sort(lines);
        }
    } catch (ParseException e) {
        lines.add(e.toString());
    } catch (IllegalArgumentException e) {
        lines.add(e.toString());
    }
    time = System.currentTimeMillis() - time;
    if (time > 5 * 60 * 1000 && !isDebugModeEnabled()) {
        // more than 5 minutes
        fail("Query took too long: " + query + " took " + time + " ms");
    }
    return lines;
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) Result(org.apache.jackrabbit.oak.api.Result)

Example 3 with ResultRow

use of org.apache.jackrabbit.oak.api.ResultRow in project jackrabbit-oak by apache.

the class IdentifierManager method resolveUUID.

private String resolveUUID(PropertyValue uuid) {
    try {
        Map<String, PropertyValue> bindings = Collections.singletonMap("id", uuid);
        Result result = root.getQueryEngine().executeQuery("SELECT * FROM [nt:base] WHERE [jcr:uuid] = $id" + QueryEngine.INTERNAL_SQL2_QUERY, Query.JCR_SQL2, bindings, NO_MAPPINGS);
        String path = null;
        for (ResultRow rr : result.getRows()) {
            if (path != null) {
                log.error("multiple results for identifier lookup: " + path + " vs. " + rr.getPath());
                return null;
            } else {
                path = rr.getPath();
            }
        }
        return path;
    } catch (ParseException ex) {
        log.error("query failed", ex);
        return null;
    }
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) PropertyValue(org.apache.jackrabbit.oak.api.PropertyValue) ParseException(java.text.ParseException) Result(org.apache.jackrabbit.oak.api.Result)

Example 4 with ResultRow

use of org.apache.jackrabbit.oak.api.ResultRow in project jackrabbit-oak by apache.

the class AccessControlManagerImpl method getEffectivePolicies.

@Nonnull
@Override
public AccessControlPolicy[] getEffectivePolicies(@Nonnull Set<Principal> principals) throws RepositoryException {
    Util.checkValidPrincipals(principals, principalManager);
    Root r = getLatestRoot();
    Result aceResult = searchAces(principals, r);
    Set<JackrabbitAccessControlList> effective = Sets.newTreeSet(new Comparator<JackrabbitAccessControlList>() {

        @Override
        public int compare(JackrabbitAccessControlList list1, JackrabbitAccessControlList list2) {
            if (list1.equals(list2)) {
                return 0;
            } else {
                String p1 = list1.getPath();
                String p2 = list2.getPath();
                if (p1 == null) {
                    return -1;
                } else if (p2 == null) {
                    return 1;
                } else {
                    int depth1 = PathUtils.getDepth(p1);
                    int depth2 = PathUtils.getDepth(p2);
                    return (depth1 == depth2) ? p1.compareTo(p2) : Ints.compare(depth1, depth2);
                }
            }
        }
    });
    Set<String> paths = Sets.newHashSet();
    for (ResultRow row : aceResult.getRows()) {
        String acePath = row.getPath();
        String aclName = Text.getName(Text.getRelativeParent(acePath, 1));
        Tree accessControlledTree = r.getTree(Text.getRelativeParent(acePath, 2));
        if (aclName.isEmpty() || !accessControlledTree.exists()) {
            log.debug("Isolated access control entry -> ignore query result at " + acePath);
            continue;
        }
        String path = (REP_REPO_POLICY.equals(aclName)) ? null : accessControlledTree.getPath();
        if (paths.contains(path)) {
            continue;
        }
        JackrabbitAccessControlList policy = createACL(path, accessControlledTree, true, new AcePredicate(principals));
        if (policy != null) {
            effective.add(policy);
            paths.add(path);
        }
    }
    return effective.toArray(new AccessControlPolicy[effective.size()]);
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) Root(org.apache.jackrabbit.oak.api.Root) Tree(org.apache.jackrabbit.oak.api.Tree) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) Result(org.apache.jackrabbit.oak.api.Result) Nonnull(javax.annotation.Nonnull)

Example 5 with ResultRow

use of org.apache.jackrabbit.oak.api.ResultRow in project jackrabbit-oak by apache.

the class AccessControlManagerImpl method createPrincipalACL.

@Nullable
private JackrabbitAccessControlList createPrincipalACL(@Nullable String oakPath, @Nonnull Principal principal) throws RepositoryException {
    Root root = getRoot();
    Result aceResult = searchAces(Collections.<Principal>singleton(principal), root);
    RestrictionProvider restrProvider = new PrincipalRestrictionProvider(restrictionProvider);
    List<ACE> entries = new ArrayList<ACE>();
    for (ResultRow row : aceResult.getRows()) {
        Tree aceTree = root.getTree(row.getPath());
        if (Util.isACE(aceTree, ntMgr)) {
            String aclPath = Text.getRelativeParent(aceTree.getPath(), 1);
            String path;
            if (aclPath.endsWith(REP_REPO_POLICY)) {
                path = null;
            } else {
                path = Text.getRelativeParent(aclPath, 1);
            }
            entries.add(createACE(path, aceTree, restrProvider));
        }
    }
    if (entries.isEmpty()) {
        // nothing found
        return null;
    } else {
        return new PrincipalACL(oakPath, principal, entries, restrProvider);
    }
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) ACE(org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.ACE) Root(org.apache.jackrabbit.oak.api.Root) PrincipalRestrictionProvider(org.apache.jackrabbit.oak.security.authorization.restriction.PrincipalRestrictionProvider) PrincipalRestrictionProvider(org.apache.jackrabbit.oak.security.authorization.restriction.PrincipalRestrictionProvider) RestrictionProvider(org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionProvider) ArrayList(java.util.ArrayList) Tree(org.apache.jackrabbit.oak.api.Tree) Result(org.apache.jackrabbit.oak.api.Result) Nullable(javax.annotation.Nullable)

Aggregations

ResultRow (org.apache.jackrabbit.oak.api.ResultRow)41 Test (org.junit.Test)29 PropertyValue (org.apache.jackrabbit.oak.api.PropertyValue)26 RemoteValue (org.apache.jackrabbit.oak.remote.RemoteValue)24 Result (org.apache.jackrabbit.oak.api.Result)12 Tree (org.apache.jackrabbit.oak.api.Tree)4 ParseException (java.text.ParseException)3 Root (org.apache.jackrabbit.oak.api.Root)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 NoSuchElementException (java.util.NoSuchElementException)2 Nonnull (javax.annotation.Nonnull)2 NodeIterator (javax.jcr.NodeIterator)2 RowIterator (javax.jcr.query.RowIterator)2 Blob (org.apache.jackrabbit.oak.api.Blob)2 PrefetchOptions (org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 BigDecimal (java.math.BigDecimal)1 HashSet (java.util.HashSet)1