Search in sources :

Example 6 with BoolFilterBuilder

use of org.elasticsearch.index.query.BoolFilterBuilder in project stash-codesearch-plugin by palantir.

the class SearchFilterUtils method aclFilter.

public FilterBuilder aclFilter(Map<String, Repository> repoMap) {
    if (repoMap.isEmpty()) {
        return boolFilter().mustNot(matchAllFilter());
    }
    // Compute cryptographic hash of repository set to use for cache key
    String[] projectRepoPairs = repoMap.keySet().toArray(new String[repoMap.size()]);
    Arrays.sort(projectRepoPairs);
    String filterHash;
    try {
        MessageDigest hasher = MessageDigest.getInstance("SHA-256");
        for (String pair : projectRepoPairs) {
            hasher.update(pair.getBytes());
            hasher.update((byte) 0);
        }
        filterHash = new String(Base64.encodeBase64(hasher.digest()));
    } catch (Exception e) {
        filterHash = null;
        log.error("Caught exception generating ACL hash -- caching is disabled.", e);
    }
    // Create disjunction of individual repo ACL filters
    BoolFilterBuilder filter = boolFilter();
    if (filterHash != null) {
        filter.cache(true).cacheKey("CACHE^ACLORFILTER^" + filterHash);
    } else {
        filter.cache(false);
    }
    for (Repository repo : repoMap.values()) {
        filter.should(projectRepositoryFilter(repo.getProject().getKey(), repo.getSlug()));
    }
    return filter;
}
Also used : Repository(com.atlassian.stash.repository.Repository) MessageDigest(java.security.MessageDigest) BoolFilterBuilder(org.elasticsearch.index.query.BoolFilterBuilder)

Aggregations

BoolFilterBuilder (org.elasticsearch.index.query.BoolFilterBuilder)6 Repository (com.atlassian.stash.repository.Repository)1 MessageDigest (java.security.MessageDigest)1