23
23
import com .cognifide .apm .core .Property ;
24
24
import com .cognifide .apm .core .endpoints .response .ResponseEntity ;
25
25
import com .cognifide .apm .core .endpoints .utils .RequestProcessor ;
26
+ import com .cognifide .apm .core .scripts .ScriptStorageImpl ;
26
27
import com .day .cq .commons .jcr .JcrConstants ;
27
28
import com .day .cq .commons .jcr .JcrUtil ;
28
29
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 ;
29
34
import javax .jcr .Session ;
30
35
import javax .servlet .Servlet ;
31
36
import org .apache .commons .lang3 .StringUtils ;
38
43
import org .apache .sling .models .factory .ModelFactory ;
39
44
import org .osgi .service .component .annotations .Component ;
40
45
import org .osgi .service .component .annotations .Reference ;
46
+ import org .slf4j .Logger ;
47
+ import org .slf4j .LoggerFactory ;
41
48
42
49
@ Component (
43
50
service = Servlet .class ,
50
57
)
51
58
public class ScriptMoveServlet extends SlingAllMethodsServlet {
52
59
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
+
53
68
@ Reference
54
69
private ModelFactory modelFactory ;
55
70
@@ -62,19 +77,24 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
62
77
String rename = containsExtension (form .getPath ())
63
78
? (form .getRename () + (containsExtension (form .getRename ()) ? "" : Apm .FILE_EXT ))
64
79
: JcrUtil .createValidName (form .getRename ());
80
+ validate (dest , rename );
65
81
String destPath = String .format ("%s/%s" , dest , rename );
66
82
if (!StringUtils .equals (form .getPath (), destPath )) {
67
83
destPath = createUniquePath (destPath , resourceResolver );
68
84
session .move (form .getPath (), destPath );
69
85
session .save ();
86
+ LOGGER .info ("Item successfully moved from {} to {}" , form .getPath (), destPath );
70
87
}
71
88
if (!containsExtension (form .getPath ())) {
72
89
ValueMap valueMap = resourceResolver .getResource (destPath ).adaptTo (ModifiableValueMap .class );
90
+ String prevTitle = valueMap .get (JcrConstants .JCR_TITLE , String .class );
73
91
valueMap .put (JcrConstants .JCR_TITLE , form .getRename ());
92
+ resourceResolver .commit ();
93
+ LOGGER .info ("Item successfully renamed from {} to {}" , prevTitle , form .getRename ());
74
94
}
75
- resourceResolver .commit ();
76
95
return ResponseEntity .ok ("Item successfully moved" );
77
96
} catch (Exception e ) {
97
+ LOGGER .error ("Errors while moving item" , e );
78
98
return ResponseEntity .badRequest (StringUtils .defaultString (e .getMessage (), "Errors while moving item" ));
79
99
}
80
100
});
@@ -93,4 +113,16 @@ private String createUniquePath(String pathWithExtension, ResourceResolver resol
93
113
}
94
114
return path + (counter > 0 ? counter : "" ) + extension ;
95
115
}
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
+ }
96
128
}
0 commit comments