Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect Manager: allow creating redirect configurations in a nested hierarchy #3498

Merged
merged 8 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
### Fixed

- #3471 - EmailService not working due to unsatisfied reference to MailTemplateManager in AEM on prem
- #3497 - Redirect Manager: allow creating redirect configurations in a nested hierarchy

## 6.9.10 - 2024-12-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
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.AbstractResourceVisitor;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* ACS AEM Commons
*
* Copyright (C) 2013 - 2023 Adobe
*
/*-
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2013 - 2024 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
Expand All @@ -14,27 +15,23 @@
* 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.
* #L%
*/
package com.adobe.acs.commons.redirects.models;

import com.adobe.acs.commons.redirects.filter.RedirectFilter;
import com.adobe.acs.commons.redirects.filter.RedirectFilterMBean;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.AbstractResourceVisitor;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.query.Query;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
Expand All @@ -61,12 +58,24 @@ public Collection<RedirectConfiguration> getConfigurations() {

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));
new AbstractResourceVisitor() {
@Override
public void accept(Resource res) {
if (res != null) {
this.visit(res);
if(!res.getPath().endsWith(storageSuffix)){
this.traverseChildren(res.listChildren());
}
}
}

@Override
public void visit(Resource res) {
if (res.isResourceType(REDIRECTS_RESOURCE_TYPE)) {
configurations.add(new RedirectConfiguration(res, storageSuffix, false));
}
}
}
}.accept(confRoot);

configurations.sort(Comparator.comparing(RedirectConfiguration::getName));
return configurations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,25 @@ public class RedirectConfiguration {

private RedirectConfiguration(){
pathRules = new LinkedHashMap<>();
caseInsensitiveRules = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
patternRules = new LinkedHashMap<>();
}

public RedirectConfiguration(Resource resource, String storageSuffix) {
pathRules = new LinkedHashMap<>();
caseInsensitiveRules = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
patternRules = new LinkedHashMap<>();
path = resource.getPath();
this(resource, storageSuffix, true);
}

RedirectConfiguration(Resource configResource, String storageSuffix, boolean loadRules) {
this();
path = configResource.getPath();
name = path.replace("/" + storageSuffix, "");
Collection<RedirectRule> rules = RedirectFilter.getRules(resource);
if(loadRules){
loadRules(configResource);
}
}

void loadRules(Resource configResource) {
Collection<RedirectRule> rules = RedirectFilter.getRules(configResource);
for (RedirectRule rule : rules) {
if (rule.getRegex() != null) {
patternRules.put(rule.getRegex(), rule);
Expand All @@ -84,7 +93,6 @@ public RedirectConfiguration(Resource resource, String storageSuffix) {
/**
* @return resource path without .html extension
*/

public static String normalizePath(String resourcePath) {
int sep = resourcePath.lastIndexOf('.');
if (sep != -1 && !resourcePath.startsWith("/content/dam/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
if (config == null) {
String contextPrefix = StringUtils.defaultString(request.getParameter(REQ_PARAM_CTX_PREFIX));
config = resolver.create(bucket, configName,
ImmutableMap.of(JcrConstants.JCR_PRIMARYTYPE, JcrResourceConstants.NT_SLING_FOLDER,
ImmutableMap.of(JcrConstants.JCR_PRIMARYTYPE, JcrResourceConstants.NT_SLING_ORDERED_FOLDER,
ResourceResolver.PROPERTY_RESOURCE_TYPE, REDIRECTS_RESOURCE_PATH,
REQ_PARAM_CTX_PREFIX, contextPrefix));
log.info("created {} with context prefix '{}'", config.getPath(), contextPrefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -73,6 +75,29 @@ public void createConfig() throws ServletException, IOException {
assertEquals(HttpServletResponse.SC_CONFLICT, context.response().getStatus());
}

@Test
public void createDeepHierarchies() throws ServletException, IOException {
context.build().resource("/conf/level0/level1/level2");

context.request().setParameterMap(Collections.singletonMap("path", "/conf/level0"));
servlet.doPost(context.request(), context.response());

context.request().setParameterMap(Collections.singletonMap("path", "/conf/level0/level1"));
servlet.doPost(context.request(), context.response());

context.request().setParameterMap(Collections.singletonMap("path", "/conf/level0/level1/level2"));
servlet.doPost(context.request(), context.response());

assertEquals(HttpServletResponse.SC_OK, context.response().getStatus());

// Read configurations via Model
Configurations confModel = context.request().adaptTo(Configurations.class);
Iterator<RedirectConfiguration> configurations = confModel.getConfigurations().iterator();
assertEquals("/conf/level0/settings/redirects", configurations.next().getPath());
assertEquals("/conf/level0/level1/settings/redirects", configurations.next().getPath());
assertEquals("/conf/level0/level1/level2/settings/redirects", configurations.next().getPath());
}

@Test
public void createConfigWithContextPrefix() throws ServletException, IOException {
context.build().resource("/conf/global");
Expand Down
Loading