Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 46686b2

Browse files
committed
Improving handling of deleting a REST API server
Unfortunately the scenario in question - when a user has changed their modules-database to the filesystem - cannot be fully supported yet due to a server bug. But this at least exposes a better error to the user, which is currently being obscured by the error due to the invalid MIME type from the call to DELETE /v1/rest-apis.
1 parent 7eb4d26 commit 46686b2

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = "com.marklogic"
11-
version = "4.5.2"
11+
version = "4.5-SNAPSHOT"
1212

1313
java {
1414
sourceCompatibility = 1.8

src/main/java/com/marklogic/mgmt/resource/restapis/RestApiManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,24 @@ public boolean deleteRestApi(RestApiDeletionRequest request) {
105105
PayloadParser parser = new PayloadParser();
106106

107107
if (request.isIncludeModules()) {
108+
boolean includeModules = true;
108109
if (request.isDeleteModulesReplicaForests()) {
109-
String modulesDatabase = parser.getPayloadFieldValue(payload, "modules-database");
110-
if (databaseManager.exists(modulesDatabase)) {
110+
String modulesDatabase = null;
111+
try {
112+
modulesDatabase = parser.getPayloadFieldValue(payload, "modules-database");
113+
} catch (Exception e) {
114+
logger.warn("Unable to get value of `modules-database`; will not be able to delete " +
115+
"modules database. This may be expected if the modules database has been set to " +
116+
"'filesystem' for the app server. Error: {}", e.getMessage());
117+
includeModules = false;
118+
}
119+
if (modulesDatabase != null && databaseManager.exists(modulesDatabase)) {
111120
databaseManager.deleteReplicaForests(modulesDatabase);
112121
}
113122
}
114-
path += "include=modules&";
123+
if (includeModules) {
124+
path += "include=modules&";
125+
}
115126
}
116127

117128
if (request.isIncludeContent()) {

src/main/java/com/marklogic/rest/util/MgmtResponseErrorHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
2020
import org.springframework.http.HttpStatus;
21+
import org.springframework.http.InvalidMediaTypeException;
2122
import org.springframework.http.client.ClientHttpResponse;
2223
import org.springframework.web.client.DefaultResponseErrorHandler;
2324
import org.springframework.web.client.HttpClientErrorException;
@@ -49,6 +50,14 @@ public void handleError(ClientHttpResponse response) throws IOException {
4950
logger.error(message);
5051
}
5152
throw ex;
53+
} catch (InvalidMediaTypeException ex) {
54+
// In at least one scenario - when deleting a REST API server whose modules database has been set to be
55+
// the filesystem (which is not a valid setup, but a user may still do it), MarkLogic returns a mime type
56+
// containing commas - e.g. "text/plain, application/json". And Spring does not like that and throws this
57+
// error. That obscures the actual error. So a runtime exception is thrown with the mime type error but
58+
// also the response body from MarkLogic, which will contain the actual error.
59+
String body = new String(getResponseBody(response));
60+
throw new RuntimeException("Unable to parse mime type: " + ex.getMessage() + "; response body from MarkLogic: " + body);
5261
}
5362
}
5463

src/test/java/com/marklogic/appdeployer/command/restapis/DeleteRestApiTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
import com.marklogic.appdeployer.AbstractAppDeployerTest;
1919
import com.marklogic.appdeployer.ConfigDir;
2020
import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand;
21+
import com.marklogic.mgmt.api.API;
22+
import com.marklogic.mgmt.api.database.Database;
23+
import com.marklogic.mgmt.api.server.Server;
2124
import com.marklogic.mgmt.resource.appservers.ServerManager;
2225
import com.marklogic.mgmt.resource.databases.DatabaseManager;
2326
import com.marklogic.mgmt.resource.restapis.RestApiManager;
27+
import org.junit.jupiter.api.Disabled;
2428
import org.junit.jupiter.api.Test;
2529

2630
import java.io.File;
@@ -50,6 +54,28 @@ public void createAndDelete() {
5054
assertFalse(serverMgr.exists(SAMPLE_APP_NAME), "The REST API app server have been deleted");
5155
}
5256

57+
@Test
58+
@Disabled("This fails due to a server bug - BUG-60358")
59+
void deleteWithFilesystemAsModulesDatabase() {
60+
initializeAppDeployer(new DeployRestApiServersCommand(true));
61+
appDeployer.deploy(appConfig);
62+
63+
API api = new API(manageClient);
64+
65+
// Set the modules-database to the "filesystem", which will cause the DELETE to /v1/rest-apis/to fail due to
66+
// the server bug.
67+
Server server = new Server(api, appConfig.getRestServerName());
68+
server.setModulesDatabase("0");
69+
server.save();
70+
71+
try {
72+
appDeployer.undeploy(appConfig);
73+
} finally {
74+
// Delete the modules-database to ensure it's not left around
75+
new Database(api, appConfig.getModulesDatabaseName()).delete();
76+
}
77+
}
78+
5379
@Test
5480
public void contentDatabaseCommandAndRestApiCommandConfiguredToDeleteContent() {
5581
DatabaseManager dbMgr = new DatabaseManager(manageClient);

0 commit comments

Comments
 (0)