3
3
import com .marklogic .appdeployer .command .AbstractResourceCommand ;
4
4
import com .marklogic .appdeployer .command .CommandContext ;
5
5
import com .marklogic .appdeployer .command .SortOrderConstants ;
6
+ import com .marklogic .mgmt .api .API ;
7
+ import com .marklogic .mgmt .api .security .User ;
8
+ import com .marklogic .mgmt .mapper .DefaultResourceMapper ;
9
+ import com .marklogic .mgmt .mapper .ResourceMapper ;
6
10
import com .marklogic .mgmt .resource .ResourceManager ;
7
11
import com .marklogic .mgmt .resource .security .UserManager ;
8
12
9
13
import java .io .File ;
10
14
15
+ /**
16
+ * As of version 3.9.0, this will now use CMA to deploy users if isOptimizeWithCma on the AppConfig object passed in
17
+ * via the CommandContext returns true.
18
+ */
11
19
public class DeployUsersCommand extends AbstractResourceCommand {
12
20
13
21
public DeployUsersCommand () {
@@ -24,4 +32,56 @@ protected ResourceManager getResourceManager(CommandContext context) {
24
32
return new UserManager (context .getManageClient ());
25
33
}
26
34
35
+ @ Override
36
+ protected void processExecuteOnResourceDir (CommandContext context , File resourceDir ) {
37
+ if (context .getAppConfig ().isOptimizeWithCma ()) {
38
+ deployUsersViaCma (context , resourceDir );
39
+ } else {
40
+ super .processExecuteOnResourceDir (context , resourceDir );
41
+ }
42
+ }
43
+
44
+ /**
45
+ * If deploying via CMA, then each user file is read in, unmarshalled into a User object, and then written out as
46
+ * JSON. This allows for both JSON and XML files to be supported.
47
+ *
48
+ * @param context
49
+ * @param resourceDir
50
+ */
51
+ protected void deployUsersViaCma (CommandContext context , File resourceDir ) {
52
+ if (resourceDir .exists ()) {
53
+ ResourceMapper resourceMapper = new DefaultResourceMapper (new API (context .getManageClient ()));
54
+
55
+ StringBuilder sb = new StringBuilder ("{\" config\" :[{\" user\" :[" );
56
+
57
+ boolean foundUser = false ;
58
+ for (File f : listFilesInDirectory (resourceDir , context )) {
59
+ if (logger .isInfoEnabled ()) {
60
+ logger .info ("Processing file: " + f .getAbsolutePath ());
61
+ }
62
+ String payload = copyFileToString (f , context );
63
+ User user = resourceMapper .readResource (payload , User .class );
64
+ if (foundUser ) {
65
+ sb .append ("," );
66
+ }
67
+ sb .append (user .getJson ());
68
+ foundUser = true ;
69
+ }
70
+
71
+ if (foundUser ) {
72
+ sb .append ("]}]}" );
73
+
74
+ // Not logging the payload because it can contain passwords
75
+ if (logger .isInfoEnabled ()) {
76
+ logger .info ("Submitting configuration containing users" );
77
+ }
78
+ context .getManageClient ().postJson ("/manage/v3" , sb .toString ());
79
+ if (logger .isInfoEnabled ()) {
80
+ logger .info ("Successfully submitted configuration containing users" );
81
+ }
82
+ }
83
+ } else {
84
+ logResourceDirectoryNotFound (resourceDir );
85
+ }
86
+ }
27
87
}
0 commit comments