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

Commit 3ebcf5a

Browse files
committed
#242 Warning now logged for uri-rewriter without rest-api in it
1 parent d5e8f95 commit 3ebcf5a

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
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;
89
import com.marklogic.mgmt.SaveReceipt;
10+
import com.marklogic.mgmt.resource.ResourceManager;
911
import com.marklogic.mgmt.resource.appservers.ServerManager;
1012

1113
import java.io.File;
@@ -49,6 +51,22 @@ public void execute(CommandContext context) {
4951
}
5052
}
5153

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+
5270
protected File findRestApiConfigFile(CommandContext context) {
5371
if (restApiFilename != null) {
5472
File f = null;

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.marklogic.rest.util.Fragment;
1010
import org.springframework.http.HttpMethod;
1111
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.client.HttpClientErrorException;
1213

1314
/**
1415
* For /v1/rest-apis. Currently only supports JSON files.
@@ -33,9 +34,25 @@ public ResponseEntity<String> createRestApi(String name, String json) {
3334
return null;
3435
} else {
3536
logger.info("Creating REST API: " + json);
36-
ResponseEntity<String> re = client.postJson("/v1/rest-apis", json);
37-
logger.info("Created REST API");
38-
return re;
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'.");
3956
}
4057
}
4158

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.marklogic.appdeployer.command.restapis;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.appdeployer.ConfigDir;
5+
import com.marklogic.appdeployer.command.appservers.UpdateRestApiServersCommand;
6+
import org.junit.After;
7+
import org.junit.Test;
8+
import org.springframework.web.client.HttpClientErrorException;
9+
10+
import java.io.File;
11+
12+
public class CreateRestApiWithCustomRewriterTest extends AbstractAppDeployerTest {
13+
14+
@After
15+
public void teardown() {
16+
undeploySampleApp();
17+
}
18+
19+
/**
20+
* This test is used to manually inspect the log statements that are written.
21+
*/
22+
@Test
23+
public void test() {
24+
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/rest-api-different-rewriter")));
25+
26+
initializeAppDeployer(new DeployRestApiServersCommand(), new UpdateRestApiServersCommand());
27+
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+
}
33+
}
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"rest-api": {
3+
"name": "%%NAME%%",
4+
"group": "%%GROUP%%",
5+
"database": "%%DATABASE%%",
6+
"modules-database": "%%MODULES_DATABASE%%",
7+
"port": "%%PORT%%",
8+
"xdbc-enabled": true,
9+
"forests-per-host": 1,
10+
"error-format": "json"
11+
}
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"server-name": "%%NAME%%",
3+
"url-rewriter": "/should/log/a-warning.xml"
4+
}

0 commit comments

Comments
 (0)