You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by re...@apache.org on 2025/01/22 17:35:57 UTC
(sling-org-apache-sling-resourceresolver) 01/01: SLING-12632: move all (only) vanity path related code in a single place
This is an automated email from the ASF dual-hosted git repository.
reschke pushed a commit to branch SLING-12632
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git
commit 0f5b313ef7addd32fc91b8aaf98fa2ee8c69ca3d
Author: Julian Reschke <re...@apache.org>
AuthorDate: Wed Jan 22 18:35:32 2025 +0100
SLING-12632: move all (only) vanity path related code in a single place
---
.../resourceresolver/impl/mapping/MapEntries.java | 1192 ++++++++++----------
1 file changed, 597 insertions(+), 595 deletions(-)
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
index a8e7827..836f639 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
@@ -290,112 +290,6 @@ public class MapEntries implements
}
}
- /**
- * Actual vanity paths initializer. Guards itself against concurrent use by
- * using a ReentrantLock. Does nothing if the resource resolver has already
- * been null-ed.
- *
- * @throws IOException in case of problems
- */
- protected void initializeVanityPaths() throws IOException {
- this.initializing.lock();
- try {
- if (this.factory.isVanityPathEnabled()) {
- this.vanityBloomFilter = createVanityBloomFilter();
- VanityPathInitializer vpi = new VanityPathInitializer(this.factory);
- if (this.factory.isVanityPathCacheInitInBackground()) {
- this.log.debug("bg init starting");
- Thread vpinit = new Thread(vpi, "VanityPathInitializer");
- vpinit.start();
- } else {
- vpi.run();
- }
- }
- } finally {
- this.initializing.unlock();
- }
- }
-
- private class VanityPathInitializer implements Runnable {
-
- private int SIZELIMIT = 10000;
-
- private MapConfigurationProvider factory;
-
- public VanityPathInitializer(MapConfigurationProvider factory) {
- this.factory = factory;
- }
-
- @Override
- public void run() {
- try {
- temporaryResolveMapsMap = Collections.synchronizedMap(new LRUMap<>(SIZELIMIT));
- execute();
- } catch (Throwable t) {
- log.error("vanity path initializer thread terminated with a throwable", t);
- }
- }
-
- private void drainQueue(List<Map.Entry<String, ResourceChange.ChangeType>> queue) {
- final AtomicBoolean resolverRefreshed = new AtomicBoolean(false);
-
- // send the change event only once
- boolean sendEvent = false;
-
- // the config needs to be reloaded only once
- final AtomicBoolean hasReloadedConfig = new AtomicBoolean(false);
-
- while (!queue.isEmpty()) {
- Map.Entry<String, ResourceChange.ChangeType> entry = queue.remove(0);
- final ResourceChange.ChangeType type = entry.getValue();
- final String path = entry.getKey();
-
- log.trace("drain type={}, path={}", type, path);
- boolean changed = handleResourceChange(type, path, resolverRefreshed, hasReloadedConfig);
-
- if (changed) {
- sendEvent = true;
- }
- }
-
- if (sendEvent) {
- sendChangeEvent();
- }
- }
-
- private void execute() {
- try (ResourceResolver resolver = factory
- .getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"))) {
-
- long initStart = System.nanoTime();
- log.debug("vanity path initialization - start");
-
- vanityTargets = loadVanityPaths(resolver);
-
- // process pending event
- drainQueue(resourceChangeQueue);
-
- vanityPathsProcessed.set(true);
-
- // drain once more in case more events have arrived
- drainQueue(resourceChangeQueue);
-
- long initElapsed = System.nanoTime() - initStart;
- long resourcesPerSecond = (vanityResourcesOnStartup.get() * TimeUnit.SECONDS.toNanos(1) / (initElapsed == 0 ? 1 : initElapsed));
-
- log.info(
- "vanity path initialization - completed, processed {} resources with sling:vanityPath properties in {}ms (~{} resource/s)",
- vanityResourcesOnStartup.get(), TimeUnit.NANOSECONDS.toMillis(initElapsed), resourcesPerSecond);
- } catch (LoginException ex) {
- log.error("Vanity path init failed", ex);
- } finally {
- log.debug("dropping temporary resolver map - {}/{} entries, {} hits, {} misses", temporaryResolveMapsMap.size(),
- SIZELIMIT, temporaryResolveMapsMapHits.get(), temporaryResolveMapsMapMisses.get());
- temporaryResolveMapsMap = null;
- }
- }
- }
-
private boolean addResource(final String path, final AtomicBoolean resolverRefreshed) {
this.initializing.lock();
@@ -540,15 +434,6 @@ public class MapEntries implements
}
}
- private boolean removeVanityPath(final String path) {
- this.initializing.lock();
- try {
- return doRemoveVanity(path);
- } finally {
- this.initializing.unlock();
- }
- }
-
/**
* Update the configuration.
* Does no locking and does not send an event at the end
@@ -568,40 +453,6 @@ public class MapEntries implements
this.mapMaps = Collections.unmodifiableSet(new TreeSet<>(newMapMaps.values()));
}
- private boolean doAddVanity(final Resource resource) {
- log.debug("doAddVanity getting {}", resource.getPath());
-
- boolean updateTheCache = isAllVanityPathEntriesCached() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries();
- return null != loadVanityPath(resource, resolveMapsMap, vanityTargets, updateTheCache, true);
- }
-
- private boolean doRemoveVanity(final String path) {
- final String actualContentPath = getActualContentPath(path);
- final List <String> l = vanityTargets.remove(actualContentPath);
- if (l != null){
- for (final String s : l){
- final List<MapEntry> entries = this.resolveMapsMap.get(s);
- if (entries!= null) {
- for (final Iterator<MapEntry> iterator =entries.iterator(); iterator.hasNext(); ) {
- final MapEntry entry = iterator.next();
- final String redirect = getMapEntryRedirect(entry);
- if (redirect != null && redirect.equals(actualContentPath)) {
- iterator.remove();
- }
- }
- }
- if (entries!= null && entries.isEmpty()) {
- this.resolveMapsMap.remove(s);
- }
- }
- if (vanityCounter.longValue() > 0) {
- vanityCounter.addAndGet(-2);
- }
- return true;
- }
- return false;
- }
-
private boolean doAddAlias(final Resource resource) {
return loadAlias(resource, this.aliasMapsMap, null, null);
}
@@ -746,68 +597,6 @@ public class MapEntries implements
// special singleton entry for negative cache entries
private static final List<MapEntry> NO_MAP_ENTRIES = Collections.emptyList();
- /**
- * get the MapEntry list containing all the nodes having a specific vanityPath
- */
- private List<MapEntry> getMapEntryList(String vanityPath) {
- List<MapEntry> mapEntries = null;
-
- boolean initFinished = vanityPathsProcessed.get();
- boolean probablyPresent = false;
-
- if (initFinished) {
- // total number of lookups after init (and when cache not complete)
- long current = this.vanityPathLookups.incrementAndGet();
- if (current >= Long.MAX_VALUE - 100000) {
- // reset counters when we get close the limit
- this.vanityPathLookups.set(1);
- this.vanityPathBloomNegatives.set(0);
- this.vanityPathBloomFalsePositives.set(0);
- log.info("Vanity Path metrics reset to 0");
- }
-
- // init is done - check the bloom filter
- probablyPresent = BloomFilterUtils.probablyContains(vanityBloomFilter, vanityPath);
- log.trace("bloom filter lookup for {} -> {}", vanityPath, probablyPresent);
-
- if (!probablyPresent) {
- // filtered by Bloom filter
- this.vanityPathBloomNegatives.incrementAndGet();
- }
- }
-
- if (!initFinished || probablyPresent) {
-
- mapEntries = this.resolveMapsMap.get(vanityPath);
-
- if (mapEntries == null) {
- if (!initFinished && temporaryResolveMapsMap != null) {
- mapEntries = temporaryResolveMapsMap.get(vanityPath);
- if (mapEntries != null) {
- temporaryResolveMapsMapHits.incrementAndGet();
- log.trace("getMapEntryList: using temp map entries for {} -> {}", vanityPath, mapEntries);
- } else {
- temporaryResolveMapsMapMisses.incrementAndGet();
- }
- }
- if (mapEntries == null) {
- Map<String, List<MapEntry>> mapEntry = getVanityPaths(vanityPath);
- mapEntries = mapEntry.get(vanityPath);
- if (!initFinished && temporaryResolveMapsMap != null) {
- log.trace("getMapEntryList: caching map entries for {} -> {}", vanityPath, mapEntries);
- temporaryResolveMapsMap.put(vanityPath, mapEntries == null ? NO_MAP_ENTRIES : mapEntries);
- }
- }
- }
- if (mapEntries == null && probablyPresent) {
- // Bloom filter had a false positive
- this.vanityPathBloomFalsePositives.incrementAndGet();
- }
- }
-
- return mapEntries == NO_MAP_ENTRIES ? null : mapEntries;
- }
-
/**
* Refresh the resource resolver if not already done
* @param resolverRefreshed Boolean flag containing the state if the resolver
@@ -857,189 +646,46 @@ public class MapEntries implements
// ---------- ResourceChangeListener interface
- /**
- * Handles the change to any of the node properties relevant for vanity URL
- * mappings. The {@link #MapEntries(MapConfigurationProvider, BundleContext, EventAdmin, StringInterpolationProvider, Optional)}
- * constructor makes sure the event listener is registered to only get
- * appropriate events.
- */
- @Override
- public void onChange(final List<ResourceChange> changes) {
+ private boolean handleResourceChange(ResourceChange.ChangeType type, String path, AtomicBoolean resolverRefreshed,
+ AtomicBoolean hasReloadedConfig) {
+ boolean changed = false;
- final boolean inStartup = !vanityPathsProcessed.get();
+ // removal of a resource is handled differently
+ if (type == ResourceChange.ChangeType.REMOVED) {
+ final Boolean result = handleConfigurationUpdate(path, hasReloadedConfig, resolverRefreshed, true);
+ if (result != null) {
+ if (result) {
+ changed = true;
+ } else {
+ changed |= removeResource(path, resolverRefreshed);
+ }
+ }
+ // session.move() is handled differently see also SLING-3713 and
+ } else if (type == ResourceChange.ChangeType.ADDED) {
+ final Boolean result = handleConfigurationUpdate(path, hasReloadedConfig, resolverRefreshed, false);
+ if (result != null) {
+ if (result) {
+ changed = true;
+ } else {
+ changed |= addResource(path, resolverRefreshed);
+ }
+ }
+ } else if (type == ResourceChange.ChangeType.CHANGED) {
+ final Boolean result = handleConfigurationUpdate(path, hasReloadedConfig, resolverRefreshed, false);
+ if (result != null) {
+ if (result) {
+ changed = true;
+ } else {
+ changed |= updateResource(path, resolverRefreshed);
+ }
+ }
+ }
- final AtomicBoolean resolverRefreshed = new AtomicBoolean(false);
-
- // send the change event only once
- boolean sendEvent = false;
-
- // the config needs to be reloaded only once
- final AtomicBoolean hasReloadedConfig = new AtomicBoolean(false);
-
- for (final ResourceChange rc : changes) {
-
- final ResourceChange.ChangeType type = rc.getType();
- final String path = rc.getPath();
-
- log.debug("onChange, type={}, path={}", rc.getType(), path);
-
- // don't care for system area
- if (path.startsWith(JCR_SYSTEM_PREFIX)) {
- continue;
- }
-
- // during startup: just enqueue the events
- if (inStartup) {
- if (type == ResourceChange.ChangeType.REMOVED || type == ResourceChange.ChangeType.ADDED
- || type == ResourceChange.ChangeType.CHANGED) {
- Map.Entry<String, ResourceChange.ChangeType> entry = new SimpleEntry<>(path, type);
- log.trace("enqueue: {}", entry);
- resourceChangeQueue.add(entry);
- }
- } else {
- boolean changed = handleResourceChange(type, path, resolverRefreshed, hasReloadedConfig);
-
- if (changed) {
- sendEvent = true;
- }
- }
- }
-
- if (sendEvent) {
- this.sendChangeEvent();
- }
- }
-
- private boolean handleResourceChange(ResourceChange.ChangeType type, String path, AtomicBoolean resolverRefreshed,
- AtomicBoolean hasReloadedConfig) {
- boolean changed = false;
-
- // removal of a resource is handled differently
- if (type == ResourceChange.ChangeType.REMOVED) {
- final Boolean result = handleConfigurationUpdate(path, hasReloadedConfig, resolverRefreshed, true);
- if (result != null) {
- if (result) {
- changed = true;
- } else {
- changed |= removeResource(path, resolverRefreshed);
- }
- }
- // session.move() is handled differently see also SLING-3713 and
- } else if (type == ResourceChange.ChangeType.ADDED) {
- final Boolean result = handleConfigurationUpdate(path, hasReloadedConfig, resolverRefreshed, false);
- if (result != null) {
- if (result) {
- changed = true;
- } else {
- changed |= addResource(path, resolverRefreshed);
- }
- }
- } else if (type == ResourceChange.ChangeType.CHANGED) {
- final Boolean result = handleConfigurationUpdate(path, hasReloadedConfig, resolverRefreshed, false);
- if (result != null) {
- if (result) {
- changed = true;
- } else {
- changed |= updateResource(path, resolverRefreshed);
- }
- }
- }
-
- return changed;
- }
+ return changed;
+ }
// ---------- internal
- private byte[] createVanityBloomFilter() throws IOException {
- return BloomFilterUtils.createFilter(VANITY_BLOOM_FILTER_MAX_ENTRIES, this.factory.getVanityBloomFilterMaxBytes());
- }
-
- private boolean isAllVanityPathEntriesCached() {
- return this.factory.getMaxCachedVanityPathEntries() == -1;
- }
-
- /**
- * get the vanity paths Search for all nodes having a specific vanityPath
- */
- private Map<String, List<MapEntry>> getVanityPaths(String vanityPath) {
-
- Map<String, List<MapEntry>> entryMap = new HashMap<>();
-
- final String queryString = String.format(
- "SELECT [sling:vanityPath], [sling:redirect], [sling:redirectStatus] FROM [nt:base] "
- + "WHERE %s AND ([sling:vanityPath]='%s' OR [sling:vanityPath]='%s') "
- + "ORDER BY [sling:vanityOrder] DESC",
- QueryBuildHelper.excludeSystemPath(),
- QueryBuildHelper.escapeString(vanityPath),
- QueryBuildHelper.escapeString(vanityPath.substring(1)));
-
- try (ResourceResolver queryResolver = factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"));) {
- long totalCount = 0;
- long totalValid = 0;
- log.debug("start vanityPath query: {}", queryString);
- final Iterator<Resource> i = queryResolver.findResources(queryString, "JCR-SQL2");
- log.debug("end vanityPath query");
- while (i.hasNext()) {
- totalCount += 1;
- 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 ) {
- totalValid += 1;
- if (this.vanityPathsProcessed.get() && (this.factory.isMaxCachedVanityPathEntriesStartup() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries())) {
- loadVanityPath(resource, resolveMapsMap, vanityTargets, true, true);
- entryMap = resolveMapsMap;
- } else {
- final Map <String, List<String>> targetPaths = new HashMap<>();
- loadVanityPath(resource, entryMap, targetPaths, true, false);
- }
- }
- }
- log.debug("read {} ({} valid) vanityPaths", totalCount, totalValid);
- } catch (LoginException e) {
- log.error("Exception while obtaining queryResolver", e);
- }
- return entryMap;
- }
-
- /**
- * Check if the path is a valid vanity path
- * @param path The resource path to check
- * @return {@code true} if this is valid, {@code false} otherwise
- */
- private boolean isValidVanityPath(final String path){
- if (path == null) {
- throw new IllegalArgumentException("Unexpected null path");
- }
-
- // ignore system tree
- if (path.startsWith(JCR_SYSTEM_PREFIX)) {
- log.debug("isValidVanityPath: not valid {}", path);
- return false;
- }
-
- // check allow/deny list
- if ( this.factory.getVanityPathConfig() != null ) {
- boolean allowed = false;
- for(final VanityPathConfig config : this.factory.getVanityPathConfig()) {
- if ( path.startsWith(config.prefix) ) {
- allowed = !config.isExclude;
- break;
- }
- }
- if ( !allowed ) {
- log.debug("isValidVanityPath: not valid as not in allow list {}", path);
- return false;
- }
- }
- return true;
- }
-
private String getActualContentPath(final String path){
final String checkPath;
if ( path.endsWith(JCR_CONTENT_SUFFIX) ) {
@@ -1365,76 +1011,574 @@ public class MapEntries implements
return it;
}
- /**
- * Load vanity paths - search for all nodes (except under /jcr:system)
- * having a sling:vanityPath property
- */
- private Map<String, List<String>> loadVanityPaths(ResourceResolver resolver) {
- final Map<String, List<String>> targetPaths = new ConcurrentHashMap<>();
- final String baseQueryString = "SELECT [sling:vanityPath], [sling:redirect], [sling:redirectStatus]" + " FROM [nt:base]"
- + " WHERE " + QueryBuildHelper.excludeSystemPath() + " AND [sling:vanityPath] IS NOT NULL";
-
- Iterator<Resource> it;
- try {
- final String queryStringWithSort = baseQueryString + " AND FIRST([sling:vanityPath]) >= '%s' ORDER BY FIRST([sling:vanityPath])";
- it = new PagedQueryIterator("vanity path", PROP_VANITY_PATH, resolver, queryStringWithSort, 2000);
- } catch (QuerySyntaxException ex) {
- log.debug("sort with first() not supported, falling back to base query", ex);
- it = queryUnpaged("vanity path", baseQueryString);
- } catch (UnsupportedOperationException ex) {
- log.debug("query failed as unsupported, retrying without paging/sorting", ex);
- it = queryUnpaged("vanity path", baseQueryString);
- }
-
- long count = 0;
- long countInScope = 0;
- long processStart = System.nanoTime();
-
- while (it.hasNext()) {
- count += 1;
- final Resource resource = it.next();
- final String resourcePath = resource.getPath();
- if (Stream.of(this.factory.getObservationPaths()).anyMatch(path -> path.matches(resourcePath))) {
- countInScope += 1;
- final boolean addToCache = isAllVanityPathEntriesCached()
- || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries();
- loadVanityPath(resource, resolveMapsMap, targetPaths, addToCache, true);
- }
+ private void updateTargetPaths(final Map<String, List<String>> targetPaths, final String key, final String entry) {
+ if (entry == null) {
+ return;
}
- long processElapsed = System.nanoTime() - processStart;
- log.debug("processed {} resources with sling:vanityPath properties (of which {} in scope) in {}ms", count, countInScope, TimeUnit.NANOSECONDS.toMillis(processElapsed));
- if (!isAllVanityPathEntriesCached()) {
- if (countInScope > this.factory.getMaxCachedVanityPathEntries()) {
- log.warn("Number of resources with sling:vanityPath property ({}) exceeds configured cache size ({}); handling of uncached vanity paths will be much slower. Consider increasing the cache size or decreasing the number of vanity paths.", countInScope, this.factory.getMaxCachedVanityPathEntries());
- } else if (countInScope > (this.factory.getMaxCachedVanityPathEntries() / 10) * 9) {
- log.info("Number of resources with sling:vanityPath property in scope ({}) within 10% of configured cache size ({})", countInScope, this.factory.getMaxCachedVanityPathEntries());
- }
+ List<String> entries = targetPaths.get(key);
+ if (entries == null) {
+ entries = new ArrayList<>();
+ targetPaths.put(key, entries);
}
-
- this.vanityResourcesOnStartup.set(count);
-
- return targetPaths;
+ entries.add(entry);
}
- /**
- * Load vanity path given a resource
- *
- * @return first vanity path or {@code null}
- */
- private String loadVanityPath(final Resource resource, final Map<String, List<MapEntry>> entryMap, final Map <String, List<String>> targetPaths, boolean addToCache, boolean updateCounter) {
-
- if (!isValidVanityPath(resource.getPath())) {
- return null;
+ private void loadConfiguration(final MapConfigurationProvider factory, final List<MapEntry> entries) {
+ // virtual uris
+ final Map<String, String> virtuals = factory.getVirtualURLMap();
+ if (virtuals != null) {
+ for (final Entry<String, String> virtualEntry : virtuals.entrySet()) {
+ final String extPath = virtualEntry.getKey();
+ final String intPath = virtualEntry.getValue();
+ if (!extPath.equals(intPath)) {
+ // this regular expression must match the whole URL !!
+ final String url = "^" + ANY_SCHEME_HOST + extPath + "$";
+ final String redirect = intPath;
+ MapEntry mapEntry = getMapEntry(url, -1, redirect);
+ if (mapEntry!=null){
+ entries.add(mapEntry);
+ }
+ }
+ }
}
- final ValueMap props = resource.getValueMap();
- long vanityOrder = props.get(PROP_VANITY_ORDER, 0L);
-
- // url is ignoring scheme and host.port and the path is
- // what is stored in the sling:vanityPath property
- boolean hasVanityPath = false;
- final String[] pVanityPaths = props.get(PROP_VANITY_PATH, new String[0]);
- if (log.isTraceEnabled()) {
+ // URL Mappings
+ final Mapping[] mappings = factory.getMappings();
+ if (mappings != null) {
+ final Map<String, List<String>> map = new HashMap<>();
+ for (final Mapping mapping : mappings) {
+ if (mapping.mapsInbound()) {
+ final String url = mapping.getTo();
+ final String alias = mapping.getFrom();
+ if (url.length() > 0) {
+ List<String> aliasList = map.get(url);
+ if (aliasList == null) {
+ aliasList = new ArrayList<>();
+ map.put(url, aliasList);
+ }
+ aliasList.add(alias);
+ }
+ }
+ }
+
+ for (final Entry<String, List<String>> entry : map.entrySet()) {
+ MapEntry mapEntry = getMapEntry(ANY_SCHEME_HOST + entry.getKey(), -1, entry.getValue().toArray(new String[0]));
+ if (mapEntry!=null){
+ entries.add(mapEntry);
+ }
+ }
+ }
+ }
+
+ private void loadMapConfiguration(final MapConfigurationProvider factory, final Map<String, MapEntry> entries) {
+ // URL Mappings
+ final Mapping[] mappings = factory.getMappings();
+ if (mappings != null) {
+ for (int i = mappings.length - 1; i >= 0; i--) {
+ final Mapping mapping = mappings[i];
+ if (mapping.mapsOutbound()) {
+ final String url = mapping.getTo();
+ final String alias = mapping.getFrom();
+ if (!url.equals(alias)) {
+ addMapEntry(entries, alias, url, -1);
+ }
+ }
+ }
+ }
+
+ // virtual uris
+ final Map<String, String> virtuals = factory.getVirtualURLMap();
+ if (virtuals != null) {
+ for (final Entry<String, String> virtualEntry : virtuals.entrySet()) {
+ final String extPath = virtualEntry.getKey();
+ final String intPath = virtualEntry.getValue();
+ if (!extPath.equals(intPath)) {
+ // this regular expression must match the whole URL !!
+ final String path = "^" + intPath + "$";
+ final String url = extPath;
+ addMapEntry(entries, path, url, -1);
+ }
+ }
+ }
+ }
+
+ private void addMapEntry(final Map<String, MapEntry> entries, final String path, final String url, final int status) {
+ MapEntry entry = entries.get(path);
+ if (entry == null) {
+ entry = getMapEntry(path, status, url);
+ } else {
+ final String[] redir = entry.getRedirect();
+ final String[] newRedir = new String[redir.length + 1];
+ System.arraycopy(redir, 0, newRedir, 0, redir.length);
+ newRedir[redir.length] = url;
+ entry = getMapEntry(entry.getPattern(), entry.getStatus(), newRedir);
+ }
+ if (entry!=null){
+ entries.put(path, entry);
+ }
+ }
+
+ private final AtomicLong lastTimeLogged = new AtomicLong(-1);
+
+ private final long LOGGING_ERROR_PERIOD = 1000 * 60 * 5;
+
+ @Override
+ public void logDisableAliasOptimization() {
+ this.logDisableAliasOptimization(null);
+ }
+
+ private void logDisableAliasOptimization(final Exception e) {
+ if ( e != null ) {
+ log.error("Unexpected problem during initialization of optimize alias resolution. Therefore disabling optimize alias resolution. Please fix the problem.", e);
+ } else {
+ final long now = System.currentTimeMillis();
+ if ( now - lastTimeLogged.getAndSet(now) > LOGGING_ERROR_PERIOD) {
+ log.error("A problem occured during initialization of optimize alias resolution. Optimize alias resolution is disabled. Check the logs for the reported problem.", e);
+ }
+ }
+ }
+
+ private MapEntry getMapEntry(final String url, final int status, final String... redirect) {
+ return getMapEntry(url, status, 0, redirect);
+ }
+
+ private MapEntry getMapEntry(final String url, final int status, long order,
+ final String... redirect) {
+ try {
+ return new MapEntry(url, status, false, order, redirect);
+ } catch (IllegalArgumentException iae) {
+ // ignore this entry
+ log.debug("ignored entry for {} due to exception", url, iae);
+ return null;
+ }
+ }
+
+ // ---- vanity path handling ----
+
+ /**
+ * Actual vanity paths initializer. Guards itself against concurrent use by
+ * using a ReentrantLock. Does nothing if the resource resolver has already
+ * been null-ed.
+ *
+ * @throws IOException in case of problems
+ */
+ protected void initializeVanityPaths() throws IOException {
+ this.initializing.lock();
+ try {
+ if (this.factory.isVanityPathEnabled()) {
+ this.vanityBloomFilter = createVanityBloomFilter();
+ VanityPathInitializer vpi = new VanityPathInitializer(this.factory);
+ if (this.factory.isVanityPathCacheInitInBackground()) {
+ this.log.debug("bg init starting");
+ Thread vpinit = new Thread(vpi, "VanityPathInitializer");
+ vpinit.start();
+ } else {
+ vpi.run();
+ }
+ }
+ } finally {
+ this.initializing.unlock();
+ }
+ }
+
+ private boolean removeVanityPath(final String path) {
+ this.initializing.lock();
+ try {
+ return doRemoveVanity(path);
+ } finally {
+ this.initializing.unlock();
+ }
+ }
+
+ private class VanityPathInitializer implements Runnable {
+
+ private int SIZELIMIT = 10000;
+
+ private MapConfigurationProvider factory;
+
+ public VanityPathInitializer(MapConfigurationProvider factory) {
+ this.factory = factory;
+ }
+
+ @Override
+ public void run() {
+ try {
+ temporaryResolveMapsMap = Collections.synchronizedMap(new LRUMap<>(SIZELIMIT));
+ execute();
+ } catch (Throwable t) {
+ log.error("vanity path initializer thread terminated with a throwable", t);
+ }
+ }
+
+ private void drainQueue(List<Map.Entry<String, ResourceChange.ChangeType>> queue) {
+ final AtomicBoolean resolverRefreshed = new AtomicBoolean(false);
+
+ // send the change event only once
+ boolean sendEvent = false;
+
+ // the config needs to be reloaded only once
+ final AtomicBoolean hasReloadedConfig = new AtomicBoolean(false);
+
+ while (!queue.isEmpty()) {
+ Map.Entry<String, ResourceChange.ChangeType> entry = queue.remove(0);
+ final ResourceChange.ChangeType type = entry.getValue();
+ final String path = entry.getKey();
+
+ log.trace("drain type={}, path={}", type, path);
+ boolean changed = handleResourceChange(type, path, resolverRefreshed, hasReloadedConfig);
+
+ if (changed) {
+ sendEvent = true;
+ }
+ }
+
+ if (sendEvent) {
+ sendChangeEvent();
+ }
+ }
+
+ private void execute() {
+ try (ResourceResolver resolver = factory
+ .getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"))) {
+
+ long initStart = System.nanoTime();
+ log.debug("vanity path initialization - start");
+
+ vanityTargets = loadVanityPaths(resolver);
+
+ // process pending event
+ drainQueue(resourceChangeQueue);
+
+ vanityPathsProcessed.set(true);
+
+ // drain once more in case more events have arrived
+ drainQueue(resourceChangeQueue);
+
+ long initElapsed = System.nanoTime() - initStart;
+ long resourcesPerSecond = (vanityResourcesOnStartup.get() * TimeUnit.SECONDS.toNanos(1) / (initElapsed == 0 ? 1 : initElapsed));
+
+ log.info(
+ "vanity path initialization - completed, processed {} resources with sling:vanityPath properties in {}ms (~{} resource/s)",
+ vanityResourcesOnStartup.get(), TimeUnit.NANOSECONDS.toMillis(initElapsed), resourcesPerSecond);
+ } catch (LoginException ex) {
+ log.error("Vanity path init failed", ex);
+ } finally {
+ log.debug("dropping temporary resolver map - {}/{} entries, {} hits, {} misses", temporaryResolveMapsMap.size(),
+ SIZELIMIT, temporaryResolveMapsMapHits.get(), temporaryResolveMapsMapMisses.get());
+ temporaryResolveMapsMap = null;
+ }
+ }
+ }
+
+ private boolean doAddVanity(final Resource resource) {
+ log.debug("doAddVanity getting {}", resource.getPath());
+
+ boolean updateTheCache = isAllVanityPathEntriesCached() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries();
+ return null != loadVanityPath(resource, resolveMapsMap, vanityTargets, updateTheCache, true);
+ }
+
+ private boolean doRemoveVanity(final String path) {
+ final String actualContentPath = getActualContentPath(path);
+ final List <String> l = vanityTargets.remove(actualContentPath);
+ if (l != null){
+ for (final String s : l){
+ final List<MapEntry> entries = this.resolveMapsMap.get(s);
+ if (entries!= null) {
+ for (final Iterator<MapEntry> iterator =entries.iterator(); iterator.hasNext(); ) {
+ final MapEntry entry = iterator.next();
+ final String redirect = getMapEntryRedirect(entry);
+ if (redirect != null && redirect.equals(actualContentPath)) {
+ iterator.remove();
+ }
+ }
+ }
+ if (entries!= null && entries.isEmpty()) {
+ this.resolveMapsMap.remove(s);
+ }
+ }
+ if (vanityCounter.longValue() > 0) {
+ vanityCounter.addAndGet(-2);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * get the MapEntry list containing all the nodes having a specific vanityPath
+ */
+ private List<MapEntry> getMapEntryList(String vanityPath) {
+ List<MapEntry> mapEntries = null;
+
+ boolean initFinished = vanityPathsProcessed.get();
+ boolean probablyPresent = false;
+
+ if (initFinished) {
+ // total number of lookups after init (and when cache not complete)
+ long current = this.vanityPathLookups.incrementAndGet();
+ if (current >= Long.MAX_VALUE - 100000) {
+ // reset counters when we get close the limit
+ this.vanityPathLookups.set(1);
+ this.vanityPathBloomNegatives.set(0);
+ this.vanityPathBloomFalsePositives.set(0);
+ log.info("Vanity Path metrics reset to 0");
+ }
+
+ // init is done - check the bloom filter
+ probablyPresent = BloomFilterUtils.probablyContains(vanityBloomFilter, vanityPath);
+ log.trace("bloom filter lookup for {} -> {}", vanityPath, probablyPresent);
+
+ if (!probablyPresent) {
+ // filtered by Bloom filter
+ this.vanityPathBloomNegatives.incrementAndGet();
+ }
+ }
+
+ if (!initFinished || probablyPresent) {
+
+ mapEntries = this.resolveMapsMap.get(vanityPath);
+
+ if (mapEntries == null) {
+ if (!initFinished && temporaryResolveMapsMap != null) {
+ mapEntries = temporaryResolveMapsMap.get(vanityPath);
+ if (mapEntries != null) {
+ temporaryResolveMapsMapHits.incrementAndGet();
+ log.trace("getMapEntryList: using temp map entries for {} -> {}", vanityPath, mapEntries);
+ } else {
+ temporaryResolveMapsMapMisses.incrementAndGet();
+ }
+ }
+ if (mapEntries == null) {
+ Map<String, List<MapEntry>> mapEntry = getVanityPaths(vanityPath);
+ mapEntries = mapEntry.get(vanityPath);
+ if (!initFinished && temporaryResolveMapsMap != null) {
+ log.trace("getMapEntryList: caching map entries for {} -> {}", vanityPath, mapEntries);
+ temporaryResolveMapsMap.put(vanityPath, mapEntries == null ? NO_MAP_ENTRIES : mapEntries);
+ }
+ }
+ }
+ if (mapEntries == null && probablyPresent) {
+ // Bloom filter had a false positive
+ this.vanityPathBloomFalsePositives.incrementAndGet();
+ }
+ }
+
+ return mapEntries == NO_MAP_ENTRIES ? null : mapEntries;
+ }
+
+ /**
+ * Handles the change to any of the node properties relevant for vanity URL
+ * mappings. The {@link #MapEntries(MapConfigurationProvider, BundleContext, EventAdmin, StringInterpolationProvider, Optional)}
+ * constructor makes sure the event listener is registered to only get
+ * appropriate events.
+ */
+ @Override
+ public void onChange(final List<ResourceChange> changes) {
+
+ final boolean inStartup = !vanityPathsProcessed.get();
+
+ final AtomicBoolean resolverRefreshed = new AtomicBoolean(false);
+
+ // send the change event only once
+ boolean sendEvent = false;
+
+ // the config needs to be reloaded only once
+ final AtomicBoolean hasReloadedConfig = new AtomicBoolean(false);
+
+ for (final ResourceChange rc : changes) {
+
+ final ResourceChange.ChangeType type = rc.getType();
+ final String path = rc.getPath();
+
+ log.debug("onChange, type={}, path={}", rc.getType(), path);
+
+ // don't care for system area
+ if (path.startsWith(JCR_SYSTEM_PREFIX)) {
+ continue;
+ }
+
+ // during startup: just enqueue the events
+ if (inStartup) {
+ if (type == ResourceChange.ChangeType.REMOVED || type == ResourceChange.ChangeType.ADDED
+ || type == ResourceChange.ChangeType.CHANGED) {
+ Map.Entry<String, ResourceChange.ChangeType> entry = new SimpleEntry<>(path, type);
+ log.trace("enqueue: {}", entry);
+ resourceChangeQueue.add(entry);
+ }
+ } else {
+ boolean changed = handleResourceChange(type, path, resolverRefreshed, hasReloadedConfig);
+
+ if (changed) {
+ sendEvent = true;
+ }
+ }
+ }
+
+ if (sendEvent) {
+ this.sendChangeEvent();
+ }
+ }
+
+ private byte[] createVanityBloomFilter() throws IOException {
+ return BloomFilterUtils.createFilter(VANITY_BLOOM_FILTER_MAX_ENTRIES, this.factory.getVanityBloomFilterMaxBytes());
+ }
+
+ private boolean isAllVanityPathEntriesCached() {
+ return this.factory.getMaxCachedVanityPathEntries() == -1;
+ }
+
+ /**
+ * get the vanity paths Search for all nodes having a specific vanityPath
+ */
+ private Map<String, List<MapEntry>> getVanityPaths(String vanityPath) {
+
+ Map<String, List<MapEntry>> entryMap = new HashMap<>();
+
+ final String queryString = String.format(
+ "SELECT [sling:vanityPath], [sling:redirect], [sling:redirectStatus] FROM [nt:base] "
+ + "WHERE %s AND ([sling:vanityPath]='%s' OR [sling:vanityPath]='%s') "
+ + "ORDER BY [sling:vanityOrder] DESC",
+ QueryBuildHelper.excludeSystemPath(),
+ QueryBuildHelper.escapeString(vanityPath),
+ QueryBuildHelper.escapeString(vanityPath.substring(1)));
+
+ try (ResourceResolver queryResolver = factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"));) {
+ long totalCount = 0;
+ long totalValid = 0;
+ log.debug("start vanityPath query: {}", queryString);
+ final Iterator<Resource> i = queryResolver.findResources(queryString, "JCR-SQL2");
+ log.debug("end vanityPath query");
+ while (i.hasNext()) {
+ totalCount += 1;
+ 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 ) {
+ totalValid += 1;
+ if (this.vanityPathsProcessed.get() && (this.factory.isMaxCachedVanityPathEntriesStartup() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries())) {
+ loadVanityPath(resource, resolveMapsMap, vanityTargets, true, true);
+ entryMap = resolveMapsMap;
+ } else {
+ final Map <String, List<String>> targetPaths = new HashMap<>();
+ loadVanityPath(resource, entryMap, targetPaths, true, false);
+ }
+ }
+ }
+ log.debug("read {} ({} valid) vanityPaths", totalCount, totalValid);
+ } catch (LoginException e) {
+ log.error("Exception while obtaining queryResolver", e);
+ }
+ return entryMap;
+ }
+
+ /**
+ * Check if the path is a valid vanity path
+ * @param path The resource path to check
+ * @return {@code true} if this is valid, {@code false} otherwise
+ */
+ private boolean isValidVanityPath(final String path){
+ if (path == null) {
+ throw new IllegalArgumentException("Unexpected null path");
+ }
+
+ // ignore system tree
+ if (path.startsWith(JCR_SYSTEM_PREFIX)) {
+ log.debug("isValidVanityPath: not valid {}", path);
+ return false;
+ }
+
+ // check allow/deny list
+ if ( this.factory.getVanityPathConfig() != null ) {
+ boolean allowed = false;
+ for(final VanityPathConfig config : this.factory.getVanityPathConfig()) {
+ if ( path.startsWith(config.prefix) ) {
+ allowed = !config.isExclude;
+ break;
+ }
+ }
+ if ( !allowed ) {
+ log.debug("isValidVanityPath: not valid as not in allow list {}", path);
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Load vanity paths - search for all nodes (except under /jcr:system)
+ * having a sling:vanityPath property
+ */
+ private Map<String, List<String>> loadVanityPaths(ResourceResolver resolver) {
+ final Map<String, List<String>> targetPaths = new ConcurrentHashMap<>();
+ final String baseQueryString = "SELECT [sling:vanityPath], [sling:redirect], [sling:redirectStatus]" + " FROM [nt:base]"
+ + " WHERE " + QueryBuildHelper.excludeSystemPath() + " AND [sling:vanityPath] IS NOT NULL";
+
+ Iterator<Resource> it;
+ try {
+ final String queryStringWithSort = baseQueryString + " AND FIRST([sling:vanityPath]) >= '%s' ORDER BY FIRST([sling:vanityPath])";
+ it = new PagedQueryIterator("vanity path", PROP_VANITY_PATH, resolver, queryStringWithSort, 2000);
+ } catch (QuerySyntaxException ex) {
+ log.debug("sort with first() not supported, falling back to base query", ex);
+ it = queryUnpaged("vanity path", baseQueryString);
+ } catch (UnsupportedOperationException ex) {
+ log.debug("query failed as unsupported, retrying without paging/sorting", ex);
+ it = queryUnpaged("vanity path", baseQueryString);
+ }
+
+ long count = 0;
+ long countInScope = 0;
+ long processStart = System.nanoTime();
+
+ while (it.hasNext()) {
+ count += 1;
+ final Resource resource = it.next();
+ final String resourcePath = resource.getPath();
+ if (Stream.of(this.factory.getObservationPaths()).anyMatch(path -> path.matches(resourcePath))) {
+ countInScope += 1;
+ final boolean addToCache = isAllVanityPathEntriesCached()
+ || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries();
+ loadVanityPath(resource, resolveMapsMap, targetPaths, addToCache, true);
+ }
+ }
+ long processElapsed = System.nanoTime() - processStart;
+ log.debug("processed {} resources with sling:vanityPath properties (of which {} in scope) in {}ms", count, countInScope, TimeUnit.NANOSECONDS.toMillis(processElapsed));
+ if (!isAllVanityPathEntriesCached()) {
+ if (countInScope > this.factory.getMaxCachedVanityPathEntries()) {
+ log.warn("Number of resources with sling:vanityPath property ({}) exceeds configured cache size ({}); handling of uncached vanity paths will be much slower. Consider increasing the cache size or decreasing the number of vanity paths.", countInScope, this.factory.getMaxCachedVanityPathEntries());
+ } else if (countInScope > (this.factory.getMaxCachedVanityPathEntries() / 10) * 9) {
+ log.info("Number of resources with sling:vanityPath property in scope ({}) within 10% of configured cache size ({})", countInScope, this.factory.getMaxCachedVanityPathEntries());
+ }
+ }
+
+ this.vanityResourcesOnStartup.set(count);
+
+ return targetPaths;
+ }
+
+ /**
+ * Load vanity path given a resource
+ *
+ * @return first vanity path or {@code null}
+ */
+ private String loadVanityPath(final Resource resource, final Map<String, List<MapEntry>> entryMap, final Map <String, List<String>> targetPaths, boolean addToCache, boolean updateCounter) {
+
+ if (!isValidVanityPath(resource.getPath())) {
+ return null;
+ }
+
+ final ValueMap props = resource.getValueMap();
+ long vanityOrder = props.get(PROP_VANITY_ORDER, 0L);
+
+ // url is ignoring scheme and host.port and the path is
+ // what is stored in the sling:vanityPath property
+ boolean hasVanityPath = false;
+ final String[] pVanityPaths = props.get(PROP_VANITY_PATH, new String[0]);
+ if (log.isTraceEnabled()) {
log.trace("vanity paths on {}: {}", resource.getPath(), Arrays.asList(pVanityPaths));
}
@@ -1509,18 +1653,6 @@ public class MapEntries implements
return hasVanityPath ? pVanityPaths[0] : null;
}
- private void updateTargetPaths(final Map<String, List<String>> targetPaths, final String key, final String entry) {
- if (entry == null) {
- return;
- }
- List<String> entries = targetPaths.get(key);
- if (entries == null) {
- entries = new ArrayList<>();
- targetPaths.put(key, entries);
- }
- entries.add(entry);
- }
-
/**
* Create the vanity path definition. String array containing:
* {protocol}/{host}[.port] {absolute path}
@@ -1573,121 +1705,6 @@ public class MapEntries implements
return new String[] { prefix, path };
}
- private void loadConfiguration(final MapConfigurationProvider factory, final List<MapEntry> entries) {
- // virtual uris
- final Map<String, String> virtuals = factory.getVirtualURLMap();
- if (virtuals != null) {
- for (final Entry<String, String> virtualEntry : virtuals.entrySet()) {
- final String extPath = virtualEntry.getKey();
- final String intPath = virtualEntry.getValue();
- if (!extPath.equals(intPath)) {
- // this regular expression must match the whole URL !!
- final String url = "^" + ANY_SCHEME_HOST + extPath + "$";
- final String redirect = intPath;
- MapEntry mapEntry = getMapEntry(url, -1, redirect);
- if (mapEntry!=null){
- entries.add(mapEntry);
- }
- }
- }
- }
-
- // URL Mappings
- final Mapping[] mappings = factory.getMappings();
- if (mappings != null) {
- final Map<String, List<String>> map = new HashMap<>();
- for (final Mapping mapping : mappings) {
- if (mapping.mapsInbound()) {
- final String url = mapping.getTo();
- final String alias = mapping.getFrom();
- if (url.length() > 0) {
- List<String> aliasList = map.get(url);
- if (aliasList == null) {
- aliasList = new ArrayList<>();
- map.put(url, aliasList);
- }
- aliasList.add(alias);
- }
- }
- }
-
- for (final Entry<String, List<String>> entry : map.entrySet()) {
- MapEntry mapEntry = getMapEntry(ANY_SCHEME_HOST + entry.getKey(), -1, entry.getValue().toArray(new String[0]));
- if (mapEntry!=null){
- entries.add(mapEntry);
- }
- }
- }
- }
-
- private void loadMapConfiguration(final MapConfigurationProvider factory, final Map<String, MapEntry> entries) {
- // URL Mappings
- final Mapping[] mappings = factory.getMappings();
- if (mappings != null) {
- for (int i = mappings.length - 1; i >= 0; i--) {
- final Mapping mapping = mappings[i];
- if (mapping.mapsOutbound()) {
- final String url = mapping.getTo();
- final String alias = mapping.getFrom();
- if (!url.equals(alias)) {
- addMapEntry(entries, alias, url, -1);
- }
- }
- }
- }
-
- // virtual uris
- final Map<String, String> virtuals = factory.getVirtualURLMap();
- if (virtuals != null) {
- for (final Entry<String, String> virtualEntry : virtuals.entrySet()) {
- final String extPath = virtualEntry.getKey();
- final String intPath = virtualEntry.getValue();
- if (!extPath.equals(intPath)) {
- // this regular expression must match the whole URL !!
- final String path = "^" + intPath + "$";
- final String url = extPath;
- addMapEntry(entries, path, url, -1);
- }
- }
- }
- }
-
- private void addMapEntry(final Map<String, MapEntry> entries, final String path, final String url, final int status) {
- MapEntry entry = entries.get(path);
- if (entry == null) {
- entry = getMapEntry(path, status, url);
- } else {
- final String[] redir = entry.getRedirect();
- final String[] newRedir = new String[redir.length + 1];
- System.arraycopy(redir, 0, newRedir, 0, redir.length);
- newRedir[redir.length] = url;
- entry = getMapEntry(entry.getPattern(), entry.getStatus(), newRedir);
- }
- if (entry!=null){
- entries.put(path, entry);
- }
- }
-
- private final AtomicLong lastTimeLogged = new AtomicLong(-1);
-
- private final long LOGGING_ERROR_PERIOD = 1000 * 60 * 5;
-
- @Override
- public void logDisableAliasOptimization() {
- this.logDisableAliasOptimization(null);
- }
-
- private void logDisableAliasOptimization(final Exception e) {
- if ( e != null ) {
- log.error("Unexpected problem during initialization of optimize alias resolution. Therefore disabling optimize alias resolution. Please fix the problem.", e);
- } else {
- final long now = System.currentTimeMillis();
- if ( now - lastTimeLogged.getAndSet(now) > LOGGING_ERROR_PERIOD) {
- log.error("A problem occured during initialization of optimize alias resolution. Optimize alias resolution is disabled. Check the logs for the reported problem.", e);
- }
- }
- }
-
// return vanity path entry iterator from cache when complete and ready, otherwise from
// regular lockup
public @Nullable Iterator<MapEntry> getCurrentMapEntryForVanityPath(final String key) {
@@ -1699,19 +1716,4 @@ public class MapEntries implements
}
return l == null ? null : l.iterator();
}
-
- private MapEntry getMapEntry(final String url, final int status, final String... redirect) {
- return getMapEntry(url, status, 0, redirect);
- }
-
- private MapEntry getMapEntry(final String url, final int status, long order,
- final String... redirect) {
- try {
- return new MapEntry(url, status, false, order, redirect);
- } catch (IllegalArgumentException iae) {
- // ignore this entry
- log.debug("ignored entry for {} due to exception", url, iae);
- return null;
- }
- }
}