Search in sources :

Example 1 with ReferenceSearch

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();
        }
    }
}
Also used : ReferenceSearch(com.day.cq.wcm.commons.ReferenceSearch) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) HttpCacheException(com.adobe.acs.commons.httpcache.exception.HttpCacheException)

Example 2 with ReferenceSearch

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);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) FieldUtils(org.apache.commons.lang.reflect.FieldUtils) ProcessDefinition(com.adobe.acs.commons.mcp.ProcessDefinition) ReplicationActionType(com.day.cq.replication.ReplicationActionType) HashMap(java.util.HashMap) SimpleFilteringResourceVisitor(com.adobe.acs.commons.util.visitors.SimpleFilteringResourceVisitor) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ReplicationStatus(com.day.cq.replication.ReplicationStatus) ManagedProcess(com.adobe.acs.commons.mcp.model.ManagedProcess) RepositoryException(javax.jcr.RepositoryException) ReplicationException(com.day.cq.replication.ReplicationException) Map(java.util.Map) ReferenceSearch(com.day.cq.wcm.commons.ReferenceSearch) FormField(com.adobe.acs.commons.mcp.form.FormField) PersistenceException(org.apache.sling.api.resource.PersistenceException) ReplicationOptions(com.day.cq.replication.ReplicationOptions) Actions(com.adobe.acs.commons.fam.actions.Actions) PathfieldComponent(com.adobe.acs.commons.mcp.form.PathfieldComponent) EnumMap(java.util.EnumMap) Session(javax.jcr.Session) Replicator(com.day.cq.replication.Replicator) Resource(org.apache.sling.api.resource.Resource) RadioComponent(com.adobe.acs.commons.mcp.form.RadioComponent) PageManagerFactory(com.day.cq.wcm.api.PageManagerFactory) ActionManager(com.adobe.acs.commons.fam.ActionManager) Field(java.lang.reflect.Field) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) PageManager(com.day.cq.wcm.api.PageManager) LoginException(org.apache.sling.api.resource.LoginException) List(java.util.List) GenericReport(com.adobe.acs.commons.mcp.model.GenericReport) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) TreeFilteringResourceVisitor(com.adobe.acs.commons.util.visitors.TreeFilteringResourceVisitor) NameConstants(com.day.cq.wcm.api.NameConstants) TextfieldComponent(com.adobe.acs.commons.mcp.form.TextfieldComponent) AccessControlManager(javax.jcr.security.AccessControlManager) CheckboxComponent(com.adobe.acs.commons.mcp.form.CheckboxComponent) Pattern(java.util.regex.Pattern) Privilege(javax.jcr.security.Privilege) ProcessInstance(com.adobe.acs.commons.mcp.ProcessInstance) FormField(com.adobe.acs.commons.mcp.form.FormField) Field(java.lang.reflect.Field) PageManager(com.day.cq.wcm.api.PageManager) ReferenceSearch(com.day.cq.wcm.commons.ReferenceSearch) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap)

Aggregations

ReferenceSearch (com.day.cq.wcm.commons.ReferenceSearch)2 ActionManager (com.adobe.acs.commons.fam.ActionManager)1 Actions (com.adobe.acs.commons.fam.actions.Actions)1 HttpCacheException (com.adobe.acs.commons.httpcache.exception.HttpCacheException)1 ProcessDefinition (com.adobe.acs.commons.mcp.ProcessDefinition)1 ProcessInstance (com.adobe.acs.commons.mcp.ProcessInstance)1 CheckboxComponent (com.adobe.acs.commons.mcp.form.CheckboxComponent)1 FormField (com.adobe.acs.commons.mcp.form.FormField)1 PathfieldComponent (com.adobe.acs.commons.mcp.form.PathfieldComponent)1 RadioComponent (com.adobe.acs.commons.mcp.form.RadioComponent)1 TextfieldComponent (com.adobe.acs.commons.mcp.form.TextfieldComponent)1 GenericReport (com.adobe.acs.commons.mcp.model.GenericReport)1 ManagedProcess (com.adobe.acs.commons.mcp.model.ManagedProcess)1 SimpleFilteringResourceVisitor (com.adobe.acs.commons.util.visitors.SimpleFilteringResourceVisitor)1 TreeFilteringResourceVisitor (com.adobe.acs.commons.util.visitors.TreeFilteringResourceVisitor)1 ReplicationActionType (com.day.cq.replication.ReplicationActionType)1 ReplicationException (com.day.cq.replication.ReplicationException)1 ReplicationOptions (com.day.cq.replication.ReplicationOptions)1 ReplicationStatus (com.day.cq.replication.ReplicationStatus)1 Replicator (com.day.cq.replication.Replicator)1