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;
}
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());
}
}
}
}
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;
}
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();
}
}
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);
}
Aggregations