33import com .marklogic .appdeployer .command .AbstractResourceCommand ;
44import com .marklogic .appdeployer .command .CommandContext ;
55import 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 ;
610import com .marklogic .mgmt .resource .ResourceManager ;
711import com .marklogic .mgmt .resource .security .UserManager ;
812
913import java .io .File ;
1014
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+ */
1119public class DeployUsersCommand extends AbstractResourceCommand {
1220
1321 public DeployUsersCommand () {
@@ -24,4 +32,56 @@ protected ResourceManager getResourceManager(CommandContext context) {
2432 return new UserManager (context .getManageClient ());
2533 }
2634
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+ }
2787}
0 commit comments