Skip to content

Commit abbe48d

Browse files
authored
Merge pull request #432 from wttech/fix-move-issue
fixed script move/rename issue
2 parents a95e13c + e44adcd commit abbe48d

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptMoveServlet.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
import com.cognifide.apm.core.Property;
2424
import com.cognifide.apm.core.endpoints.response.ResponseEntity;
2525
import com.cognifide.apm.core.endpoints.utils.RequestProcessor;
26+
import com.cognifide.apm.core.scripts.ScriptStorageImpl;
2627
import com.day.cq.commons.jcr.JcrConstants;
2728
import com.day.cq.commons.jcr.JcrUtil;
2829
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
import java.util.regex.Pattern;
33+
import java.util.stream.Collectors;
2934
import javax.jcr.Session;
3035
import javax.servlet.Servlet;
3136
import org.apache.commons.lang3.StringUtils;
@@ -38,6 +43,8 @@
3843
import org.apache.sling.models.factory.ModelFactory;
3944
import org.osgi.service.component.annotations.Component;
4045
import org.osgi.service.component.annotations.Reference;
46+
import org.slf4j.Logger;
47+
import org.slf4j.LoggerFactory;
4148

4249
@Component(
4350
service = Servlet.class,
@@ -50,6 +57,14 @@
5057
)
5158
public class ScriptMoveServlet extends SlingAllMethodsServlet {
5259

60+
private static final Logger LOGGER = LoggerFactory.getLogger(ScriptMoveServlet.class);
61+
62+
private static final Pattern SCRIPT_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm");
63+
64+
private static final Pattern FOLDER_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+");
65+
66+
private static final Pattern DESTINATION_PATTERN = Pattern.compile("(/[0-9a-zA-Z_\\-]+)+");
67+
5368
@Reference
5469
private ModelFactory modelFactory;
5570

@@ -62,19 +77,24 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
6277
String rename = containsExtension(form.getPath())
6378
? (form.getRename() + (containsExtension(form.getRename()) ? "" : Apm.FILE_EXT))
6479
: JcrUtil.createValidName(form.getRename());
80+
validate(dest, rename);
6581
String destPath = String.format("%s/%s", dest, rename);
6682
if (!StringUtils.equals(form.getPath(), destPath)) {
6783
destPath = createUniquePath(destPath, resourceResolver);
6884
session.move(form.getPath(), destPath);
6985
session.save();
86+
LOGGER.info("Item successfully moved from {} to {}", form.getPath(), destPath);
7087
}
7188
if (!containsExtension(form.getPath())) {
7289
ValueMap valueMap = resourceResolver.getResource(destPath).adaptTo(ModifiableValueMap.class);
90+
String prevTitle = valueMap.get(JcrConstants.JCR_TITLE, String.class);
7391
valueMap.put(JcrConstants.JCR_TITLE, form.getRename());
92+
resourceResolver.commit();
93+
LOGGER.info("Item successfully renamed from {} to {}", prevTitle, form.getRename());
7494
}
75-
resourceResolver.commit();
7695
return ResponseEntity.ok("Item successfully moved");
7796
} catch (Exception e) {
97+
LOGGER.error("Errors while moving item", e);
7898
return ResponseEntity.badRequest(StringUtils.defaultString(e.getMessage(), "Errors while moving item"));
7999
}
80100
});
@@ -93,4 +113,16 @@ private String createUniquePath(String pathWithExtension, ResourceResolver resol
93113
}
94114
return path + (counter > 0 ? counter : "") + extension;
95115
}
116+
117+
private void validate(String path, String name) {
118+
List<String> validationErrors = new ArrayList<>();
119+
ScriptStorageImpl.ensurePropertyMatchesPattern(validationErrors, "rename", name,
120+
containsExtension(name) ? SCRIPT_PATTERN : FOLDER_PATTERN);
121+
ScriptStorageImpl.ensurePropertyMatchesPattern(validationErrors, "destination", path, DESTINATION_PATTERN);
122+
if (!validationErrors.isEmpty()) {
123+
String message = validationErrors.stream()
124+
.collect(Collectors.joining("\n", "Validation errors:\n", ""));
125+
throw new RuntimeException(message);
126+
}
127+
}
96128
}

app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class ScriptStorageImpl implements ScriptStorage {
6262

6363
private static final Pattern FILE_NAME_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm");
6464

65-
private static final Pattern PATH_PATTERN = Pattern.compile("/[0-9a-zA-Z_\\-/]+");
65+
private static final Pattern PATH_PATTERN = Pattern.compile("(/[0-9a-zA-Z_\\-]+)+");
6666

6767
private static final Charset SCRIPT_ENCODING = StandardCharsets.UTF_8;
6868

@@ -173,7 +173,7 @@ private List<String> validate(FileDescriptor file) {
173173
return errors;
174174
}
175175

176-
private static void ensurePropertyMatchesPattern(List<String> errors, String property, String value,
176+
public static void ensurePropertyMatchesPattern(List<String> errors, String property, String value,
177177
Pattern pattern) {
178178
if (!pattern.matcher(value).matches()) {
179179
errors.add(String.format("Invalid %s: \"%s\"", property, value));

app/aem/core/src/main/java/com/cognifide/apm/core/services/ScriptsResourceChangeListener.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,17 @@ public void onChange(List<ResourceChange> changes) {
102102

103103
SlingHelper.operateTraced(resolverProvider, resolver -> {
104104
// rename copy/paste folders
105-
changes.stream()
106-
.filter(change -> change.getType() == ResourceChange.ChangeType.ADDED)
107-
.map(change -> resolver.getResource(change.getPath()))
108-
.filter(ScriptsRowModel::isFolder)
109-
.forEach(resource -> {
110-
ValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
111-
valueMap.put(JcrConstants.JCR_TITLE, resource.getName());
112-
});
105+
boolean onlyAdded = changes.stream()
106+
.allMatch(change -> change.getType() == ResourceChange.ChangeType.ADDED);
107+
if (onlyAdded) {
108+
changes.stream()
109+
.map(change -> resolver.getResource(change.getPath()))
110+
.filter(ScriptsRowModel::isFolder)
111+
.forEach(resource -> {
112+
ValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
113+
valueMap.put(JcrConstants.JCR_TITLE, resource.getName());
114+
});
115+
}
113116
//register schedule or cron expression scripts
114117
changes.stream()
115118
.filter(change -> StringUtils.endsWith(change.getPath(), Apm.FILE_EXT))

0 commit comments

Comments
 (0)