use of com.day.cq.wcm.commons.ReferenceSearch in project acs-aem-commons by Adobe-Consulting-Services.
the class HttpCacheInvalidationJobConsumer method invalidateReferences.
/**
* Searches for references to the given path and invalidates them in the cache
*
* @param path the path to search for
*/
void invalidateReferences(String path) {
ResourceResolver adminResolver = null;
try {
adminResolver = resolverFactory.getServiceResourceResolver(null);
Collection<ReferenceSearch.Info> refs = new ReferenceSearch().search(adminResolver, path).values();
for (ReferenceSearch.Info info : refs) {
String refPath = info.getPage().getPath();
invalidate(refPath);
}
} catch (Exception e) {
log.debug("failed to invalidate references of {}", path);
} finally {
if (adminResolver != null) {
adminResolver.close();
}
}
}
use of com.day.cq.wcm.commons.ReferenceSearch in project acs-aem-commons by Adobe-Consulting-Services.
the class PageRelocator method movePage.
@SuppressWarnings("squid:S00112")
private void movePage(ResourceResolver rr, String sourcePage) throws Exception {
PageManager manager = pageManagerFactory.getPageManager(rr);
Field replicatorField = FieldUtils.getDeclaredField(manager.getClass(), "replicator", true);
FieldUtils.writeField(replicatorField, manager, replicatorQueue);
String destination = convertSourceToDestination(sourcePage);
String destinationParent = destination.substring(0, destination.lastIndexOf('/'));
note(sourcePage, Report.target, destination);
String beforeName = "";
final long start = System.currentTimeMillis();
String contentPath = sourcePage + "/jcr:content";
List<String> refs = new ArrayList<>();
List<String> publishRefs = new ArrayList<>();
if (maxReferences != 0 && resourceExists(rr, contentPath)) {
ReferenceSearch refSearch = new ReferenceSearch();
refSearch.setExact(true);
refSearch.setHollow(true);
refSearch.setMaxReferencesPerPage(maxReferences);
refSearch.setSearchRoot(referenceSearchRoot);
refSearch.search(rr, sourcePath).values().stream().peek(p -> refs.add(p.getPagePath())).filter(p -> isActivated(rr, p.getPagePath())).map(ReferenceSearch.Info::getPagePath).collect(Collectors.toCollection(() -> publishRefs));
}
note(sourcePage, Report.all_references, refs.size());
note(sourcePage, Report.published_references, publishRefs.size());
if (!dryRun) {
Actions.retry(10, 500, res -> {
waitUntilResourceFound(res, destinationParent);
Resource source = rr.getResource(sourcePage);
if (resourceExists(res, contentPath)) {
manager.move(source, destination, beforeName, true, true, listToStringArray(refs), listToStringArray(publishRefs));
} else {
Map<String, Object> props = new HashMap<>();
Resource parent = res.getResource(destinationParent);
res.create(parent, source.getName(), source.getValueMap());
}
res.commit();
res.refresh();
source = rr.getResource(sourcePage);
if (source != null && source.hasChildren()) {
for (Resource child : source.getChildren()) {
res.move(child.getPath(), destination);
}
res.commit();
}
}).accept(rr);
}
long end = System.currentTimeMillis();
note(sourcePage, Report.move_time, end - start);
}
Aggregations