forked from adobe/aem-core-wcm-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DM support to Teaser component (adobe#2350)
- provide servlet to include asset tab from image component into the the teaser dialog. In this case the cqDesign request attribute needs to be reset to the imageDelegate component - move the DM settings from the metadata tab to the asset tap in the image dialog, so it is still only required to include the asset tab within the teaser dialog relates to adobe#1473
- Loading branch information
Showing
31 changed files
with
5,373 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...n/java/com/adobe/cq/wcm/core/components/internal/servlets/ImageDelegatePolicyServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
~ Copyright 2022 Adobe | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | ||
package com.adobe.cq.wcm.core.components.internal.servlets; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import javax.servlet.RequestDispatcher; | ||
import javax.servlet.Servlet; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.SlingHttpServletResponse; | ||
import org.apache.sling.api.request.RequestPathInfo; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
import org.apache.sling.api.resource.SyntheticResource; | ||
import org.apache.sling.api.resource.ValueMap; | ||
import org.apache.sling.api.servlets.SlingSafeMethodsServlet; | ||
import org.apache.sling.servlets.annotations.SlingServletResourceTypes; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
import com.adobe.cq.wcm.core.components.internal.models.v1.AbstractImageDelegatingModel; | ||
import com.adobe.granite.ui.components.ExpressionResolver; | ||
import com.day.cq.wcm.api.WCMFilteringResourceWrapper; | ||
import com.day.cq.wcm.api.components.ComponentManager; | ||
|
||
@Component(service = { Servlet.class }) | ||
@SlingServletResourceTypes( | ||
resourceTypes = ImageDelegatePolicyServlet.RESOURCE_TYPE | ||
) | ||
public class ImageDelegatePolicyServlet extends SlingSafeMethodsServlet { | ||
|
||
public static final String RESOURCE_TYPE = "core/wcm/components/include/imagedelegate"; | ||
private static final String PN_PATH = "path"; | ||
|
||
@Reference | ||
ExpressionResolver expressionResolver; | ||
|
||
@Override | ||
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) | ||
throws ServletException, IOException { | ||
ResourceResolver resourceResolver = request.getResourceResolver(); | ||
ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class); | ||
RequestPathInfo requestPathInfo = request.getRequestPathInfo(); | ||
com.day.cq.wcm.api.components.Component component = | ||
Optional.ofNullable(requestPathInfo.getSuffix()) | ||
.flatMap(s -> Optional.ofNullable(resourceResolver.getResource(s))) | ||
.flatMap(r -> Optional.ofNullable(componentManager) | ||
.map(c -> c.getComponentOfResource(r))) | ||
.orElse(null); | ||
if (Objects.nonNull(component)) { | ||
ValueMap properties = component.getProperties(); | ||
String imageDelegate = properties.get(AbstractImageDelegatingModel.IMAGE_DELEGATE, String.class); | ||
RequestDispatcher requestDispatcher = Optional.ofNullable(request.getResource().getValueMap().get(PN_PATH | ||
, String.class)) | ||
.map(p -> new WCMFilteringResourceWrapper(resourceResolver.getResource(p), new SyntheticResource(resourceResolver, | ||
requestPathInfo.getSuffix(), | ||
imageDelegate), expressionResolver, request)).map(request::getRequestDispatcher).orElse(null); | ||
if (Objects.nonNull(requestDispatcher)) { | ||
requestDispatcher.include(request, response); | ||
} else { | ||
response.setStatus(HttpServletResponse.SC_NOT_FOUND); | ||
} | ||
} else { | ||
response.setStatus(HttpServletResponse.SC_NOT_FOUND); | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
...va/com/adobe/cq/wcm/core/components/internal/servlets/ImageDelegatePolicyServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
~ Copyright 2022 Adobe | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | ||
package com.adobe.cq.wcm.core.components.internal.servlets; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.RequestDispatcher; | ||
import javax.servlet.ServletException; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.apache.sling.api.request.RequestDispatcherOptions; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.testing.mock.sling.servlet.MockRequestDispatcherFactory; | ||
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import com.adobe.cq.wcm.core.components.context.CoreComponentTestContext; | ||
import com.adobe.granite.ui.components.ExpressionCustomizer; | ||
import com.day.cq.wcm.api.designer.Style; | ||
import io.wcm.testing.mock.aem.junit5.AemContext; | ||
import io.wcm.testing.mock.aem.junit5.AemContextExtension; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith({AemContextExtension.class, MockitoExtension.class}) | ||
class ImageDelegatePolicyServletTest { | ||
|
||
private static final String TEST_BASE = "/image-delegate-policy-servlet"; | ||
private static final String APPS_ROOT = "/apps/core/wcm/components"; | ||
private static final String CONTENT_ROOT = "/content"; | ||
private static final String SUFFIX = "/content/test/jcr:content/root/responsivegrid/teaser"; | ||
|
||
public final AemContext context = CoreComponentTestContext.newAemContext(); | ||
private ImageDelegatePolicyServlet underTest; | ||
|
||
@Mock | ||
private RequestDispatcher requestDispatcher; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
context.load().json(TEST_BASE + CoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT); | ||
context.load().json(TEST_BASE + CoreComponentTestContext.TEST_APPS_JSON, APPS_ROOT); | ||
underTest = new ImageDelegatePolicyServlet(); | ||
} | ||
|
||
@Test | ||
void testCqDesignAttribute() throws ServletException, IOException { | ||
context.requestPathInfo().setSuffix(SUFFIX); | ||
context.currentResource("/apps/core/wcm/components/teaser/cq:design/content/items/tabs/items/image"); | ||
MockSlingHttpServletRequest request = context.request(); | ||
request.setRequestDispatcherFactory(new MockRequestDispatcherFactory() { | ||
@Override | ||
public RequestDispatcher getRequestDispatcher(String s, RequestDispatcherOptions requestDispatcherOptions) { | ||
return requestDispatcher; | ||
} | ||
|
||
@Override | ||
public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions requestDispatcherOptions) { | ||
return requestDispatcher; | ||
} | ||
}); | ||
underTest.doGet(request, context.response()); | ||
ExpressionCustomizer expressionCustomizer = (ExpressionCustomizer) request.getAttribute(ExpressionCustomizer.class.getName()); | ||
assertNotNull(expressionCustomizer); | ||
Style cqDesign = (Style) expressionCustomizer.getVariable("cqDesign"); | ||
assertNotNull(cqDesign); | ||
verify(requestDispatcher).include(context.request(), context.response()); | ||
} | ||
|
||
@Test | ||
void testComponentNotFound() throws ServletException, IOException { | ||
context.requestPathInfo().setSuffix(SUFFIX + "non-existing"); | ||
context.currentResource("/apps/core/wcm/components/teaser/cq:design/content/items/tabs/items/image"); | ||
MockSlingHttpServletRequest request = context.request(); | ||
underTest.doGet(request, context.response()); | ||
assertEquals(HttpStatus.SC_NOT_FOUND, context.response().getStatus()); | ||
} | ||
|
||
@Test | ||
void testNoRequestDispatcher() throws ServletException, IOException { | ||
MockSlingHttpServletRequest request = context.request(); | ||
context.currentResource("/apps/core/wcm/components/teaser/cq:design/content/items/tabs/items/image"); | ||
request.setRequestDispatcherFactory(new MockRequestDispatcherFactory() { | ||
@Override | ||
public RequestDispatcher getRequestDispatcher(String s, RequestDispatcherOptions requestDispatcherOptions) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions requestDispatcherOptions) { | ||
return null; | ||
} | ||
}); | ||
context.requestPathInfo().setSuffix(SUFFIX); | ||
underTest.doGet(request, context.response()); | ||
assertEquals(HttpStatus.SC_NOT_FOUND, context.response().getStatus()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
bundles/core/src/test/resources/image-delegate-policy-servlet/test-apps.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"teaser": { | ||
"jcr:primaryType" : "cq:Component", | ||
"imageDelegate" : "core/wcm/components/image", | ||
"jcr:createdBy" : "admin", | ||
"jcr:title" : "Teaser (v2)", | ||
"jcr:description" : "Displays an image together with a link that can act as a teaser for a site section.", | ||
"cq:icon" : "image", | ||
"componentGroup" : ".core-wcm", | ||
"cq:design" : { | ||
"jcr:primaryType": "nt:unstructured", | ||
"content": { | ||
"jcr:primaryType": "nt:unstructured", | ||
"items": { | ||
"jcr:primaryType": "nt:unstructured", | ||
"tabs": { | ||
"jcr:primaryType": "nt:unstructured", | ||
"items": { | ||
"jcr:primaryType": "nt:unstructured", | ||
"image" :{ | ||
"jcr:primaryType": "nt:unstructured", | ||
"sling:resourceType": "core/wcm/components/include/imagedelegate", | ||
"path" : "core/wcm/components/image/cq:dialog/content/items/tabs/items/asset" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"image" : { | ||
"jcr:primaryType" : "cq:Component", | ||
"jcr:createdBy" : "admin", | ||
"jcr:title" : "Image (v3)", | ||
"sling:resourceSuperType": "core/wcm/components/image", | ||
"jcr:description" : "Smart Adaptive Image", | ||
"cq:icon" : "image", | ||
"componentGroup" : ".core-wcm" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
bundles/core/src/test/resources/image-delegate-policy-servlet/test-content.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"test" : { | ||
"jcr:primaryType": "cq:Page", | ||
"jcr:content": { | ||
"jcr:primaryType": "cq:PageContent", | ||
"root": { | ||
"jcr:primaryType": "nt:unstructured", | ||
"responsivegrid": { | ||
"jcr:primaryType": "nt:unstructured", | ||
"teaser": { | ||
"jcr:primaryType": "nt:unstructured", | ||
"sling:resourceType": "core/wcm/components/teaser" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"globals": { | ||
"Coral": false, | ||
"Granite": false, | ||
"CMP": false | ||
"CMP": false, | ||
"Promise": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/coverage |
Oops, something went wrong.