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

Commit 3850f0e

Browse files
committed
#256 Using servers endpoint to check for REST server existence
Instead of /v1/rest-apis
1 parent 42aa6d5 commit 3850f0e

File tree

8 files changed

+31
-59
lines changed

8 files changed

+31
-59
lines changed

src/main/java/com/marklogic/appdeployer/command/appservers/UpdateRestApiServersCommand.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import com.marklogic.appdeployer.command.AbstractCommand;
66
import com.marklogic.appdeployer.command.CommandContext;
77
import com.marklogic.appdeployer.command.SortOrderConstants;
8-
import com.marklogic.mgmt.PayloadParser;
9-
import com.marklogic.mgmt.SaveReceipt;
10-
import com.marklogic.mgmt.resource.ResourceManager;
118
import com.marklogic.mgmt.resource.appservers.ServerManager;
129

1310
import java.io.File;
@@ -51,22 +48,6 @@ public void execute(CommandContext context) {
5148
}
5249
}
5350

54-
@Override
55-
protected String adjustPayloadBeforeSavingResource(ResourceManager mgr, CommandContext context, File f, String payload) {
56-
payload = super.adjustPayloadBeforeSavingResource(mgr, context, f, payload);
57-
if (payload != null) {
58-
String urlRewriter = new PayloadParser().getPayloadFieldValue(payload, "url-rewriter", false);
59-
if (urlRewriter != null && !urlRewriter.contains("rest-api")) {
60-
logger.warn("The REST server is being updated with a url-rewriter that does not contain the string 'rest-api' in its URI. " +
61-
"This may result in a 'Port is in use' exception if the command for creating a REST server is run. " +
62-
"This is because the command to create a REST server first sends a GET to /v1/rest-apis, and that endpoint " +
63-
"only returns servers that have the string 'rest-api' in the URI of the url-rewriter. It is recommended to " +
64-
"include the string 'rest-api' in the URI of a url-rewriter for a REST server to avoid this problem.");
65-
}
66-
}
67-
return payload;
68-
}
69-
7051
protected File findRestApiConfigFile(CommandContext context) {
7152
if (restApiFilename != null) {
7253
File f = null;

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
import com.marklogic.mgmt.PayloadParser;
77
import com.marklogic.mgmt.resource.appservers.ServerManager;
88
import com.marklogic.mgmt.resource.databases.DatabaseManager;
9-
import com.marklogic.rest.util.Fragment;
109
import org.springframework.http.HttpMethod;
1110
import org.springframework.http.ResponseEntity;
12-
import org.springframework.web.client.HttpClientErrorException;
1311

1412
/**
1513
* For /v1/rest-apis. Currently only supports JSON files.
@@ -34,25 +32,9 @@ public ResponseEntity<String> createRestApi(String name, String json) {
3432
return null;
3533
} else {
3634
logger.info("Creating REST API: " + json);
37-
try {
38-
ResponseEntity<String> re = client.postJson("/v1/rest-apis", json);
39-
logger.info("Created REST API");
40-
return re;
41-
} catch (HttpClientErrorException ex) {
42-
logWarningIfErrorIsDueToPortInUse(ex);
43-
throw ex;
44-
}
45-
}
46-
}
47-
48-
protected void logWarningIfErrorIsDueToPortInUse(HttpClientErrorException ex) {
49-
String body = ex.getResponseBodyAsString();
50-
if (body != null && body.contains("Invalid parameter") && body.contains("is in use")) {
51-
logger.warn("Caught exception due to a port being in use; this may because the REST server already exists " +
52-
"but is using a rewriter that the /v1/rest-apis endpoint does not recognize as a REST rewriter, and thus " +
53-
"the /v1/rest-apis endpoint does not think that the REST server has been created yet. The error is then " +
54-
"because an attempt is made to create the REST server but it fails because the port is already in use. " +
55-
"To fix this, try modifying the URI of the rewriter module so it contains the string 'rest-api'.");
35+
ResponseEntity<String> re = client.postJson("/v1/rest-apis", json);
36+
logger.info("Created REST API");
37+
return re;
5638
}
5739
}
5840

@@ -61,9 +43,20 @@ public String extractNameFromJson(String json) {
6143
return node.get("rest-api").get("name").textValue();
6244
}
6345

46+
/**
47+
* Prior to ML 9.0-4, the /v1/rest-apis endpoint required that a server's url-rewriter have the string "rest-api"
48+
* somewhere in it. With 9.0-4, the url-rewriter must match the pattern:
49+
* <p>
50+
* ^/MarkLogic/rest-api/(8000-rewriter|rewriter|rewriter-noxdbc)\.xml$
51+
* <p>
52+
* It's not likely that a user's custom rewriter will fit that pattern, so this method no longer uses /v1/rest-apis,
53+
* opting to use ServerManager instead.
54+
*
55+
* @param name
56+
* @return
57+
*/
6458
public boolean restApiServerExists(String name) {
65-
Fragment f = client.getXml("/v1/rest-apis?format=xml", "rapi", "http://marklogic.com/rest-api");
66-
return f.elementExists(String.format("/rapi:rest-apis/rapi:rest-api[rapi:name = '%s']", name));
59+
return new ServerManager(client).exists(name);
6760
}
6861

6962
/**

src/test/java/com/marklogic/appdeployer/AbstractAppDeployerTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.marklogic.appdeployer.command.Command;
44
import com.marklogic.appdeployer.command.modules.DefaultModulesLoaderFactory;
55
import com.marklogic.appdeployer.command.modules.LoadModulesCommand;
6-
import com.marklogic.appdeployer.command.restapis.DeployRestApiServersCommand;
76
import com.marklogic.appdeployer.impl.SimpleAppDeployer;
87
import com.marklogic.client.ext.modulesloader.impl.DefaultModulesLoader;
98
import com.marklogic.mgmt.AbstractMgmtTest;
@@ -45,10 +44,6 @@ protected void initializeAppConfig() {
4544
appConfig.setRestAdminPassword(manageConfig.getPassword());
4645
}
4746

48-
protected void initializeAppDeployer() {
49-
initializeAppDeployer(new DeployRestApiServersCommand());
50-
}
51-
5247
/**
5348
* Initialize an AppDeployer with the given set of commands. Avoids having to create a Spring configuration.
5449
*

src/test/java/com/marklogic/appdeployer/command/ReplaceTokensTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void test() {
3232
loadModulesCommand.initializeDefaultModulesLoader(new CommandContext(appConfig, manageClient, adminManager));
3333
((DefaultModulesLoader) loadModulesCommand.getModulesLoader()).setModulesManager(null);
3434

35-
initializeAppDeployer(new DeployRestApiServersCommand(), loadModulesCommand);
35+
initializeAppDeployer(new DeployRestApiServersCommand(true), loadModulesCommand);
3636
deploySampleApp();
3737

3838
// We know xdbcEnabled was replaced, otherwise the deployment of the REST API server would have failed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.marklogic.appdeployer.AbstractAppDeployerTest;
44
import com.marklogic.appdeployer.ConfigDir;
55
import com.marklogic.appdeployer.command.appservers.UpdateRestApiServersCommand;
6+
import com.marklogic.mgmt.api.API;
67
import org.junit.After;
78
import org.junit.Test;
8-
import org.springframework.web.client.HttpClientErrorException;
99

1010
import java.io.File;
1111

@@ -17,18 +17,20 @@ public void teardown() {
1717
}
1818

1919
/**
20-
* This test is used to manually inspect the log statements that are written.
20+
* This was created for #242, but is being modified for #256 as this library is now using ServerManager instead
21+
* of /v1/rest-apis to see if a REST server exists already.
2122
*/
2223
@Test
2324
public void test() {
2425
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/rest-api-different-rewriter")));
2526

26-
initializeAppDeployer(new DeployRestApiServersCommand(), new UpdateRestApiServersCommand());
27+
initializeAppDeployer(new DeployRestApiServersCommand(true), new UpdateRestApiServersCommand());
2728

28-
try {
29-
deploySampleApp();
30-
} catch (HttpClientErrorException ex) {
31-
logger.info("Caught expected exception because the REST server has a url-rewriter without 'rest-api' in it: " + ex.getMessage());
32-
}
29+
deploySampleApp();
30+
assertEquals("/my/custom/rewriter.xml", new API(manageClient).server("sample-app").getUrlRewriter());
31+
32+
// This shouldn't throw an error, because it's no longer using /v1/rest-apis to see if the REST server exists
33+
deploySampleApp();
34+
assertEquals("/my/custom/rewriter.xml", new API(manageClient).server("sample-app").getUrlRewriter());
3335
}
3436
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void createAndDelete() {
2424
RestApiManager mgr = new RestApiManager(manageClient);
2525
ServerManager serverMgr = new ServerManager(manageClient, appConfig.getGroupName());
2626

27-
initializeAppDeployer(new DeployRestApiServersCommand());
27+
initializeAppDeployer(new DeployRestApiServersCommand(true));
2828
appDeployer.deploy(appConfig);
2929

3030
assertTrue("The REST API server should exist", mgr.restApiServerExists(SAMPLE_APP_NAME));

src/test/java/com/marklogic/appdeployer/command/servers/UpdateRestApiServersTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public void teardown() {
1919
@Test
2020
public void updateMainAndRestRestApiServers() {
2121
// Deploy a REST API server and a test one too
22-
initializeAppDeployer();
22+
initializeAppDeployer(new DeployRestApiServersCommand(true));
23+
2324
appConfig.setTestRestPort(SAMPLE_APP_TEST_REST_PORT);
2425
appDeployer.deploy(appConfig);
2526

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"server-name": "%%NAME%%",
3-
"url-rewriter": "/should/log/a-warning.xml"
3+
"url-rewriter": "/my/custom/rewriter.xml"
44
}

0 commit comments

Comments
 (0)