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 .FileDescriptor ;
27
26
import com .cognifide .apm .core .scripts .ScriptStorageException ;
27
+ import com .cognifide .apm .core .scripts .ScriptStorageImpl ;
28
28
import com .day .cq .commons .jcr .JcrConstants ;
29
29
import com .day .cq .commons .jcr .JcrUtil ;
30
30
import java .io .IOException ;
31
+ import java .util .ArrayList ;
31
32
import java .util .List ;
33
+ import java .util .regex .Pattern ;
32
34
import javax .jcr .Session ;
33
35
import javax .servlet .Servlet ;
34
36
import org .apache .commons .lang3 .StringUtils ;
41
43
import org .apache .sling .models .factory .ModelFactory ;
42
44
import org .osgi .service .component .annotations .Component ;
43
45
import org .osgi .service .component .annotations .Reference ;
46
+ import org .slf4j .Logger ;
47
+ import org .slf4j .LoggerFactory ;
44
48
45
49
@ Component (
46
50
service = Servlet .class ,
53
57
)
54
58
public class ScriptMoveServlet extends SlingAllMethodsServlet {
55
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
+
56
68
@ Reference
57
69
private ModelFactory modelFactory ;
58
70
@@ -77,8 +89,10 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
77
89
valueMap .put (JcrConstants .JCR_TITLE , form .getRename ());
78
90
}
79
91
resourceResolver .commit ();
92
+ LOGGER .info ("Item successfully moved from {} to {}" , form .getPath (), destPath );
80
93
return ResponseEntity .ok ("Item successfully moved" );
81
94
} catch (Exception e ) {
95
+ LOGGER .error ("Errors while moving item" , e );
82
96
return ResponseEntity .badRequest (StringUtils .defaultString (e .getMessage (), "Errors while moving item" ));
83
97
}
84
98
});
@@ -99,7 +113,10 @@ private String createUniquePath(String pathWithExtension, ResourceResolver resol
99
113
}
100
114
101
115
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 );
103
120
if (!validationErrors .isEmpty ()) {
104
121
throw new ScriptStorageException ("Script errors" , validationErrors );
105
122
}
0 commit comments