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();
}
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);
}
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);
}
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());
}
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);
}
Aggregations