Skip to content

Commit c90b821

Browse files
committed
fixed script move/rename issue
1 parent 7710efd commit c90b821

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +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.FileDescriptor;
2726
import com.cognifide.apm.core.scripts.ScriptStorageException;
27+
import com.cognifide.apm.core.scripts.ScriptStorageImpl;
2828
import com.day.cq.commons.jcr.JcrConstants;
2929
import com.day.cq.commons.jcr.JcrUtil;
3030
import java.io.IOException;
31+
import java.util.ArrayList;
3132
import java.util.List;
33+
import java.util.regex.Pattern;
3234
import javax.jcr.Session;
3335
import javax.servlet.Servlet;
3436
import org.apache.commons.lang3.StringUtils;
@@ -41,6 +43,8 @@
4143
import org.apache.sling.models.factory.ModelFactory;
4244
import org.osgi.service.component.annotations.Component;
4345
import org.osgi.service.component.annotations.Reference;
46+
import org.slf4j.Logger;
47+
import org.slf4j.LoggerFactory;
4448

4549
@Component(
4650
service = Servlet.class,
@@ -53,6 +57,14 @@
5357
)
5458
public class ScriptMoveServlet extends SlingAllMethodsServlet {
5559

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+
5668
@Reference
5769
private ModelFactory modelFactory;
5870

@@ -77,8 +89,10 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
7789
valueMap.put(JcrConstants.JCR_TITLE, form.getRename());
7890
}
7991
resourceResolver.commit();
92+
LOGGER.info("Item successfully moved from {} to {}", form.getPath(), destPath);
8093
return ResponseEntity.ok("Item successfully moved");
8194
} catch (Exception e) {
95+
LOGGER.error("Errors while moving item", e);
8296
return ResponseEntity.badRequest(StringUtils.defaultString(e.getMessage(), "Errors while moving item"));
8397
}
8498
});
@@ -99,7 +113,10 @@ private String createUniquePath(String pathWithExtension, ResourceResolver resol
99113
}
100114

101115
private void validate(String path, String name) {
102-
List<String> validationErrors = new FileDescriptor(path, name, null).validate();
116+
List<String> validationErrors = new ArrayList<>();
117+
ScriptStorageImpl.ensurePropertyMatchesPattern(validationErrors, "rename", name,
118+
containsExtension(name) ? SCRIPT_PATTERN : FOLDER_PATTERN);
119+
ScriptStorageImpl.ensurePropertyMatchesPattern(validationErrors, "destination", path, DESTINATION_PATTERN);
103120
if (!validationErrors.isEmpty()) {
104121
throw new ScriptStorageException("Script errors", validationErrors);
105122
}

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,12 @@
2121
package com.cognifide.apm.core.scripts;
2222

2323
import java.io.InputStream;
24-
import java.util.ArrayList;
25-
import java.util.List;
26-
import java.util.regex.Pattern;
2724
import org.apache.commons.lang3.StringUtils;
2825

29-
public class FileDescriptor {
26+
class FileDescriptor {
3027

3128
private static final String SCRIPT_PATH = "/conf/apm/scripts";
3229

33-
private static final Pattern FILE_NAME_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm");
34-
35-
private static final Pattern PATH_PATTERN = Pattern.compile("(/[0-9a-zA-Z_\\-]+)+");
36-
3730
private final String path;
3831

3932
private final String name;
@@ -88,17 +81,4 @@ public String getName() {
8881
public InputStream getInputStream() {
8982
return inputStream;
9083
}
91-
92-
public List<String> validate() {
93-
List<String> errors = new ArrayList<>();
94-
ensurePropertyMatchesPattern(errors, "file name", name, FILE_NAME_PATTERN);
95-
ensurePropertyMatchesPattern(errors, "file path", path, PATH_PATTERN);
96-
return errors;
97-
}
98-
99-
private void ensurePropertyMatchesPattern(List<String> errors, String property, String value, Pattern pattern) {
100-
if (!pattern.matcher(value).matches()) {
101-
errors.add(String.format("Invalid %s: \"%s\"", property, value));
102-
}
103-
}
10484
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import java.nio.charset.Charset;
2929
import java.nio.charset.StandardCharsets;
3030
import java.time.OffsetDateTime;
31+
import java.util.ArrayList;
3132
import java.util.Calendar;
3233
import java.util.Collection;
3334
import java.util.Collections;
3435
import java.util.Date;
3536
import java.util.List;
37+
import java.util.regex.Pattern;
3638
import java.util.stream.Collectors;
3739
import javax.jcr.Binary;
3840
import javax.jcr.Node;
@@ -58,6 +60,10 @@ public class ScriptStorageImpl implements ScriptStorage {
5860

5961
private static final Logger LOG = LoggerFactory.getLogger(ScriptStorageImpl.class);
6062

63+
private static final Pattern FILE_NAME_PATTERN = Pattern.compile("[0-9a-zA-Z_\\-]+\\.apm");
64+
65+
private static final Pattern PATH_PATTERN = Pattern.compile("(/[0-9a-zA-Z_\\-]+)+");
66+
6167
private static final Charset SCRIPT_ENCODING = StandardCharsets.UTF_8;
6268

6369
@Reference
@@ -153,10 +159,25 @@ private String generateFileName(String fileName, Node saveNode) throws Repositor
153159

154160
private void validate(Collection<FileDescriptor> fileDescriptors) {
155161
List<String> validationErrors = fileDescriptors.stream()
156-
.flatMap(fileDescriptor -> fileDescriptor.validate().stream())
162+
.flatMap(fileDescriptor -> validate(fileDescriptor).stream())
157163
.collect(Collectors.toList());
158164
if (!validationErrors.isEmpty()) {
159165
throw new ScriptStorageException("Script errors", validationErrors);
160166
}
161167
}
168+
169+
private List<String> validate(FileDescriptor file) {
170+
List<String> errors = new ArrayList<>();
171+
ensurePropertyMatchesPattern(errors, "file name", file.getName(), FILE_NAME_PATTERN);
172+
ensurePropertyMatchesPattern(errors, "file path", file.getPath(), PATH_PATTERN);
173+
return errors;
174+
}
175+
176+
public static void ensurePropertyMatchesPattern(List<String> errors, String property, String value,
177+
Pattern pattern) {
178+
if (!pattern.matcher(value).matches()) {
179+
errors.add(String.format("Invalid %s: \"%s\"", property, value));
180+
}
181+
}
182+
162183
}

0 commit comments

Comments
 (0)