Search in sources :

Example 36 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntries method loadVanityPaths.

/**
     * Load vanity paths Search for all nodes inheriting the sling:VanityPath
     * mixin
     */
private Map<String, List<String>> loadVanityPaths(boolean createVanityBloomFilter) {
    // sling:vanityPath (lowercase) is the property name
    final Map<String, List<String>> targetPaths = new ConcurrentHashMap<>();
    final String queryString = "SELECT sling:vanityPath, sling:redirect, sling:redirectStatus FROM nt:base WHERE sling:vanityPath IS NOT NULL";
    final Iterator<Resource> i = resolver.findResources(queryString, "sql");
    while (i.hasNext() && (createVanityBloomFilter || isAllVanityPathEntriesCached() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries())) {
        final Resource resource = i.next();
        boolean isValid = false;
        for (final Path sPath : this.factory.getObservationPaths()) {
            if (sPath.matches(resource.getPath())) {
                isValid = true;
                break;
            }
        }
        if (isValid) {
            if (isAllVanityPathEntriesCached() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries()) {
                // fill up the cache and the bloom filter
                loadVanityPath(resource, resolveMapsMap, targetPaths, true, createVanityBloomFilter);
            } else {
                // fill up the bloom filter
                loadVanityPath(resource, resolveMapsMap, targetPaths, false, createVanityBloomFilter);
            }
        }
    }
    return targetPaths;
}
Also used : Path(org.apache.sling.api.resource.path.Path) Resource(org.apache.sling.api.resource.Resource) List(java.util.List) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 37 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntries method gather.

private void gather(final ResourceResolver resolver, final List<MapEntry> entries, final Map<String, MapEntry> mapEntries, final Resource parent, final String parentPath) {
    // scheme list
    final Iterator<Resource> children = parent.listChildren();
    while (children.hasNext()) {
        final Resource child = children.next();
        final ValueMap vm = ResourceUtil.getValueMap(child);
        String name = vm.get(PROP_REG_EXP, String.class);
        boolean trailingSlash = false;
        if (name == null) {
            name = child.getName().concat("/");
            trailingSlash = true;
        }
        final String childPath = parentPath.concat(name);
        // hooked)
        if (!childPath.endsWith("$")) {
            // add trailing slash to child path to append the child
            String childParent = childPath;
            if (!trailingSlash) {
                childParent = childParent.concat("/");
            }
            gather(resolver, entries, mapEntries, child, childParent);
        }
        // add resolution entries for this node
        MapEntry childResolveEntry = null;
        try {
            childResolveEntry = MapEntry.createResolveEntry(childPath, child, trailingSlash);
        } catch (IllegalArgumentException iae) {
            //ignore this entry
            log.debug("ignored entry due exception ", iae);
        }
        if (childResolveEntry != null) {
            entries.add(childResolveEntry);
        }
        // add map entries for this node
        final List<MapEntry> childMapEntries = MapEntry.createMapEntry(childPath, child, trailingSlash);
        if (childMapEntries != null) {
            for (final MapEntry mapEntry : childMapEntries) {
                addMapEntry(mapEntries, mapEntry.getPattern(), mapEntry.getRedirect()[0], mapEntry.getStatus());
            }
        }
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource)

Example 38 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntries method doUpdateAlias.

/**
     * Update alias from a resource
     * @param resource The resource
     * @return {@code true} if any change
     */
private boolean doUpdateAlias(final Resource resource) {
    final Resource containingResource;
    if (JCR_CONTENT.equals(resource.getName())) {
        containingResource = resource.getParent();
    } else {
        containingResource = resource;
    }
    if (containingResource != null) {
        final String containingResourceName = containingResource.getName();
        final String parentPath = ResourceUtil.getParent(containingResource.getPath());
        final Map<String, String> aliasMapEntry = parentPath == null ? null : aliasMap.get(parentPath);
        if (aliasMapEntry != null) {
            for (Iterator<Map.Entry<String, String>> iterator = aliasMapEntry.entrySet().iterator(); iterator.hasNext(); ) {
                final Map.Entry<String, String> entry = iterator.next();
                if (containingResourceName.equals(entry.getValue())) {
                    iterator.remove();
                }
            }
        }
        if (aliasMapEntry != null && aliasMapEntry.isEmpty()) {
            this.aliasMap.remove(parentPath);
        }
        boolean changed = aliasMapEntry != null;
        if (containingResource.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS)) {
            changed |= doAddAlias(containingResource);
        }
        final Resource child = containingResource.getChild(JCR_CONTENT);
        if (child != null && child.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS)) {
            changed |= doAddAlias(child);
        }
        return changed;
    }
    return false;
}
Also used : Entry(java.util.Map.Entry) Resource(org.apache.sling.api.resource.Resource) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 39 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntries method addResource.

private boolean addResource(final String path, final AtomicBoolean resolverRefreshed) {
    this.initializing.lock();
    try {
        this.refreshResolverIfNecessary(resolverRefreshed);
        final Resource resource = resolver.getResource(path);
        if (resource != null) {
            boolean changed = doAddVanity(resource);
            if (this.factory.isOptimizeAliasResolutionEnabled() && resource.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS)) {
                changed |= doAddAlias(resource);
            }
            return changed;
        }
        return false;
    } finally {
        this.initializing.unlock();
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource)

Example 40 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class ResourceResolverImpl method create.

/**
     * @see org.apache.sling.api.resource.ResourceResolver#create(org.apache.sling.api.resource.Resource, java.lang.String, Map)
     */
@Override
public Resource create(final Resource parent, final String name, final Map<String, Object> properties) throws PersistenceException {
    // if parent or name is null, we get an NPE as stated in the API
    if (name == null) {
        throw new NullPointerException("name");
    }
    // name should be a name not a path
    if (name.indexOf("/") != -1) {
        throw new IllegalArgumentException("Name should not contain a slash: " + name);
    }
    final String path;
    if (parent.getPath().equals("/")) {
        path = parent.getPath() + name;
    } else {
        path = parent.getPath() + "/" + name;
    }
    // experimental code for handling synthetic resources
    if (ResourceUtil.isSyntheticResource(parent)) {
        Resource grandParent = parent.getParent();
        if (grandParent != null) {
            this.create(grandParent, parent.getName(), null);
        } else {
            throw new IllegalArgumentException("Can't create child on a synthetic root");
        }
    }
    final Resource rsrc = this.control.create(this.context, path, properties);
    rsrc.getResourceMetadata().setResolutionPath(rsrc.getPath());
    return this.factory.getResourceDecoratorTracker().decorate(rsrc);
}
Also used : StarResource(org.apache.sling.resourceresolver.impl.helper.StarResource) RedirectResource(org.apache.sling.resourceresolver.impl.helper.RedirectResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1569 Test (org.junit.Test)695 ValueMap (org.apache.sling.api.resource.ValueMap)338 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)260 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)164 HashMap (java.util.HashMap)160 Node (javax.jcr.Node)151 ArrayList (java.util.ArrayList)137 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)119 PersistenceException (org.apache.sling.api.resource.PersistenceException)114 Test (org.junit.jupiter.api.Test)72 Map (java.util.Map)70 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 LoginException (org.apache.sling.api.resource.LoginException)58 RepositoryException (javax.jcr.RepositoryException)54 Calendar (java.util.Calendar)50 Session (javax.jcr.Session)49