Search in sources :

Example 1 with PageManager

use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class WCMUsePojoBaseTest method getResourceBackedBindings.

/**
     * <p>
     * Creates a {@link Bindings} map initialised with the following default bindings available to Sightly use objects based on {@link
     * WCMUsePojo}:
     * </p>
     * <ul>
     * <li>{@link SlingBindings#RESOURCE}</li>
     * <li>{@link SlingBindings#REQUEST}</li>
     * <li>{@link SlingBindings#RESPONSE}</li>
     * <li>{@link WCMBindings#PROPERTIES}</li>
     * <li>{@link WCMBindings#WCM_MODE}</li>
     * <li>{@link WCMBindings#PAGE_MANAGER}</li>
     * <li>{@link WCMBindings#RESOURCE_PAGE}</li>
     * <li>{@link WCMBindings#CURRENT_PAGE}</li>
     * <li>{@link WCMBindings#PAGE_PROPERTIES}</li>
     * </ul>
     *
     * @param resourcePath the path to a resource already loaded in the testing context
     * @return the bindings map
     */
protected Bindings getResourceBackedBindings(String resourcePath) {
    Bindings bindings = getDefaultSlingBindings();
    Resource resource = context.resourceResolver().getResource(resourcePath);
    if (resource != null) {
        ValueMap properties = resource.adaptTo(ValueMap.class);
        bindings.put(SlingBindings.RESOURCE, resource);
        bindings.put(WCMBindings.PROPERTIES, properties);
        bindings.put(WCMBindings.WCM_MODE, new SightlyWCMMode(context.request()));
        PageManager pageManager = context.pageManager();
        bindings.put(WCMBindings.PAGE_MANAGER, pageManager);
        context.request().setResource(resource);
        Page resourcePage = pageManager.getContainingPage(resource);
        if (resourcePage != null) {
            bindings.put(WCMBindings.RESOURCE_PAGE, resourcePage);
            bindings.put(WCMBindings.CURRENT_PAGE, resourcePage);
            bindings.put(WCMBindings.PAGE_PROPERTIES, properties);
        }
    } else {
        throw new IllegalArgumentException("Cannot find a resource at " + resourcePath);
    }
    return bindings;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) SightlyWCMMode(com.adobe.cq.sightly.SightlyWCMMode) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) WCMBindings(com.adobe.cq.sightly.WCMBindings)

Example 2 with PageManager

use of com.day.cq.wcm.api.PageManager in project CQ-Actions by Cognifide.

the class HandleMessageJob method process.

@Override
public JobResult process(Job job) {
    final String path = (String) job.getProperty(SlingConstants.PROPERTY_PATH);
    ResourceResolver resolver = null;
    try {
        resolver = resolverFactory.getAdministrativeResourceResolver(null);
        final PageManager pm = resolver.adaptTo(PageManager.class);
        final Page page = pm.getPage(path);
        final String actionType;
        if (page != null && page.getContentResource() != null) {
            actionType = page.getContentResource().getResourceType();
            consumeMessage(actionType, page.getContentResource());
        } else {
            LOG.debug("Empty resource type for action page: " + path);
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    } finally {
        if (resolver != null) {
            resolver.close();
        }
    }
    return JobResult.OK;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Page(com.day.cq.wcm.api.Page)

Example 3 with PageManager

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

the class AemObjectInjector method getResourcePage.

private Page getResourcePage(Object adaptable) {
    PageManager pageManager = getPageManager(adaptable);
    Resource resource = getResource(adaptable);
    if (pageManager != null && resource != null) {
        return pageManager.getContainingPage(resource);
    }
    return null;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) Resource(org.apache.sling.api.resource.Resource)

Example 4 with PageManager

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

the class WorkflowPackageManagerImpl method create.

/**
 * {@inheritDoc}
 */
public final Page create(final ResourceResolver resourceResolver, String bucketSegment, final String name, final String... paths) throws WCMException, RepositoryException {
    final Session session = resourceResolver.adaptTo(Session.class);
    final PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    String bucketPath = "/etc/workflow/packages";
    if (StringUtils.isNotBlank(bucketSegment)) {
        bucketPath += "/" + bucketSegment;
    }
    final Node shardNode = JcrUtils.getOrCreateByPath(bucketPath, NT_SLING_FOLDER, NT_SLING_FOLDER, session, false);
    final Page page = pageManager.create(shardNode.getPath(), JcrUtil.createValidName(name), WORKFLOW_PACKAGE_TEMPLATE, name, false);
    final Resource contentResource = page.getContentResource();
    Node node = JcrUtil.createPath(contentResource.getPath() + "/" + NN_VLT_DEFINITION, NT_VLT_DEFINITION, session);
    node = JcrUtil.createPath(node.getPath() + "/filter", JcrConstants.NT_UNSTRUCTURED, session);
    JcrUtil.setProperty(node, SLING_RESOURCE_TYPE, FILTER_RESOURCE_TYPE);
    int i = 0;
    Node resourceNode = null;
    for (final String path : paths) {
        if (path != null) {
            resourceNode = JcrUtil.createPath(node.getPath() + "/resource_" + i++, JcrConstants.NT_UNSTRUCTURED, session);
            JcrUtil.setProperty(resourceNode, "root", path);
            JcrUtil.setProperty(resourceNode, "rules", this.getIncludeRules(path));
            JcrUtil.setProperty(resourceNode, SLING_RESOURCE_TYPE, FILTER_RESOURCE_RESOURCE_TYPE);
        }
    }
    session.save();
    return page;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) Session(javax.jcr.Session)

Example 5 with PageManager

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

the class TwitterFeedUpdaterImpl method updateTwitterFeedComponents.

@Override
@SuppressWarnings("squid:S3776")
public void updateTwitterFeedComponents(ResourceResolver resourceResolver) {
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    List<Resource> twitterResources = findTwitterResources(resourceResolver);
    for (Resource twitterResource : twitterResources) {
        Page page = pageManager.getContainingPage(twitterResource);
        if (page != null) {
            Twitter client = page.adaptTo(Twitter.class);
            if (client != null) {
                try {
                    ValueMap properties = twitterResource.getValueMap();
                    String username = properties.get("username", String.class);
                    if (!StringUtils.isEmpty(username)) {
                        log.info("Loading Twitter timeline for user {} for component {}.", username, twitterResource.getPath());
                        List<Status> statuses = client.getUserTimeline(username);
                        if (statuses != null) {
                            List<String> tweetsList = new ArrayList<>(statuses.size());
                            List<String> jsonList = new ArrayList<>(statuses.size());
                            for (Status status : statuses) {
                                tweetsList.add(processTweet(status));
                                jsonList.add(DataObjectFactory.getRawJSON(status));
                            }
                            if (!tweetsList.isEmpty()) {
                                ModifiableValueMap map = twitterResource.adaptTo(ModifiableValueMap.class);
                                map.put("tweets", tweetsList.toArray(new String[tweetsList.size()]));
                                map.put("tweetsJson", jsonList.toArray(new String[jsonList.size()]));
                                twitterResource.getResourceResolver().commit();
                                handleReplication(twitterResource);
                            }
                        }
                    }
                } catch (PersistenceException e) {
                    log.error("Exception while updating twitter feed on resource:" + twitterResource.getPath(), e);
                } catch (ReplicationException e) {
                    log.error("Exception while replicating twitter feed on resource:" + twitterResource.getPath(), e);
                } catch (TwitterException e) {
                    log.error("Exception while loading twitter feed on resource:" + twitterResource.getPath(), e);
                }
            } else {
                log.warn("Twitter component found on {}, but page cannot be adapted to Twitter API. Check Cloud SErvice configuration", page.getPath());
            }
        }
    }
}
Also used : Status(twitter4j.Status) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) Page(com.day.cq.wcm.api.Page) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) PageManager(com.day.cq.wcm.api.PageManager) PersistenceException(org.apache.sling.api.resource.PersistenceException) ReplicationException(com.day.cq.replication.ReplicationException) TwitterException(twitter4j.TwitterException)

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