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