Search in sources :

Example 6 with PageManager

use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.

the class DispatcherFlusherServlet method doPost.

@Override
@SuppressWarnings("squid:S3776")
protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    final Resource resource = request.getResource();
    final ResourceResolver resourceResolver = request.getResourceResolver();
    final PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    final Page currentPage = pageManager.getContainingPage(resource);
    final ValueMap properties = resource.getValueMap();
    /* Properties */
    final String[] paths = properties.get("paths", new String[0]);
    final ReplicationActionType replicationActionType = ReplicationActionType.valueOf(properties.get("replicationActionType", ReplicationActionType.ACTIVATE.name()));
    final List<FlushResult> overallResults = new ArrayList<FlushResult>();
    boolean caughtException = false;
    ResourceResolver flushingResourceResolver = null;
    try {
        if (paths.length > 0) {
            if (flushWithAdminResourceResolver) {
                // Use the admin resource resolver for replication to ensure all
                // replication permission checks are OK
                // Make sure to close this resource resolver
                flushingResourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO);
            } else {
                // Use the HTTP Request's resource resolver; don't close this resource resolver
                flushingResourceResolver = resourceResolver;
            }
            final Map<Agent, ReplicationResult> results = dispatcherFlusher.flush(flushingResourceResolver, replicationActionType, true, paths);
            for (final Map.Entry<Agent, ReplicationResult> entry : results.entrySet()) {
                final Agent agent = entry.getKey();
                final ReplicationResult result = entry.getValue();
                overallResults.add(new FlushResult(agent, result));
            }
        }
    } catch (ReplicationException e) {
        log.error("Replication exception occurred during Dispatcher Flush request.", e);
        caughtException = true;
    } catch (LoginException e) {
        log.error("Could not obtain an Admin Resource Resolver during Dispatcher Flush request.", e);
        caughtException = true;
    } finally {
        if (flushWithAdminResourceResolver && flushingResourceResolver != null) {
            // Close the admin resource resolver if opened by this servlet
            flushingResourceResolver.close();
        }
    }
    if (request.getRequestPathInfo().getExtension().equals("json")) {
        response.setContentType("application/json");
        JSONWriter writer = new JSONWriter(response.getWriter());
        try {
            writer.object();
            for (final FlushResult result : overallResults) {
                writer.key(result.agentId);
                writer.value(result.success);
            }
            writer.endObject();
        } catch (JSONException e) {
            throw new ServletException("Unable to output JSON data", e);
        }
    } else {
        String suffix;
        if (caughtException) {
            suffix = "replication-error";
        } else {
            suffix = StringUtils.join(overallResults, '/');
        }
        response.sendRedirect(request.getContextPath() + currentPage.getPath() + ".html/" + suffix);
    }
}
Also used : JSONWriter(org.apache.sling.commons.json.io.JSONWriter) Agent(com.day.cq.replication.Agent) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) JSONException(org.apache.sling.commons.json.JSONException) Page(com.day.cq.wcm.api.Page) ServletException(javax.servlet.ServletException) ReplicationResult(com.day.cq.replication.ReplicationResult) PageManager(com.day.cq.wcm.api.PageManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) ReplicationException(com.day.cq.replication.ReplicationException) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map) ReplicationActionType(com.day.cq.replication.ReplicationActionType)

Example 7 with PageManager

use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.

the class PagesReferenceProvider method findReferences.

@Override
public List<Reference> findReferences(Resource resource) {
    List<Reference> references = new ArrayList<Reference>();
    ResourceResolver resolver = resource.getResourceResolver();
    PageManager pageManager = resolver.adaptTo(PageManager.class);
    Set<Page> pages = new HashSet<Page>();
    search(resource, pages, pageManager);
    for (Page page : pages) {
        Resource contentResource = page.getContentResource();
        if (contentResource != null && !contentResource.getPath().equals(resource.getPath())) {
            references.add(getReference(page));
        }
    }
    return references;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) Reference(com.day.cq.wcm.api.reference.Reference) ArrayList(java.util.ArrayList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) HashSet(java.util.HashSet)

Example 8 with PageManager

use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.

the class SiteMapServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType(request.getResponseContentType());
    if (StringUtils.isNotEmpty(this.characterEncoding)) {
        response.setCharacterEncoding(characterEncoding);
    }
    ResourceResolver resourceResolver = request.getResourceResolver();
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    Page page = pageManager.getContainingPage(request.getResource());
    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    try {
        XMLStreamWriter stream = outputFactory.createXMLStreamWriter(response.getWriter());
        stream.writeStartDocument("1.0");
        stream.writeStartElement("", "urlset", NS);
        stream.writeNamespace("", NS);
        // first do the current page
        write(page, stream, resourceResolver);
        for (Iterator<Page> children = page.listChildren(new PageFilter(false, true), true); children.hasNext(); ) {
            write(children.next(), stream, resourceResolver);
        }
        if (damAssetTypes.size() > 0 && damAssetProperty.length() > 0) {
            for (Resource assetFolder : getAssetFolders(page, resourceResolver)) {
                writeAssets(stream, assetFolder, resourceResolver);
            }
        }
        stream.writeEndElement();
        stream.writeEndDocument();
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) PageFilter(com.day.cq.wcm.api.PageFilter) IOException(java.io.IOException)

Example 9 with PageManager

use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.

the class SystemNotificationsImpl method isActiveNotification.

private boolean isActiveNotification(final SlingHttpServletRequest request, final Resource resource) {
    if (JcrConstants.JCR_CONTENT.equals(resource.getName()) || REP_POLICY.equals(resource.getName())) {
        return false;
    }
    final PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
    final Page notificationPage = pageManager.getContainingPage(resource);
    if (notificationPage == null) {
        log.warn("Trying to get a invalid System Notification page at [ {} ]", resource.getPath());
        return false;
    } else if (this.isDismissed(request, notificationPage)) {
        // System Notification previously dismissed by the user
        return false;
    }
    // Looks like a valid Notification Page; now check if the properties are valid
    final ValueMap properties = notificationPage.getProperties();
    final boolean enabled = properties.get(PN_ENABLED, false);
    if (!enabled) {
        // Disabled
        return false;
    } else {
        final Calendar onTime = properties.get(PN_ON_TIME, Calendar.class);
        final Calendar offTime = properties.get(PN_OFF_TIME, Calendar.class);
        if (onTime == null && offTime == null) {
            // No on time or off time is set, but is enabled so always show
            return true;
        }
        final Calendar now = Calendar.getInstance();
        if (onTime != null && now.before(onTime)) {
            return false;
        }
        if (offTime != null && now.after(offTime)) {
            return false;
        }
        return true;
    }
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) ValueMap(org.apache.sling.api.resource.ValueMap) Calendar(java.util.Calendar) Page(com.day.cq.wcm.api.Page)

Example 10 with PageManager

use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.

the class WorkflowHelperImpl method getPageOrAssetResource.

/**
 * @{inheritDoc}
 */
@Override
public final Resource getPageOrAssetResource(ResourceResolver resourceResolver, String path) {
    Resource payloadResource = resourceResolver.getResource(path);
    if (payloadResource == null) {
        return null;
    }
    Asset asset = DamUtil.resolveToAsset(payloadResource);
    if (asset != null) {
        return asset.adaptTo(Resource.class);
    }
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    Page page = pageManager.getContainingPage(payloadResource);
    if (page != null) {
        return page.adaptTo(Resource.class);
    }
    return null;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) Resource(org.apache.sling.api.resource.Resource) Asset(com.day.cq.dam.api.Asset) Page(com.day.cq.wcm.api.Page)

Aggregations

PageManager (com.day.cq.wcm.api.PageManager)36 Page (com.day.cq.wcm.api.Page)31 Resource (org.apache.sling.api.resource.Resource)23 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)14 ArrayList (java.util.ArrayList)10 ValueMap (org.apache.sling.api.resource.ValueMap)8 HashMap (java.util.HashMap)7 Map (java.util.Map)5 Asset (com.day.cq.dam.api.Asset)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Pattern (java.util.regex.Pattern)3 PostConstruct (javax.annotation.PostConstruct)3 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)2 Image (com.adobe.cq.wcm.core.components.models.Image)2 Rendition (com.day.cq.dam.api.Rendition)2 ReplicationException (com.day.cq.replication.ReplicationException)2 NameConstants (com.day.cq.wcm.api.NameConstants)2 Template (com.day.cq.wcm.api.Template)2 Reference (com.day.cq.wcm.api.reference.Reference)2