Search in sources :

Example 11 with Page

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

the class AemObjectInjector method getCurrentDesign.

/**
 * Get the current design.
 *
 * @param adaptable a SlingHttpServletRequest
 * @return the current Design if the adaptable was a SlingHttpServletRequest, the default Design otherwise
 */
private Design getCurrentDesign(Object adaptable) {
    Page currentPage = getCurrentPage(adaptable);
    Designer designer = getDesigner(adaptable);
    if (currentPage != null && designer != null) {
        return designer.getDesign(currentPage);
    }
    return null;
}
Also used : Designer(com.day.cq.wcm.api.designer.Designer) Page(com.day.cq.wcm.api.Page)

Example 12 with Page

use of com.day.cq.wcm.api.Page 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 13 with Page

use of com.day.cq.wcm.api.Page 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)

Example 14 with Page

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

the class TwitterAdapterFactoryTest method setupPage.

private Page setupPage(ValueMap configData) {
    com.day.cq.wcm.webservicesupport.Configuration config = setupConfiguration(configData);
    String[] configPath = new String[] { "configpath " };
    Page page = mock(Page.class);
    Resource contentResource = mock(Resource.class);
    when(page.getContentResource()).thenReturn(contentResource);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    when(contentResource.getResourceResolver()).thenReturn(resourceResolver);
    ConfigurationManager configurationManager = mock(ConfigurationManager.class);
    when(resourceResolver.adaptTo(ConfigurationManager.class)).thenReturn(configurationManager);
    when(contentResource.getValueMap()).thenReturn(new ValueMapDecorator(Collections.singletonMap(ConfigurationConstants.PN_CONFIGURATIONS, configPath)));
    when(configurationManager.getConfiguration("twitterconnect", configPath)).thenReturn(config);
    return page;
}
Also used : Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ValueMapDecorator(com.adobe.cq.commerce.common.ValueMapDecorator) Page(com.day.cq.wcm.api.Page) ConfigurationManager(com.day.cq.wcm.webservicesupport.ConfigurationManager)

Example 15 with Page

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

the class SharedComponentPropertiesBindingsValuesProviderTest method setUp.

@Before
public void setUp() throws Exception {
    resource = mock(Resource.class);
    pageRootProvider = mock(PageRootProvider.class);
    page = mock(Page.class);
    bindings = new SimpleBindings();
    component = mock(Component.class);
    sharedPropsResource = mock(Resource.class);
    globalPropsResource = mock(Resource.class);
    resourceResolver = mock(ResourceResolver.class);
    componentManager = mock(ComponentManager.class);
    String globalPropsPath = SITE_ROOT + "/jcr:content/" + SharedComponentProperties.NN_GLOBAL_COMPONENT_PROPERTIES;
    String sharedPropsPath = SITE_ROOT + "/jcr:content/" + SharedComponentProperties.NN_SHARED_COMPONENT_PROPERTIES + "/" + RESOURCE_TYPE;
    bindings.put("resource", resource);
    when(resource.getResourceResolver()).thenReturn(resourceResolver);
    when(resourceResolver.getResource(sharedPropsPath)).thenReturn(sharedPropsResource);
    when(resourceResolver.getResource(globalPropsPath)).thenReturn(globalPropsResource);
    when(resourceResolver.adaptTo(ComponentManager.class)).thenReturn(componentManager);
    when(componentManager.getComponentOfResource(resource)).thenReturn(component);
    when(page.getPath()).thenReturn(SITE_ROOT);
    when(pageRootProvider.getRootPage(resource)).thenReturn(page);
    when(component.getResourceType()).thenReturn(RESOURCE_TYPE);
    when(sharedPropsResource.getName()).thenReturn("Shared Properties Resource");
    when(globalPropsResource.getName()).thenReturn("Global Properties Resource");
    sharedProps = new ValueMapDecorator(new HashMap<String, Object>());
    globalProps = new ValueMapDecorator(new HashMap<String, Object>());
    sharedProps.put("shared", "value");
    globalProps.put("global", "value");
    when(globalPropsResource.getValueMap()).thenReturn(globalProps);
    when(sharedPropsResource.getValueMap()).thenReturn(sharedProps);
}
Also used : HashMap(java.util.HashMap) SimpleBindings(javax.script.SimpleBindings) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ComponentManager(com.day.cq.wcm.api.components.ComponentManager) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Page(com.day.cq.wcm.api.Page) Component(com.day.cq.wcm.api.components.Component) PageRootProvider(com.adobe.acs.commons.wcm.PageRootProvider) Before(org.junit.Before)

Aggregations

Page (com.day.cq.wcm.api.Page)100 Resource (org.apache.sling.api.resource.Resource)45 PageManager (com.day.cq.wcm.api.PageManager)34 Test (org.junit.jupiter.api.Test)22 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)18 ValueMap (org.apache.sling.api.resource.ValueMap)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)12 SlingBindings (org.apache.sling.api.scripting.SlingBindings)12 NotNull (org.jetbrains.annotations.NotNull)10 Test (org.junit.Test)9 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Optional (java.util.Optional)6 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)6 Nullable (org.jetbrains.annotations.Nullable)6 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)5 Template (com.day.cq.wcm.api.Template)5 StringUtils (org.apache.commons.lang3.StringUtils)5 Before (org.junit.Before)5