diff --git a/CHANGELOG.md b/CHANGELOG.md index 726f307819..78546bdb38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com) ## Unreleased ([details][unreleased changes details]) - #3480 - Sites Copy Publish URLs +### Fixed +- #3479 - Fixed Configurations Model for Redirect Manager after change in "redirect" resource as "sling:Folder" + ## 6.9.6 - 2024-11-20 ### Fixed diff --git a/bundle/src/main/java/com/adobe/acs/commons/redirects/models/Configurations.java b/bundle/src/main/java/com/adobe/acs/commons/redirects/models/Configurations.java index 8927a6a341..4a20dceece 100755 --- a/bundle/src/main/java/com/adobe/acs/commons/redirects/models/Configurations.java +++ b/bundle/src/main/java/com/adobe/acs/commons/redirects/models/Configurations.java @@ -43,7 +43,6 @@ */ @Model(adaptables = SlingHttpServletRequest.class) public class Configurations { - private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); @SlingObject private SlingHttpServletRequest request; @@ -54,21 +53,24 @@ public class Configurations { private static final String REDIRECTS_RESOURCE_TYPE = "acs-commons/components/utilities/manage-redirects/redirects"; public Collection getConfigurations() { - String sql = "SELECT * FROM [nt:unstructured] AS s WHERE ISDESCENDANTNODE([/conf]) " - + "AND s.[sling:resourceType]='" + REDIRECTS_RESOURCE_TYPE + "'"; - log.debug(sql); - Iterator it = request.getResourceResolver().findResources(sql, Query.JCR_SQL2); - List lst = new ArrayList<>(); - String bucketName = redirectFilter == null ? RedirectFilter.DEFAULT_CONFIG_BUCKET : redirectFilter.getBucket(); - String configName = redirectFilter == null ? RedirectFilter.DEFAULT_CONFIG_NAME : redirectFilter.getConfigName(); + List configurations = new ArrayList<>(); - while (it.hasNext()) { - Resource resource = it.next(); - String storageSuffix = bucketName + "/" + configName; - RedirectConfiguration cfg = new RedirectConfiguration(resource, storageSuffix); - lst.add(cfg); + String bucketName = redirectFilter != null ? redirectFilter.getBucket() : RedirectFilter.DEFAULT_CONFIG_BUCKET; + String configName = redirectFilter != null ? redirectFilter.getConfigName() : RedirectFilter.DEFAULT_CONFIG_NAME; + String storageSuffix = bucketName + "/" + configName; + + Resource confRoot = request.getResourceResolver().getResource("/conf"); + + for (Resource child : confRoot.getChildren()) { + Resource res = child.getChild(storageSuffix); + if (res != null && res.isResourceType(REDIRECTS_RESOURCE_TYPE)) { + configurations.add(new RedirectConfiguration(res, storageSuffix)); + } } - lst.sort(Comparator.comparing(RedirectConfiguration::getName)); - return lst; + + configurations.sort(Comparator.comparing(RedirectConfiguration::getName)); + return configurations; } + + } diff --git a/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/CreateRedirectConfigurationServletTest.java b/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/CreateRedirectConfigurationServletTest.java index eea8879322..e93546a061 100755 --- a/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/CreateRedirectConfigurationServletTest.java +++ b/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/CreateRedirectConfigurationServletTest.java @@ -18,9 +18,11 @@ package com.adobe.acs.commons.redirects.servlets; import com.adobe.acs.commons.redirects.filter.RedirectFilterMBean; -import org.apache.sling.api.resource.Resource; +import com.adobe.acs.commons.redirects.models.Configurations; +import com.adobe.acs.commons.redirects.models.RedirectConfiguration; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -28,9 +30,9 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.Collection; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -43,22 +45,28 @@ public class CreateRedirectConfigurationServletTest { @Before public void setUp() { + context.addModelsForClasses(Configurations.class); servlet = new CreateRedirectConfigurationServlet(); RedirectFilterMBean redirectFilter = mock(RedirectFilterMBean.class); when(redirectFilter.getBucket()).thenReturn("settings"); when(redirectFilter.getConfigName()).thenReturn("redirects"); + context.registerService(RedirectFilterMBean.class, redirectFilter); servlet.redirectFilter = redirectFilter; } @Test - public void createConfig() throws ServletException, IOException { + public void createConfig() throws ServletException, IOException { context.build().resource("/conf/global"); context.request().addRequestParameter("path", "/conf/global"); servlet.doPost(context.request(), context.response()); assertEquals(HttpServletResponse.SC_OK, context.response().getStatus()); - Resource cfg = context.resourceResolver().getResource("/conf/global/settings/redirects"); - assertNotNull(cfg); + + // Read configurations via Model + Configurations confModel = context.request().adaptTo(Configurations.class); + Collection configurations = confModel.getConfigurations(); + RedirectConfiguration cfg = configurations.iterator().next(); + assertEquals("/conf/global/settings/redirects", cfg.getPath()); // return 409 if already exists servlet.doPost(context.request(), context.response()); @@ -66,19 +74,24 @@ public void createConfig() throws ServletException, IOException { } @Test - public void createConfigWithContextPrefix() throws ServletException, IOException { + public void createConfigWithContextPrefix() throws ServletException, IOException { context.build().resource("/conf/global"); - context.request().addRequestParameter("path", "/conf/global"); - context.request().addRequestParameter("contextPrefix", "/content/mysite"); - servlet.doPost(context.request(), context.response()); + MockSlingHttpServletRequest request = context.request(); + request.addRequestParameter("path", "/conf/global"); + request.addRequestParameter("contextPrefix", "/content/mysite"); + servlet.doPost(request, context.response()); assertEquals(HttpServletResponse.SC_OK, context.response().getStatus()); - Resource cfg = context.resourceResolver().getResource("/conf/global/settings/redirects"); - assertNotNull(cfg); - assertEquals("/content/mysite", cfg.getValueMap().get("contextPrefix")); + + // Read configurations via Model + Configurations confModel = request.adaptTo(Configurations.class); + Collection configurations = confModel.getConfigurations(); + RedirectConfiguration cfg = configurations.iterator().next(); + assertEquals("/conf/global/settings/redirects", cfg.getPath()); + assertEquals("/content/mysite", request.getResourceResolver().getResource(cfg.getPath()).getValueMap().get("contextPrefix")); // return 409 if already exists - servlet.doPost(context.request(), context.response()); + servlet.doPost(request, context.response()); assertEquals(HttpServletResponse.SC_CONFLICT, context.response().getStatus()); } }