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

Commit 6092ce8

Browse files
committed
Providing helpful error context when connection fails
Intent is to identify the app server that RestTemplateUtil is trying to access, but also to no longer assume that a username is being used.
1 parent 46734b6 commit 6092ce8

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src/main/java/com/marklogic/mgmt/ManageConfig.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ public ManageConfig(ManageConfig other) {
6262
this.securitySslContext = other.securitySslContext;
6363
this.cleanJsonPayloads = other.cleanJsonPayloads;
6464
}
65-
@Override
66-
public String toString() {
67-
return String.format("[ManageConfig host: %s, port: %d, username: %s, security username: %s]", getHost(),
68-
getPort(), getUsername(), getSecurityUsername());
69-
}
7065

7166
public boolean isCleanJsonPayloads() {
7267
return cleanJsonPayloads;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public DatabaseClientBuilder newDatabaseClientBuilder() {
110110

111111
@Override
112112
public String toString() {
113-
return String.format("[scheme: %s, host: %s, port: %d, username: %s]", scheme, host, port, username);
113+
return String.format("%s://%shost:%d", getScheme(), getHost(), getPort());
114114
}
115115

116116
/**

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ public static RestTemplate newRestTemplate(String host, int port, String usernam
6464
* @return
6565
*/
6666
public static RestTemplate newRestTemplate(RestConfig config) {
67-
DatabaseClientFactory.Bean bean = config.newDatabaseClientBuilder().buildBean();
68-
OkHttpClient client = OkHttpClientBuilderFactory
69-
.newOkHttpClientBuilder(bean.getHost(), bean.getSecurityContext())
70-
.build();
67+
OkHttpClient client;
68+
try {
69+
DatabaseClientFactory.Bean bean = config.newDatabaseClientBuilder().buildBean();
70+
client = OkHttpClientBuilderFactory
71+
.newOkHttpClientBuilder(bean.getHost(), bean.getSecurityContext())
72+
.build();
73+
} catch (RuntimeException ex) {
74+
throw new RuntimeException(String.format("Unable to connect to the MarkLogic app server at %s; cause: %s", config.toString(), ex.getMessage()));
75+
}
7176

7277
RestTemplate rt = new RestTemplate(new OkHttp3ClientHttpRequestFactory(client));
7378
rt.setErrorHandler(new MgmtResponseErrorHandler());

src/test/java/com/marklogic/rest/util/RestTemplateUtilTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import static org.junit.jupiter.api.Assertions.assertEquals;
1414
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
import static org.junit.jupiter.api.Assertions.assertThrows;
1516
import static org.junit.jupiter.api.Assertions.assertTrue;
1617
import static org.junit.jupiter.api.Assertions.fail;
1718

@@ -117,4 +118,17 @@ public void defaultKeystoreWithInvalidAlgorithm() {
117118
logger.info("Caught expected exception: " + ex.getMessage());
118119
}
119120
}
121+
122+
@Test
123+
void noUsername() {
124+
manageConfig.setUsername(null);
125+
RuntimeException ex = assertThrows(RuntimeException.class,
126+
() -> RestTemplateUtil.newRestTemplate(manageConfig));
127+
128+
assertEquals("Unable to connect to the MarkLogic app server at http://localhosthost:8002; cause: username must be of type String",
129+
ex.getMessage(),
130+
"As of 4.5.0, since auth strategies other than basic/digest are now supported, the error message is expected " +
131+
"to identify which MarkLogic app server is being accessed but not any authentication details. This is " +
132+
"due to a change to toString of RestConfig/ManageConfig so that a username is not logged.");
133+
}
120134
}

0 commit comments

Comments
 (0)