Search in sources :

Example 1 with ComponentContext

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

the class ComponentImplTest method getReferencedComponentUnderTest.

private Component getReferencedComponentUnderTest(String resourcePath, String currentPagePath, String referencerPath, Object... properties) {
    Resource resource = context.currentResource(resourcePath);
    if (resource != null && properties != null) {
        context.contentPolicyMapping(resource.getResourceType(), properties);
    }
    Resource referencer = context.resourceResolver().getResource(referencerPath);
    SlingBindings slingBindings = new SlingBindings();
    ComponentContext componentContext = mock(ComponentContext.class);
    ComponentContext parentContext = mock(ComponentContext.class);
    when(parentContext.getResource()).thenReturn(referencer);
    when(componentContext.getParent()).thenReturn(parentContext);
    Page currentPage = context.pageManager().getPage(currentPagePath);
    slingBindings.put(WCMBindings.COMPONENT_CONTEXT, componentContext);
    slingBindings.put(WCMBindings.CURRENT_PAGE, currentPage);
    MockSlingHttpServletRequest request = context.request();
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    return request.adaptTo(Component.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) ComponentContext(com.day.cq.wcm.api.components.ComponentContext) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page)

Example 2 with ComponentContext

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

the class PageImplTest method getPageUnderTest.

protected Page getPageUnderTest(String pagePath, Object... properties) {
    Utils.enableDataLayer(context, true);
    Map<String, Object> propertyMap = MapUtil.toMap(properties);
    Resource resource = context.currentResource(pagePath + "/" + JcrConstants.JCR_CONTENT);
    MockSlingHttpServletRequest request = context.request();
    context.request().setContextPath("/core");
    if (resource != null && !propertyMap.isEmpty()) {
        if (propertyMap.containsKey(DESIGN_PATH_KEY)) {
            Designer designer = context.resourceResolver().adaptTo(Designer.class);
            if (designer != null) {
                Design design = designer.getDesign(pagePath);
                Design spyDesign = Mockito.spy(design);
                Mockito.doReturn(propertyMap.get(DESIGN_PATH_KEY)).when(spyDesign).getPath();
                request.setAttribute(DESIGN_CACHE_KEY, spyDesign);
            }
        }
        if (propertyMap.containsKey(CSS_CLASS_NAMES_KEY)) {
            ComponentContext spyComponentContext = Mockito.spy((ComponentContext) request.getAttribute(ComponentContext.CONTEXT_ATTR_NAME));
            Mockito.doReturn(Sets.newLinkedHashSet(Arrays.asList((String[]) propertyMap.get(CSS_CLASS_NAMES_KEY)))).when(spyComponentContext).getCssClassNames();
            request.setAttribute(ComponentContext.CONTEXT_ATTR_NAME, spyComponentContext);
        }
        context.contentPolicyMapping(resource.getResourceType(), properties);
    }
    return request.adaptTo(Page.class);
}
Also used : Design(com.day.cq.wcm.api.designer.Design) Designer(com.day.cq.wcm.api.designer.Designer) ComponentContext(com.day.cq.wcm.api.components.ComponentContext) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource)

Example 3 with ComponentContext

use of com.day.cq.wcm.api.components.ComponentContext 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)

Example 4 with ComponentContext

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

the class AemObjectInjector method getCurrentStyle.

/**
 * Get the current style.
 *
 * @param adaptable a SlingHttpServletRequest
 * @return the current Style if the adaptable was a SlingHttpServletRequest, null otherwise
 */
private Style getCurrentStyle(Object adaptable) {
    Design currentDesign = getCurrentDesign(adaptable);
    ComponentContext componentContext = getComponentContext(adaptable);
    if (currentDesign != null && componentContext != null) {
        return currentDesign.getStyle(componentContext.getCell());
    }
    return null;
}
Also used : Design(com.day.cq.wcm.api.designer.Design) ComponentContext(com.day.cq.wcm.api.components.ComponentContext)

Example 5 with ComponentContext

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

the class ComponentErrorHandlerImpl method accepts.

protected final boolean accepts(final SlingHttpServletRequest request, final SlingHttpServletResponse response) {
    if (!StringUtils.endsWith(request.getRequestURI(), ".html") || !StringUtils.contains(response.getContentType(), "html")) {
        // Do not inject around non-HTML requests
        return false;
    }
    final ComponentContext componentContext = WCMUtils.getComponentContext(request);
    if (// ComponentContext is null
    componentContext == null || // Component is null
    componentContext.getComponent() == null || componentContext.isRoot()) {
        // Suppress on root context
        return false;
    }
    // Check to make sure the suppress key has not been added to the request
    if (this.isComponentErrorHandlingSuppressed(request)) {
        return false;
    }
    // Check to make sure the SlingRequest's resource isn't in the suppress list
    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
    for (final String suppressedResourceType : suppressedResourceTypes) {
        if (slingRequest.getResource().isResourceType(suppressedResourceType)) {
            return false;
        }
    }
    return true;
}
Also used : ComponentContext(com.day.cq.wcm.api.components.ComponentContext) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Aggregations

ComponentContext (com.day.cq.wcm.api.components.ComponentContext)5 Resource (org.apache.sling.api.resource.Resource)3 Page (com.day.cq.wcm.api.Page)2 Design (com.day.cq.wcm.api.designer.Design)2 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)2 PageManager (com.day.cq.wcm.api.PageManager)1 Template (com.day.cq.wcm.api.Template)1 Designer (com.day.cq.wcm.api.designer.Designer)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1 NotNull (org.jetbrains.annotations.NotNull)1