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: Large-Scale Import Optimization #3504

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
<!-- Keep this up to date! After a release, change the tag name to the latest release -->-

## Unreleased ([details][unreleased changes details])

- ### Added
#3501 Redirect Manager: Large-Scale Import Optimization

### Fixed
- #3497 - Redirect Manager: allow creating redirect configurations in a nested hierarchy

## 6.10.0 - 2024-12-13

### Fixed
- #3497 - Redirect Manager: allow creating redirect configurations in a nested hierarchy

## 6.10.0 - 2024-12-13

### Changed

Expand Down
2 changes: 1 addition & 1 deletion all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.10.1-SNAPSHOT</version>
<version>6.11.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
2 changes: 1 addition & 1 deletion bundle-cloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.10.1-SNAPSHOT</version>
<version>6.11.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
2 changes: 1 addition & 1 deletion bundle-onprem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.10.1-SNAPSHOT</version>
<version>6.11.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
2 changes: 1 addition & 1 deletion bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.10.1-SNAPSHOT</version>
<version>6.11.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
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 Expand Up @@ -101,6 +102,7 @@
import org.slf4j.LoggerFactory;

import static com.adobe.acs.commons.redirects.models.Redirects.CFG_PROP_IGNORE_SELECTORS;
import static com.adobe.acs.commons.redirects.models.Redirects.readRedirects;
import static org.apache.sling.engine.EngineConstants.SLING_FILTER_SCOPE;
import static org.osgi.framework.Constants.SERVICE_DESCRIPTION;
import static org.osgi.framework.Constants.SERVICE_ID;
Expand Down Expand Up @@ -332,15 +334,14 @@ RedirectConfiguration loadRules(Resource storageResource) {
}

public static Collection<RedirectRule> getRules(Resource resource) {
Collection<RedirectRule> rules = new ArrayList<>();
for (Resource res : resource.getChildren()) {
if(res.isResourceType(REDIRECT_RULE_RESOURCE_TYPE)){
RedirectRule rule = res.adaptTo(RedirectRule.class);
if(rule != null) {
rules.add(rule);
}
}
}
List<Resource> resources = readRedirects(resource);
long t0 = System.currentTimeMillis();
Collection<RedirectRule> rules = resources
.stream()
.map(res -> res.adaptTo(RedirectRule.class))
.filter(res -> res != null)
.collect(Collectors.toList());
log.trace("mapped {} models in {} ms", resources.size(), System.currentTimeMillis() - t0);
return rules;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum ExportColumn {
CREATED_BY("Created By", RedirectRule.CREATED_BY_PROPERTY_NAME, String.class, false),
MODIFIED("Modified", RedirectRule.MODIFIED_PROPERTY_NAME, Calendar.class, false),
MODIFIED_BY("Modified By", RedirectRule.MODIFIED_BY_PROPERTY_NAME, String.class, false),
CACHE_CONTROL("Cache-Control", RedirectRule.CACHE_CONTROL_HEADER_NAME, String.class, false);
CACHE_CONTROL("Cache-Control", RedirectRule.CACHE_CONTROL_HEADER_NAME, String.class, true);

private final String title;
private final String propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public String getMsg() {
}
}

enum Level {
public enum Level {
WARN,
INFO
}
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 @@ -148,8 +148,6 @@ protected void init() {

source = source.trim();
target = target.trim();
createdBy = AuthorizableUtil.getFormattedName(resource.getResourceResolver(), createdBy);
modifiedBy = AuthorizableUtil.getFormattedName(resource.getResourceResolver(), modifiedBy);

String regex = source;
if (regex.endsWith("*")) {
Expand Down Expand Up @@ -187,11 +185,11 @@ public boolean isCaseInsensitive() {
}

public String getCreatedBy() {
return createdBy;
return AuthorizableUtil.getFormattedName(resource.getResourceResolver(), createdBy);
}

public String getModifiedBy() {
return modifiedBy;
return AuthorizableUtil.getFormattedName(resource.getResourceResolver(), modifiedBy);
}

public boolean getContextPrefixIgnored() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
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.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.adobe.acs.commons.redirects.filter.RedirectFilter.REDIRECT_RULE_RESOURCE_TYPE;
import static com.adobe.acs.commons.redirects.models.RedirectRule.*;

/**
Expand All @@ -50,6 +54,8 @@ public class Redirects {

public static final String CFG_PROP_CONTEXT_PREFIX = "contextPrefix";
public static final String CFG_PROP_IGNORE_SELECTORS = "ignoreSelectors";
public static final String CFG_PROP_PAGE_SIZE = "pageSize";
private static final Logger log = LoggerFactory.getLogger(Redirects.class);

@SlingObject
private SlingHttpServletRequest request;
Expand All @@ -72,8 +78,7 @@ protected void init() {
ValueMap properties = configResource.getValueMap();
contextPrefix = properties.get(Redirects.CFG_PROP_CONTEXT_PREFIX, "");
ignoreSelectors = properties.get(Redirects.CFG_PROP_IGNORE_SELECTORS, false);

List<Resource> all = new ArrayList<>();
pageSize = properties.get(Redirects.CFG_PROP_PAGE_SIZE, 100);

if (ArrayUtils.contains(request.getRequestPathInfo().getSelectors(), "search")) {
// Search
Expand All @@ -85,11 +90,32 @@ protected void init() {
if (pg != null) {
pageNumber = Integer.parseInt(pg);
}
configResource.listChildren().forEachRemaining(all::add);
List<Resource> all = readRedirects(configResource);
pages = Lists.partition(all, pageSize);
}
}

/**
* Read redirects stored in AEM
*
* @param configResource the configuration resource, e.g. /conf/my-site/settings/redirects
* @return list of collected redirect resources
*/
public static List<Resource> readRedirects(Resource configResource) {
long t0 = System.currentTimeMillis();
List<Resource> redirects = new ArrayList<>();
new AbstractResourceVisitor() {
@Override
public void visit(Resource res) {
if(res.isResourceType(REDIRECT_RULE_RESOURCE_TYPE)){
redirects.add(res);
}
}
}.accept(configResource);
log.debug("Read {} redirects from {} in {}ms", redirects.size(), configResource.getPath(), System.currentTimeMillis() - t0);
return redirects;
}

public List<Resource> getItems() {
return pages.isEmpty() ? Collections.emptyList() : pages.get(pageNumber - 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@org.osgi.annotation.versioning.Version("6.1.0")
@org.osgi.annotation.versioning.Version("6.11.0")
package com.adobe.acs.commons.redirects.models;
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import org.osgi.service.component.annotations.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
Expand Down Expand Up @@ -61,7 +61,7 @@ public class ExportRedirectMapServlet extends SlingSafeMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(ExportRedirectMapServlet.class);
private static final long serialVersionUID = -3564475196678277711L;

static final String SPREADSHEETML_SHEET = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
static final String CONTENT_TYPE_EXCEL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
Expand All @@ -72,16 +72,17 @@ protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse r
log.debug("Requesting redirect maps from {}", path);

Collection<RedirectRule> rules = RedirectFilter.getRules(root);
XSSFWorkbook wb = export(rules);
Workbook wb = export(rules);

response.setContentType(SPREADSHEETML_SHEET);
response.setHeader("Content-Disposition", "attachment;filename=\"acs-redirects.xlsx\" ");
response.setContentType(CONTENT_TYPE_EXCEL);
String fileName = root.getParent().getParent().getName() + "-redirects";
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xlsx\" ");
wb.write(response.getOutputStream());
}

static XSSFWorkbook export(Collection<RedirectRule> rules) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFCellStyle headerStyle = wb.createCellStyle();
static Workbook export(Collection<RedirectRule> rules) {
Workbook wb = new SXSSFWorkbook();
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont();
Expand Down
Loading
Loading