Search in sources :

Example 41 with Page

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

the class LinkHandler method resolveRedirects.

/**
 * Attempts to resolve the redirect chain starting from the given page, avoiding loops.
 *
 * @param page The starting {@link Page}
 * @return A pair of {@link Page} and {@link String} the redirect chain resolves to. The page can be the original page, if no redirect
 * target is defined or even {@code null} if the redirect chain does not resolve to a valid page, in this case one should use the right
 * part of the pair (the {@link String} redirect target).
 */
@NotNull
public Pair<Page, String> resolveRedirects(@Nullable final Page page) {
    Page result = page;
    String redirectTarget = null;
    if (page != null && page.getPageManager() != null) {
        Set<String> redirectCandidates = new LinkedHashSet<>();
        redirectCandidates.add(page.getPath());
        while (result != null && StringUtils.isNotEmpty((redirectTarget = result.getProperties().get(PageImpl.PN_REDIRECT_TARGET, String.class)))) {
            result = page.getPageManager().getPage(redirectTarget);
            if (result != null) {
                if (!redirectCandidates.add(result.getPath())) {
                    LOGGER.warn("Detected redirect loop for the following pages: {}.", redirectCandidates);
                    break;
                }
            }
        }
    }
    return new ImmutablePair<>(result, redirectTarget);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Page(com.day.cq.wcm.api.Page) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with Page

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

the class LinkImplTest method testValidLinkWithTargetAndTargetPage.

@Test
void testValidLinkWithTargetAndTargetPage() {
    Page page = mock(Page.class);
    Link<Page> link = new LinkImpl<>(URL, URL, MockExternalizerFactory.ROOT + URL, page, new HashMap<String, String>() {

        {
            put(ATTR_TARGET, "_blank");
        }
    });
    assertValidLink(link, URL, "_blank");
    assertSame(page, link.getReference());
}
Also used : LinkImpl(com.adobe.cq.wcm.core.components.internal.link.LinkImpl) Page(com.day.cq.wcm.api.Page) Test(org.junit.jupiter.api.Test)

Example 43 with Page

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

the class LinkImplTest method testValidLikWithFilteredHtmlAttributes.

@Test
void testValidLikWithFilteredHtmlAttributes() {
    Page page = mock(Page.class);
    String invalidAttribute = "invalidAttribute";
    Link<Page> link = new LinkImpl<>(URL, URL, MockExternalizerFactory.ROOT + URL, page, ImmutableMap.of(invalidAttribute, "invalidValue"));
    assertValidLink(link, URL);
    assertNull(link.getHtmlAttributes().get(invalidAttribute));
}
Also used : LinkImpl(com.adobe.cq.wcm.core.components.internal.link.LinkImpl) Page(com.day.cq.wcm.api.Page) Test(org.junit.jupiter.api.Test)

Example 44 with Page

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

the class PageSerializerTest method serialize.

@Test
public void serialize() throws Exception {
    Page page = mock(Page.class);
    when(page.getName()).thenReturn(PAGE_NAME);
    when(page.getTitle()).thenReturn(PAGE_TITLE);
    when(page.getPageTitle()).thenReturn(PAGE_TITLE);
    when(page.getPath()).thenReturn(PAGE_PATH);
    when(page.getDescription()).thenReturn(PAGE_DESCRIPTION);
    JsonGenerator jsonGenerator = mock(JsonGenerator.class);
    SerializerProvider serializerProvider = mock(SerializerProvider.class);
    PageSerializer pageSerializer = new PageSerializer(Page.class);
    pageSerializer.serialize(page, jsonGenerator, serializerProvider);
    verify(jsonGenerator).writeStartObject();
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_NAME, page.getName());
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_TITLE, page.getTitle());
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_PAGE_TITLE, page.getPageTitle());
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_PATH, page.getPath());
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_DESCRIPTION, page.getDescription());
    verify(jsonGenerator).writeEndObject();
    verifyNoMoreInteractions(jsonGenerator);
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Page(com.day.cq.wcm.api.Page) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Test(org.junit.jupiter.api.Test)

Example 45 with Page

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

the class ComponentUtils method generateId.

/**
 * Returns an auto generated component ID.
 *
 * The ID is the first {@value ComponentUtils#ID_HASH_LENGTH} characters of an SHA-1 hexadecimal hash of the component path,
 * prefixed with the component name. Example: title-810f3af321
 *
 * If the component is referenced, the path is taken to be a concatenation of the component path,
 * with the path of the first parent context resource that exists on the page or in the template.
 * This ensures the ID is unique if the same component is referenced multiple times on the same page or template.
 *
 * Collision
 * ---------
 * c = expected collisions
 * c ~= (i^2)/(2m) - where i is the number of items and m is the number of possibilities for each item.
 * m = 16^n - for a hexadecimal string, where n is the number of characters.
 *
 * For i = 1000 components with the same name, and n = 10 characters:
 *
 * c ~= (1000^2)/(2*(16^10))
 * c ~= 0.00000045
 *
 * @return the auto generated component ID
 */
@NotNull
private static String generateId(@NotNull final Resource resource, @Nullable final Page currentPage, @Nullable final String resourceCallerPath, @Nullable final ComponentContext componentContext) {
    String resourceType = resource.getResourceType();
    String prefix = StringUtils.substringAfterLast(resourceType, "/");
    String path = resource.getPath();
    if (currentPage != null && componentContext != null) {
        PageManager pageManager = currentPage.getPageManager();
        Page resourcePage = pageManager.getContainingPage(resource);
        Template template = currentPage.getTemplate();
        boolean inCurrentPage = (resourcePage != null && StringUtils.equals(resourcePage.getPath(), currentPage.getPath()));
        boolean inTemplate = (template != null && path.startsWith(template.getPath()));
        if (resourceCallerPath != null) {
            path = resourceCallerPath.concat(resource.getPath());
        } else if (!inCurrentPage && !inTemplate) {
            ComponentContext parentContext = componentContext.getParent();
            while (parentContext != null) {
                Resource parentContextResource = parentContext.getResource();
                if (parentContextResource != null) {
                    Page parentContextPage = pageManager.getContainingPage(parentContextResource);
                    inCurrentPage = (parentContextPage != null && StringUtils.equals(parentContextPage.getPath(), currentPage.getPath()));
                    inTemplate = (template != null && parentContextResource.getPath().startsWith(template.getPath()));
                    if (inCurrentPage || inTemplate) {
                        path = parentContextResource.getPath().concat(resource.getPath());
                        break;
                    }
                }
                parentContext = parentContext.getParent();
            }
        }
    }
    return ComponentUtils.generateId(prefix, path);
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) ComponentContext(com.day.cq.wcm.api.components.ComponentContext) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) Template(com.day.cq.wcm.api.Template) NotNull(org.jetbrains.annotations.NotNull)

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