Search in sources :

Example 86 with Page

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

the class LocalizationUtils method getLocalPage.

/**
 * Given the current requested page and a reference page, this method will determine a page belonging to the
 * current site and locale that can be used instead of the reference site.
 *
 * Specifically, if the reference page and the current page are both found under a language root, and that language
 * root is not the same, then the returned page is the page located under the current page's language root at the
 * same relative path as the reference page is located under it's own language root; or empty if that page does not
 * exist.
 *
 * If either the reference page or the current page are not located under a language root, or if they share the
 * same language root, and if the reference page has a live relationship where the target is the current page or
 * an ancestor of the current page, then the target of that live relationship is returned; or empty if that page
 * does not exist.
 *
 * All other conditions return empty.
 *
 * @param referencePage The referenced page.
 * @param currentPage The current page.
 * @param resourceResolver A resource resolver.
 * @param languageManager The language manager service.
 * @param relationshipManager The live relationship manager service.
 * @return A page, that belongs to the same language or live copy as the current page, and can be used as the local
 * alternative to the referenced page, or empty if no such page exists.
 */
public static Optional<Page> getLocalPage(@NotNull final Page referencePage, @NotNull final Page currentPage, @NotNull final ResourceResolver resourceResolver, @NotNull final LanguageManager languageManager, @NotNull final LiveRelationshipManager relationshipManager) {
    Page referencePageLanguageRoot = Optional.ofNullable(referencePage.getPath()).map(resourceResolver::getResource).map(languageManager::getLanguageRoot).orElse(null);
    Page currentPageLanguageRoot = languageManager.getLanguageRoot(currentPage.getContentResource());
    if (referencePageLanguageRoot != null && currentPageLanguageRoot != null && !referencePageLanguageRoot.equals(currentPageLanguageRoot)) {
        // check if there's a language copy of the navigation root
        return Optional.ofNullable(referencePage.getPageManager().getPage(ResourceUtil.normalize(String.join("/", currentPageLanguageRoot.getPath(), getRelativePath(referencePageLanguageRoot, referencePage)))));
    } else {
        try {
            String currentPagePath = currentPage.getPath() + "/";
            return Optional.of(Optional.ofNullable((Iterator<LiveRelationship>) relationshipManager.getLiveRelationships(referencePage.adaptTo(Resource.class), null, null)).map(liveRelationshipIterator -> StreamSupport.stream(((Iterable<LiveRelationship>) () -> liveRelationshipIterator).spliterator(), false)).orElseGet(Stream::empty).map(LiveRelationship::getTargetPath).filter(target -> currentPagePath.startsWith(target + "/")).map(referencePage.getPageManager()::getPage).findFirst().orElse(referencePage));
        } catch (WCMException e) {
        // ignore it
        }
    }
    return Optional.empty();
}
Also used : LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ResourceUtil(org.apache.sling.api.resource.ResourceUtil) Iterator(java.util.Iterator) Resource(org.apache.sling.api.resource.Resource) LanguageManager(com.day.cq.wcm.api.LanguageManager) Page(com.day.cq.wcm.api.Page) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) Nullable(org.jetbrains.annotations.Nullable) WCMException(com.day.cq.wcm.api.WCMException) Stream(java.util.stream.Stream) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) NotNull(org.jetbrains.annotations.NotNull) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) Stream(java.util.stream.Stream) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) WCMException(com.day.cq.wcm.api.WCMException)

Example 87 with Page

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

the class LinkHandler method resolvePage.

/**
 * Attempts to resolve a Link URL and page for the given page. Redirect chains are followed, if
 * shadowing is not disabled.
 *
 * @param page Page
 * @return A pair of {@link String} and {@link Page} the page resolves to.
 */
@NotNull
private Pair<Page, String> resolvePage(@Nullable final Page page) {
    Page resolved = page;
    String redirectTarget = null;
    String linkURL = null;
    if (!isShadowingDisabled()) {
        Pair<Page, String> pair = resolveRedirects(page);
        resolved = pair.getLeft();
        redirectTarget = pair.getRight();
    }
    if (resolved == null) {
        if (StringUtils.isNotEmpty(redirectTarget)) {
            return new ImmutablePair<>(page, redirectTarget);
        } else {
            resolved = page;
        }
    }
    if (resolved != null) {
        linkURL = getPageLinkURL(resolved);
    }
    return new ImmutablePair<>(resolved, linkURL);
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Page(com.day.cq.wcm.api.Page) NotNull(org.jetbrains.annotations.NotNull)

Example 88 with Page

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

the class TabsImplTest method createTabsResource.

private Resource createTabsResource(Object... properties) {
    Map<String, Object> props = new HashMap<>();
    props.put(PROPERTY_RESOURCE_TYPE, TabsImpl.RESOURCE_TYPE);
    props.putAll(MapUtil.toMap(properties));
    Page testPage = context.pageManager().getPage(TEST_ROOT_PAGE);
    return context.create().resource(testPage, "tabs-test", props);
}
Also used : HashMap(java.util.HashMap) Page(com.day.cq.wcm.api.Page)

Example 89 with Page

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

the class ClientLibrariesImplTest method testGetCategories.

@Test
void testGetCategories() {
    PageManager pageManager = context.pageManager();
    Page page = pageManager.getPage(ROOT_PAGE);
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(ClientLibraries.OPTION_RESOURCE_TYPES, Utils.getPageResourceTypes(page, context.request(), mock(ModelFactory.class)));
    ClientLibrariesImpl clientlibs = Objects.requireNonNull((ClientLibrariesImpl) getClientLibrariesUnderTest(ROOT_PAGE, attributes));
    Set<String> categories = new HashSet<>();
    categories.add(TEASER_CATEGORY);
    categories.add(ACCORDION_CATEGORY);
    categories.add(CAROUSEL_CATEGORY);
    assertEquals(categories, clientlibs.getCategoriesFromComponents());
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) HashMap(java.util.HashMap) Page(com.day.cq.wcm.api.Page) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 90 with Page

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

the class ClientLibrariesImplTest method testUtilsGetTemplateResourceTypes.

@Test
void testUtilsGetTemplateResourceTypes() {
    Page page = context.currentPage(PAGE_WITH_TEMPLATE);
    ModelFactory modelFactory = mock(ModelFactory.class);
    Set<String> resourceTypes = Utils.getTemplateResourceTypes(page, context.request(), modelFactory);
    Set<String> expectedResourceTypes = new HashSet<>(Arrays.asList("core/wcm/components/page/v2/page", "wcm/foundation/components/responsivegrid", "core/wcm/components/text/v1/text"));
    assertEquals(expectedResourceTypes, resourceTypes);
}
Also used : Page(com.day.cq.wcm.api.Page) ModelFactory(org.apache.sling.models.factory.ModelFactory) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.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