Search in sources :

Example 61 with Page

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

the class LastModifiedOperationImpl method withParams.

@Override
protected List<Result> withParams(final SlingHttpServletRequest request, final SlingHttpServletResponse response, final Command cmd) {
    final long start = System.currentTimeMillis();
    final List<Result> results = new ArrayList<Result>();
    final ResourceResolver resourceResolver = request.getResourceResolver();
    final PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    final List<Resource> pages = this.getLastModifiedPages(resourceResolver, cmd);
    log.debug("LastModified pages -- [ {} ] results", pages.size());
    for (final Resource resource : pages) {
        final Page page = pageManager.getContainingPage(resource);
        if (page == null) {
            continue;
        }
        final String title = TextUtil.getFirstNonEmpty(page.getTitle(), page.getPageTitle(), page.getNavigationTitle(), page.getName());
        final String description = page.getPath() + " by " + page.getLastModifiedBy() + " at " + DATE_FORMAT.format(page.getLastModified().getTime());
        results.add(new Result.Builder(title).path(page.getPath()).description(description).resultType(OpenResultSerializerImpl.TYPE).build());
    }
    log.debug("Lastmod - Execution time: {} ms", System.currentTimeMillis() - start);
    return results;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) 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) SearchResult(com.day.cq.search.result.SearchResult) Result(com.adobe.acs.commons.quickly.results.Result)

Example 62 with Page

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

the class GenericListAdapterFactoryTest method test_that_adapting_page_with_null_template_returns_null.

@Test
public void test_that_adapting_page_with_null_template_returns_null() {
    Page wrongPage = mock(Page.class);
    when(wrongPage.getProperties()).thenAnswer(new Answer<ValueMap>() {

        public ValueMap answer(InvocationOnMock invocation) throws Throwable {
            return new ValueMapDecorator(new HashMap<String, Object>());
        }
    });
    GenericList section = adaptToGenericList(wrongPage);
    assertNull(section);
}
Also used : HashMap(java.util.HashMap) GenericList(com.adobe.acs.commons.genericlists.GenericList) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Page(com.day.cq.wcm.api.Page) Test(org.junit.Test)

Example 63 with Page

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

the class GenericListAdapterFactoryTest method test_that_adapting_page_with_wrong_resourceType_returns_null.

@Test
public void test_that_adapting_page_with_wrong_resourceType_returns_null() {
    Page wrongPage = mock(Page.class);
    Resource wrongContentResource = mock(Resource.class);
    when(wrongPage.getContentResource()).thenReturn(wrongContentResource);
    when(wrongContentResource.isResourceType(GenericListImpl.RT_GENERIC_LIST)).thenReturn(false);
    GenericList section = adaptToGenericList(wrongPage);
    assertNull(section);
}
Also used : GenericList(com.adobe.acs.commons.genericlists.GenericList) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) Test(org.junit.Test)

Example 64 with Page

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

the class NamedTransformImageServlet method resolveImage.

/**
 * Intelligently determines how to find the Image based on the associated SlingRequest.
 *
 * @param request the SlingRequest Obj
 * @return the Image object configured w the info of where the image to render is stored in CRX
 */
protected final Image resolveImage(final SlingHttpServletRequest request) {
    final Resource resource = request.getResource();
    final ResourceResolver resourceResolver = request.getResourceResolver();
    final PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    final Page page = pageManager.getContainingPage(resource);
    if (DamUtil.isAsset(resource)) {
        // For assets, pick the configured rendition if it exists
        // If rendition does not exist, use original
        final Asset asset = DamUtil.resolveToAsset(resource);
        Rendition rendition = asset.getRendition(renditionPatternPicker);
        if (rendition == null) {
            log.warn("Could not find rendition [ {} ] for [ {} ]", renditionPatternPicker.toString(), resource.getPath());
            rendition = asset.getOriginal();
        }
        final Resource renditionResource = request.getResourceResolver().getResource(rendition.getPath());
        final Image image = new Image(resource);
        image.set(Image.PN_REFERENCE, renditionResource.getPath());
        return image;
    } else if (DamUtil.isRendition(resource) || resourceResolver.isResourceType(resource, JcrConstants.NT_FILE) || resourceResolver.isResourceType(resource, JcrConstants.NT_RESOURCE)) {
        // For renditions; use the requested rendition
        final Image image = new Image(resource);
        image.set(Image.PN_REFERENCE, resource.getPath());
        return image;
    } else if (page != null) {
        if (resourceResolver.isResourceType(resource, NameConstants.NT_PAGE) || StringUtils.equals(resource.getPath(), page.getContentResource().getPath())) {
            // Is a Page or Page's Content Resource; use the Page's image resource
            return new Image(page.getContentResource(), NAME_IMAGE);
        } else {
            return new Image(resource);
        }
    } else {
        if (resourceResolver.isResourceType(resource, RT_LOCAL_SOCIAL_IMAGE) && resource.getValueMap().get("mimetype", StringUtils.EMPTY).startsWith("image/")) {
            // Is a UGC image
            return new SocialImageImpl(resource, NAME_IMAGE);
        } else if (resourceResolver.isResourceType(resource, RT_REMOTE_SOCIAL_IMAGE)) {
            // Is a UGC image
            return new SocialRemoteImageImpl(resource, NAME_IMAGE);
        }
    }
    return new Image(resource);
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) Rendition(com.day.cq.dam.api.Rendition) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Asset(com.day.cq.dam.api.Asset) Page(com.day.cq.wcm.api.Page) Image(com.day.cq.wcm.foundation.Image)

Example 65 with Page

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

the class TemplateUtilTest method test_that_correct_template_returns_false.

@Test
public void test_that_correct_template_returns_false() {
    Page page = mock(Page.class);
    ValueMap properties = createTemplateValueMap(TMPL_FAKE2);
    when(page.getProperties()).thenReturn(properties);
    assertThat(TemplateUtil.hasTemplate(page, TMPL_FAKE), is(false));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Page(com.day.cq.wcm.api.Page) Test(org.junit.Test)

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