Search in sources :

Example 1 with ExpressionCustomizer

use of com.adobe.granite.ui.components.ExpressionCustomizer in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ImageDelegateRenderConditionTest method testDoGet.

@Test
public void testDoGet() throws Exception {
    ImageDelegateRenderCondition imageDelegateRenderCondition = new ImageDelegateRenderCondition();
    context.requestPathInfo().setSuffix(SUFFIX);
    imageDelegateRenderCondition.doGet(context.request(), context.response());
    RenderCondition renderCondition = (RenderCondition) context.request().getAttribute(RenderCondition.class.getName());
    assertNotNull(renderCondition);
    assertTrue(renderCondition.check());
    ExpressionCustomizer expressionCustomizer = (ExpressionCustomizer) context.request().getAttribute(ExpressionCustomizer.class.getName());
    assertNotNull(expressionCustomizer);
    assertTrue(expressionCustomizer.hasVariable(AbstractImageDelegatingModel.IMAGE_DELEGATE));
}
Also used : RenderCondition(com.adobe.granite.ui.components.rendercondition.RenderCondition) ExpressionCustomizer(com.adobe.granite.ui.components.ExpressionCustomizer) Test(org.junit.jupiter.api.Test)

Example 2 with ExpressionCustomizer

use of com.adobe.granite.ui.components.ExpressionCustomizer in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ImageDelegateRenderCondition method doGet.

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
    boolean hasImageDelegation = false;
    ResourceResolver resourceResolver = request.getResourceResolver();
    ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
    String suffix = request.getRequestPathInfo().getSuffix();
    if (componentManager != null && StringUtils.isNotEmpty(suffix)) {
        Resource policiesRootPage = getPoliciesRootPage(resourceResolver, suffix);
        if (policiesRootPage != null) {
            String resourceType = suffix.substring(policiesRootPage.getPath().length() + 1, suffix.lastIndexOf('/'));
            com.day.cq.wcm.api.components.Component component = componentManager.getComponent(resourceType);
            if (component != null && component.isAccessible()) {
                String imageDelegate = component.getProperties().get(AbstractImageDelegatingModel.IMAGE_DELEGATE, String.class);
                if (StringUtils.isNotEmpty(imageDelegate)) {
                    hasImageDelegation = true;
                    com.day.cq.wcm.api.components.Component delegate = componentManager.getComponent(imageDelegate);
                    if (delegate != null && delegate.isAccessible()) {
                        ExpressionCustomizer customizer = ExpressionCustomizer.from(request);
                        customizer.setVariable(AbstractImageDelegatingModel.IMAGE_DELEGATE, delegate);
                    }
                }
            }
        }
    }
    request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(hasImageDelegation));
}
Also used : SimpleRenderCondition(com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition) RenderCondition(com.adobe.granite.ui.components.rendercondition.RenderCondition) SimpleRenderCondition(com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition) ExpressionCustomizer(com.adobe.granite.ui.components.ExpressionCustomizer) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ComponentManager(com.day.cq.wcm.api.components.ComponentManager) Resource(org.apache.sling.api.resource.Resource)

Aggregations

ExpressionCustomizer (com.adobe.granite.ui.components.ExpressionCustomizer)2 RenderCondition (com.adobe.granite.ui.components.rendercondition.RenderCondition)2 SimpleRenderCondition (com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition)1 ComponentManager (com.day.cq.wcm.api.components.ComponentManager)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 Test (org.junit.jupiter.api.Test)1