From 1dee38ac612d83ab195e73b115eee68d823bc6c4 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Sat, 4 Nov 2023 16:01:48 +0100 Subject: [PATCH 1/4] fixed script move/rename issue --- .../apm/core/endpoints/ScriptMoveServlet.java | 11 +++++++++ .../apm/core/scripts/FileDescriptor.java | 22 +++++++++++++++++- .../apm/core/scripts/ScriptStorageImpl.java | 23 +------------------ 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java index 3890a70f..c041e23c 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java @@ -23,9 +23,12 @@ import com.cognifide.apm.core.Property; import com.cognifide.apm.core.endpoints.response.ResponseEntity; import com.cognifide.apm.core.endpoints.utils.RequestProcessor; +import com.cognifide.apm.core.scripts.FileDescriptor; +import com.cognifide.apm.core.scripts.ScriptStorageException; import com.day.cq.commons.jcr.JcrConstants; import com.day.cq.commons.jcr.JcrUtil; import java.io.IOException; +import java.util.List; import javax.jcr.Session; import javax.servlet.Servlet; import org.apache.commons.lang3.StringUtils; @@ -62,6 +65,7 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse String rename = containsExtension(form.getPath()) ? (form.getRename() + (containsExtension(form.getRename()) ? "" : Apm.FILE_EXT)) : JcrUtil.createValidName(form.getRename()); + validate(dest, rename); String destPath = String.format("%s/%s", dest, rename); if (!StringUtils.equals(form.getPath(), destPath)) { destPath = createUniquePath(destPath, resourceResolver); @@ -93,4 +97,11 @@ private String createUniquePath(String pathWithExtension, ResourceResolver resol } return path + (counter > 0 ? counter : "") + extension; } + + private void validate(String path, String name) { + List validationErrors = new FileDescriptor(path, name, null).validate(); + if (!validationErrors.isEmpty()) { + throw new ScriptStorageException("Script errors", validationErrors); + } + } } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/FileDescriptor.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/FileDescriptor.java index 95d3d4aa..00e27277 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/FileDescriptor.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/FileDescriptor.java @@ -21,12 +21,19 @@ package com.cognifide.apm.core.scripts; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; -class FileDescriptor { +public class FileDescriptor { private static final String SCRIPT_PATH = "/conf/apm/scripts"; + private static final Pattern FILE_NAME_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm"); + + private static final Pattern PATH_PATTERN = Pattern.compile("(/[0-9a-zA-Z_\\-]+)+"); + private final String path; private final String name; @@ -81,4 +88,17 @@ public String getName() { public InputStream getInputStream() { return inputStream; } + + public List validate() { + List errors = new ArrayList<>(); + ensurePropertyMatchesPattern(errors, "file name", name, FILE_NAME_PATTERN); + ensurePropertyMatchesPattern(errors, "file path", path, PATH_PATTERN); + return errors; + } + + private void ensurePropertyMatchesPattern(List errors, String property, String value, Pattern pattern) { + if (!pattern.matcher(value).matches()) { + errors.add(String.format("Invalid %s: \"%s\"", property, value)); + } + } } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java index ebb10e84..b6a18886 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java @@ -28,13 +28,11 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.time.OffsetDateTime; -import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; -import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.jcr.Binary; import javax.jcr.Node; @@ -60,10 +58,6 @@ public class ScriptStorageImpl implements ScriptStorage { private static final Logger LOG = LoggerFactory.getLogger(ScriptStorageImpl.class); - private static final Pattern FILE_NAME_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm"); - - private static final Pattern PATH_PATTERN = Pattern.compile("/[0-9a-zA-Z_\\-/]+"); - private static final Charset SCRIPT_ENCODING = StandardCharsets.UTF_8; @Reference @@ -159,25 +153,10 @@ private String generateFileName(String fileName, Node saveNode) throws Repositor private void validate(Collection fileDescriptors) { List validationErrors = fileDescriptors.stream() - .flatMap(fileDescriptor -> validate(fileDescriptor).stream()) + .flatMap(fileDescriptor -> fileDescriptor.validate().stream()) .collect(Collectors.toList()); if (!validationErrors.isEmpty()) { throw new ScriptStorageException("Script errors", validationErrors); } } - - private List validate(FileDescriptor file) { - List errors = new ArrayList<>(); - ensurePropertyMatchesPattern(errors, "file name", file.getName(), FILE_NAME_PATTERN); - ensurePropertyMatchesPattern(errors, "file path", file.getPath(), PATH_PATTERN); - return errors; - } - - private static void ensurePropertyMatchesPattern(List errors, String property, String value, - Pattern pattern) { - if (!pattern.matcher(value).matches()) { - errors.add(String.format("Invalid %s: \"%s\"", property, value)); - } - } - } From c90b82154958d08a7821263f19800bf8d3b64e9e Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Sun, 5 Nov 2023 01:03:41 +0100 Subject: [PATCH 2/4] fixed script move/rename issue --- .../apm/core/endpoints/ScriptMoveServlet.java | 21 +++++++++++++++-- .../apm/core/scripts/FileDescriptor.java | 22 +----------------- .../apm/core/scripts/ScriptStorageImpl.java | 23 ++++++++++++++++++- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java index c041e23c..88ff7d84 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java @@ -23,12 +23,14 @@ import com.cognifide.apm.core.Property; import com.cognifide.apm.core.endpoints.response.ResponseEntity; import com.cognifide.apm.core.endpoints.utils.RequestProcessor; -import com.cognifide.apm.core.scripts.FileDescriptor; import com.cognifide.apm.core.scripts.ScriptStorageException; +import com.cognifide.apm.core.scripts.ScriptStorageImpl; import com.day.cq.commons.jcr.JcrConstants; import com.day.cq.commons.jcr.JcrUtil; import java.io.IOException; +import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; import javax.jcr.Session; import javax.servlet.Servlet; import org.apache.commons.lang3.StringUtils; @@ -41,6 +43,8 @@ import org.apache.sling.models.factory.ModelFactory; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Component( service = Servlet.class, @@ -53,6 +57,14 @@ ) public class ScriptMoveServlet extends SlingAllMethodsServlet { + private static final Logger LOGGER = LoggerFactory.getLogger(ScriptMoveServlet.class); + + private static final Pattern SCRIPT_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm"); + + private static final Pattern FOLDER_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+"); + + private static final Pattern DESTINATION_PATTERN = Pattern.compile("(/[0-9a-zA-Z_\\-]+)+"); + @Reference private ModelFactory modelFactory; @@ -77,8 +89,10 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse valueMap.put(JcrConstants.JCR_TITLE, form.getRename()); } resourceResolver.commit(); + LOGGER.info("Item successfully moved from {} to {}", form.getPath(), destPath); return ResponseEntity.ok("Item successfully moved"); } catch (Exception e) { + LOGGER.error("Errors while moving item", e); return ResponseEntity.badRequest(StringUtils.defaultString(e.getMessage(), "Errors while moving item")); } }); @@ -99,7 +113,10 @@ private String createUniquePath(String pathWithExtension, ResourceResolver resol } private void validate(String path, String name) { - List validationErrors = new FileDescriptor(path, name, null).validate(); + List validationErrors = new ArrayList<>(); + ScriptStorageImpl.ensurePropertyMatchesPattern(validationErrors, "rename", name, + containsExtension(name) ? SCRIPT_PATTERN : FOLDER_PATTERN); + ScriptStorageImpl.ensurePropertyMatchesPattern(validationErrors, "destination", path, DESTINATION_PATTERN); if (!validationErrors.isEmpty()) { throw new ScriptStorageException("Script errors", validationErrors); } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/FileDescriptor.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/FileDescriptor.java index 00e27277..95d3d4aa 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/FileDescriptor.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/FileDescriptor.java @@ -21,19 +21,12 @@ package com.cognifide.apm.core.scripts; import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; -public class FileDescriptor { +class FileDescriptor { private static final String SCRIPT_PATH = "/conf/apm/scripts"; - private static final Pattern FILE_NAME_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm"); - - private static final Pattern PATH_PATTERN = Pattern.compile("(/[0-9a-zA-Z_\\-]+)+"); - private final String path; private final String name; @@ -88,17 +81,4 @@ public String getName() { public InputStream getInputStream() { return inputStream; } - - public List validate() { - List errors = new ArrayList<>(); - ensurePropertyMatchesPattern(errors, "file name", name, FILE_NAME_PATTERN); - ensurePropertyMatchesPattern(errors, "file path", path, PATH_PATTERN); - return errors; - } - - private void ensurePropertyMatchesPattern(List errors, String property, String value, Pattern pattern) { - if (!pattern.matcher(value).matches()) { - errors.add(String.format("Invalid %s: \"%s\"", property, value)); - } - } } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java index b6a18886..c0fb506c 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java @@ -28,11 +28,13 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.time.OffsetDateTime; +import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; +import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.jcr.Binary; import javax.jcr.Node; @@ -58,6 +60,10 @@ public class ScriptStorageImpl implements ScriptStorage { private static final Logger LOG = LoggerFactory.getLogger(ScriptStorageImpl.class); + private static final Pattern FILE_NAME_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm"); + + private static final Pattern PATH_PATTERN = Pattern.compile("(/[0-9a-zA-Z_\\-]+)+"); + private static final Charset SCRIPT_ENCODING = StandardCharsets.UTF_8; @Reference @@ -153,10 +159,25 @@ private String generateFileName(String fileName, Node saveNode) throws Repositor private void validate(Collection fileDescriptors) { List validationErrors = fileDescriptors.stream() - .flatMap(fileDescriptor -> fileDescriptor.validate().stream()) + .flatMap(fileDescriptor -> validate(fileDescriptor).stream()) .collect(Collectors.toList()); if (!validationErrors.isEmpty()) { throw new ScriptStorageException("Script errors", validationErrors); } } + + private List validate(FileDescriptor file) { + List errors = new ArrayList<>(); + ensurePropertyMatchesPattern(errors, "file name", file.getName(), FILE_NAME_PATTERN); + ensurePropertyMatchesPattern(errors, "file path", file.getPath(), PATH_PATTERN); + return errors; + } + + public static void ensurePropertyMatchesPattern(List errors, String property, String value, + Pattern pattern) { + if (!pattern.matcher(value).matches()) { + errors.add(String.format("Invalid %s: \"%s\"", property, value)); + } + } + } From 978497465cbbeabf059370e722eec4e3a47a8491 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Sun, 5 Nov 2023 01:32:10 +0100 Subject: [PATCH 3/4] fixed script move/rename issue --- .../com/cognifide/apm/core/endpoints/ScriptMoveServlet.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java index 88ff7d84..2679170e 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java @@ -23,7 +23,6 @@ import com.cognifide.apm.core.Property; import com.cognifide.apm.core.endpoints.response.ResponseEntity; import com.cognifide.apm.core.endpoints.utils.RequestProcessor; -import com.cognifide.apm.core.scripts.ScriptStorageException; import com.cognifide.apm.core.scripts.ScriptStorageImpl; import com.day.cq.commons.jcr.JcrConstants; import com.day.cq.commons.jcr.JcrUtil; @@ -31,6 +30,7 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; +import java.util.stream.Collectors; import javax.jcr.Session; import javax.servlet.Servlet; import org.apache.commons.lang3.StringUtils; @@ -118,7 +118,9 @@ private void validate(String path, String name) { containsExtension(name) ? SCRIPT_PATTERN : FOLDER_PATTERN); ScriptStorageImpl.ensurePropertyMatchesPattern(validationErrors, "destination", path, DESTINATION_PATTERN); if (!validationErrors.isEmpty()) { - throw new ScriptStorageException("Script errors", validationErrors); + String message = validationErrors.stream() + .collect(Collectors.joining("\n", "Validation errors:\n", "")); + throw new RuntimeException(message); } } } From e44adcdba10afb7f4590a02c054b82636c3971f0 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Sun, 5 Nov 2023 23:41:50 +0100 Subject: [PATCH 4/4] fixed script move/rename issue --- .../apm/core/endpoints/ScriptMoveServlet.java | 6 ++++-- .../ScriptsResourceChangeListener.java | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java index 2679170e..671bc0d0 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java @@ -83,13 +83,15 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse destPath = createUniquePath(destPath, resourceResolver); session.move(form.getPath(), destPath); session.save(); + LOGGER.info("Item successfully moved from {} to {}", form.getPath(), destPath); } if (!containsExtension(form.getPath())) { ValueMap valueMap = resourceResolver.getResource(destPath).adaptTo(ModifiableValueMap.class); + String prevTitle = valueMap.get(JcrConstants.JCR_TITLE, String.class); valueMap.put(JcrConstants.JCR_TITLE, form.getRename()); + resourceResolver.commit(); + LOGGER.info("Item successfully renamed from {} to {}", prevTitle, form.getRename()); } - resourceResolver.commit(); - LOGGER.info("Item successfully moved from {} to {}", form.getPath(), destPath); return ResponseEntity.ok("Item successfully moved"); } catch (Exception e) { LOGGER.error("Errors while moving item", e); diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/services/ScriptsResourceChangeListener.java b/app/aem/core/src/main/java/com/cognifide/apm/core/services/ScriptsResourceChangeListener.java index 705c384e..78a2f6e0 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/services/ScriptsResourceChangeListener.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/services/ScriptsResourceChangeListener.java @@ -102,14 +102,17 @@ public void onChange(List changes) { SlingHelper.operateTraced(resolverProvider, resolver -> { // rename copy/paste folders - changes.stream() - .filter(change -> change.getType() == ResourceChange.ChangeType.ADDED) - .map(change -> resolver.getResource(change.getPath())) - .filter(ScriptsRowModel::isFolder) - .forEach(resource -> { - ValueMap valueMap = resource.adaptTo(ModifiableValueMap.class); - valueMap.put(JcrConstants.JCR_TITLE, resource.getName()); - }); + boolean onlyAdded = changes.stream() + .allMatch(change -> change.getType() == ResourceChange.ChangeType.ADDED); + if (onlyAdded) { + changes.stream() + .map(change -> resolver.getResource(change.getPath())) + .filter(ScriptsRowModel::isFolder) + .forEach(resource -> { + ValueMap valueMap = resource.adaptTo(ModifiableValueMap.class); + valueMap.put(JcrConstants.JCR_TITLE, resource.getName()); + }); + } //register schedule or cron expression scripts changes.stream() .filter(change -> StringUtils.endsWith(change.getPath(), Apm.FILE_EXT))