Skip to content

Commit

Permalink
Merge branch 'Adobe-Consulting-Services:master' into feature/view-pub…
Browse files Browse the repository at this point in the history
…lish-url-sites
  • Loading branch information
krassib authored Dec 9, 2024
2 parents 285625a + 06461dd commit d9e876b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,21 +53,24 @@ public class Configurations {
private static final String REDIRECTS_RESOURCE_TYPE = "acs-commons/components/utilities/manage-redirects/redirects";

public Collection<RedirectConfiguration> getConfigurations() {
String sql = "SELECT * FROM [nt:unstructured] AS s WHERE ISDESCENDANTNODE([/conf]) "
+ "AND s.[sling:resourceType]='" + REDIRECTS_RESOURCE_TYPE + "'";
log.debug(sql);
Iterator<Resource> it = request.getResourceResolver().findResources(sql, Query.JCR_SQL2);
List<RedirectConfiguration> lst = new ArrayList<>();
String bucketName = redirectFilter == null ? RedirectFilter.DEFAULT_CONFIG_BUCKET : redirectFilter.getBucket();
String configName = redirectFilter == null ? RedirectFilter.DEFAULT_CONFIG_NAME : redirectFilter.getConfigName();
List<RedirectConfiguration> 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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@
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;

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;

Expand All @@ -43,42 +45,53 @@ 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<RedirectConfiguration> 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());
assertEquals(HttpServletResponse.SC_CONFLICT, context.response().getStatus());
}

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

0 comments on commit d9e876b

Please sign in to comment.