@@ -25,49 +25,83 @@ public class DeployRestApiServersCommand extends AbstractCommand implements Undo
25
25
private boolean deleteModulesDatabase = true ;
26
26
private boolean deleteContentDatabase = false ;
27
27
28
+ private String restApiFilename ;
29
+
28
30
public DeployRestApiServersCommand () {
29
31
setExecuteSortOrder (SortOrderConstants .DEPLOY_REST_API_SERVERS );
30
32
}
31
33
34
+ public DeployRestApiServersCommand (String restApiFilename ) {
35
+ this ();
36
+ this .restApiFilename = restApiFilename ;
37
+ }
38
+
32
39
public DeployRestApiServersCommand (boolean deleteContentDatabase ) {
33
40
this ();
34
41
this .deleteContentDatabase = deleteContentDatabase ;
35
42
}
36
43
44
+ public DeployRestApiServersCommand (String restApiFilename , boolean deleteContentDatabase ) {
45
+ this ();
46
+ this .restApiFilename = restApiFilename ;
47
+ this .deleteContentDatabase = deleteContentDatabase ;
48
+ }
49
+
37
50
@ Override
38
51
public Integer getUndoSortOrder () {
39
52
return SortOrderConstants .DELETE_REST_API_SERVERS ;
40
53
}
41
54
42
55
@ Override
43
56
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 );
46
72
if (f .exists ()) {
47
- payload = copyFileToString (f );
73
+ return copyFileToString (f );
48
74
} else {
49
75
logger .info (format ("Could not find REST API file at %s, will use default payload" , f .getAbsolutePath ()));
50
- payload = getDefaultRestApiPayload ();
76
+ return getDefaultRestApiPayload ();
51
77
}
78
+ }
52
79
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 ();
60
85
}
61
86
}
62
87
63
88
@ Override
64
89
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 ) {
65
101
final AppConfig appConfig = context .getAppConfig ();
66
102
final ManageClient manageClient = context .getManageClient ();
67
103
68
104
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
71
105
if (appConfig .isTestPortSet () && mgr .exists (appConfig .getTestRestServerName ())) {
72
106
mgr .setModulesDatabaseToDocuments (appConfig .getTestRestServerName ());
73
107
context .getAdminManager ().invokeActionRequiringRestart (new ActionRequiringRestart () {
@@ -78,13 +112,24 @@ public boolean execute() {
78
112
}
79
113
});
80
114
}
115
+ }
116
+
117
+ protected void deleteMainRestServer (CommandContext context ) {
118
+ final AppConfig appConfig = context .getAppConfig ();
119
+ final ManageClient manageClient = context .getManageClient ();
81
120
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 )) {
83
128
context .getAdminManager ().invokeActionRequiringRestart (new ActionRequiringRestart () {
84
129
@ Override
85
130
public boolean execute () {
86
- return deleteRestApi (appConfig . getRestServerName () , appConfig .getGroupName (), manageClient ,
87
- deleteModulesDatabase , deleteContentDatabase );
131
+ return deleteRestApi (serverName , appConfig .getGroupName (), manageClient , deleteModulesDatabase ,
132
+ deleteContentDatabase );
88
133
}
89
134
});
90
135
}
@@ -94,6 +139,11 @@ protected String getDefaultRestApiPayload() {
94
139
return RestApiUtil .buildDefaultRestApiJson ();
95
140
}
96
141
142
+ /**
143
+ * TODO Move this to RestApiManager.
144
+ *
145
+ * @return
146
+ */
97
147
protected boolean deleteRestApi (String serverName , String groupName , ManageClient manageClient ,
98
148
boolean includeModules , boolean includeContent ) {
99
149
if (new ServerManager (manageClient , groupName ).exists (serverName )) {
@@ -130,4 +180,12 @@ public void setDeleteContentDatabase(boolean includeContent) {
130
180
this .deleteContentDatabase = includeContent ;
131
181
}
132
182
183
+ public String getRestApiFilename () {
184
+ return restApiFilename ;
185
+ }
186
+
187
+ public void setRestApiFilename (String restApiFilename ) {
188
+ this .restApiFilename = restApiFilename ;
189
+ }
190
+
133
191
}
0 commit comments