@@ -25,49 +25,83 @@ public class DeployRestApiServersCommand extends AbstractCommand implements Undo
2525 private boolean deleteModulesDatabase = true ;
2626 private boolean deleteContentDatabase = false ;
2727
28+ private String restApiFilename ;
29+
2830 public DeployRestApiServersCommand () {
2931 setExecuteSortOrder (SortOrderConstants .DEPLOY_REST_API_SERVERS );
3032 }
3133
34+ public DeployRestApiServersCommand (String restApiFilename ) {
35+ this ();
36+ this .restApiFilename = restApiFilename ;
37+ }
38+
3239 public DeployRestApiServersCommand (boolean deleteContentDatabase ) {
3340 this ();
3441 this .deleteContentDatabase = deleteContentDatabase ;
3542 }
3643
44+ public DeployRestApiServersCommand (String restApiFilename , boolean deleteContentDatabase ) {
45+ this ();
46+ this .restApiFilename = restApiFilename ;
47+ this .deleteContentDatabase = deleteContentDatabase ;
48+ }
49+
3750 @ Override
3851 public Integer getUndoSortOrder () {
3952 return SortOrderConstants .DELETE_REST_API_SERVERS ;
4053 }
4154
4255 @ Override
4356 public void execute (CommandContext context ) {
44- File f = context .getAppConfig ().getConfigDir ().getRestApiFile ();
45- String payload = null ;
57+ String payload = getRestApiPayload (context );
58+ if (payload != null ) {
59+ RestApiManager mgr = new RestApiManager (context .getManageClient ());
60+ AppConfig appConfig = context .getAppConfig ();
61+
62+ mgr .createRestApi (tokenReplacer .replaceTokens (payload , appConfig , false ));
63+
64+ if (appConfig .isTestPortSet ()) {
65+ mgr .createRestApi (tokenReplacer .replaceTokens (payload , appConfig , true ));
66+ }
67+ }
68+ }
69+
70+ protected String getRestApiPayload (CommandContext context ) {
71+ File f = findRestApiConfigFile (context );
4672 if (f .exists ()) {
47- payload = copyFileToString (f );
73+ return copyFileToString (f );
4874 } else {
4975 logger .info (format ("Could not find REST API file at %s, will use default payload" , f .getAbsolutePath ()));
50- payload = getDefaultRestApiPayload ();
76+ return getDefaultRestApiPayload ();
5177 }
78+ }
5279
53- RestApiManager mgr = new RestApiManager (context .getManageClient ());
54- AppConfig appConfig = context .getAppConfig ();
55-
56- mgr .createRestApi (appConfig .getRestServerName (), tokenReplacer .replaceTokens (payload , appConfig , false ));
57-
58- if (appConfig .isTestPortSet ()) {
59- mgr .createRestApi (appConfig .getTestRestServerName (), tokenReplacer .replaceTokens (payload , appConfig , true ));
80+ protected File findRestApiConfigFile (CommandContext context ) {
81+ if (restApiFilename != null ) {
82+ return new File (context .getAppConfig ().getConfigDir ().getBaseDir (), restApiFilename );
83+ } else {
84+ return context .getAppConfig ().getConfigDir ().getRestApiFile ();
6085 }
6186 }
6287
6388 @ Override
6489 public void undo (CommandContext context ) {
90+ deleteTestRestServer (context );
91+ deleteMainRestServer (context );
92+ }
93+
94+ /**
95+ * If we have a test REST API, we first modify it to point at Documents for the modules database so we can safely
96+ * delete each REST API
97+ *
98+ * @param context
99+ */
100+ protected void deleteTestRestServer (CommandContext context ) {
65101 final AppConfig appConfig = context .getAppConfig ();
66102 final ManageClient manageClient = context .getManageClient ();
67103
68104 ServerManager mgr = new ServerManager (manageClient , appConfig .getGroupName ());
69- // If we have a test REST API, first modify it to point at Documents for the modules database so we can safely
70- // delete each REST API
71105 if (appConfig .isTestPortSet () && mgr .exists (appConfig .getTestRestServerName ())) {
72106 mgr .setModulesDatabaseToDocuments (appConfig .getTestRestServerName ());
73107 context .getAdminManager ().invokeActionRequiringRestart (new ActionRequiringRestart () {
@@ -78,13 +112,24 @@ public boolean execute() {
78112 }
79113 });
80114 }
115+ }
116+
117+ protected void deleteMainRestServer (CommandContext context ) {
118+ final AppConfig appConfig = context .getAppConfig ();
119+ final ManageClient manageClient = context .getManageClient ();
81120
82- if (mgr .exists (appConfig .getRestServerName ())) {
121+ ServerManager mgr = new ServerManager (manageClient , appConfig .getGroupName ());
122+
123+ String payload = getRestApiPayload (context );
124+ payload = tokenReplacer .replaceTokens (payload , appConfig , false );
125+ final String serverName = new RestApiManager (manageClient ).extractNameFromJson (payload );
126+
127+ if (mgr .exists (serverName )) {
83128 context .getAdminManager ().invokeActionRequiringRestart (new ActionRequiringRestart () {
84129 @ Override
85130 public boolean execute () {
86- return deleteRestApi (appConfig . getRestServerName () , appConfig .getGroupName (), manageClient ,
87- deleteModulesDatabase , deleteContentDatabase );
131+ return deleteRestApi (serverName , appConfig .getGroupName (), manageClient , deleteModulesDatabase ,
132+ deleteContentDatabase );
88133 }
89134 });
90135 }
@@ -94,6 +139,11 @@ protected String getDefaultRestApiPayload() {
94139 return RestApiUtil .buildDefaultRestApiJson ();
95140 }
96141
142+ /**
143+ * TODO Move this to RestApiManager.
144+ *
145+ * @return
146+ */
97147 protected boolean deleteRestApi (String serverName , String groupName , ManageClient manageClient ,
98148 boolean includeModules , boolean includeContent ) {
99149 if (new ServerManager (manageClient , groupName ).exists (serverName )) {
@@ -130,4 +180,12 @@ public void setDeleteContentDatabase(boolean includeContent) {
130180 this .deleteContentDatabase = includeContent ;
131181 }
132182
183+ public String getRestApiFilename () {
184+ return restApiFilename ;
185+ }
186+
187+ public void setRestApiFilename (String restApiFilename ) {
188+ this .restApiFilename = restApiFilename ;
189+ }
190+
133191}
0 commit comments