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

Commit c9c7892

Browse files
authored
Merge pull request #235 from marklogic-community/feature/232-set-config
#232 Added setAdminConfig and setManageConfig for easily rebuilding t…
2 parents 17cd923 + 0a9c46b commit c9c7892

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,49 @@ public ManageClient() {
3737
}
3838

3939
public ManageClient(ManageConfig config) {
40-
initialize(config);
40+
setManageConfig(config);
4141
}
4242

43-
public void initialize(ManageConfig config) {
44-
this.manageConfig = config;
45-
if (logger.isInfoEnabled()) {
46-
logger.info("Initializing ManageClient with manage config of: " + config);
47-
}
48-
this.restTemplate = RestTemplateUtil.newRestTemplate(config);
49-
50-
if (!config.getUsername().equals(config.getAdminUsername())) {
51-
if (logger.isInfoEnabled()) {
52-
logger.info("Initializing ManageClient with admin config, admin user: " + config.getAdminUsername());
53-
}
54-
55-
RestConfig rc = new RestConfig(config.getHost(), config.getPort(), config.getAdminUsername(), config.getAdminPassword());
56-
rc.setScheme(config.getScheme());
57-
rc.setConfigureSimpleSsl(config.isConfigureSimpleSsl());
58-
rc.setHostnameVerifier(config.getHostnameVerifier());
59-
rc.setSslContext(config.getSslContext());
60-
this.adminRestTemplate = RestTemplateUtil.newRestTemplate(rc);
61-
} else {
62-
this.adminRestTemplate = restTemplate;
63-
}
43+
/**
44+
* Use setManageConfig instead.
45+
*
46+
* @param config
47+
*/
48+
@Deprecated
49+
public void initialize(ManageConfig config) {
50+
setManageConfig(config);
51+
}
52+
53+
/**
54+
* Uses the given ManageConfig instance to construct a Spring RestTemplate for communicating with the Manage API.
55+
* In addition, if adminUsername on the ManageConfig instance differs from username, then a separate RestTemplate is
56+
* constructed for making calls to the Manage API that need an admin user (as noted elsewhere, these are usually
57+
* calls for creating users/roles/privileges that only need "manage-admin" and "security", but that's typically
58+
* an admin user).
59+
*
60+
* @param config
61+
*/
62+
public void setManageConfig(ManageConfig config) {
63+
this.manageConfig = config;
64+
if (logger.isInfoEnabled()) {
65+
logger.info("Initializing ManageClient with manage config of: " + config);
66+
}
67+
this.restTemplate = RestTemplateUtil.newRestTemplate(config);
68+
69+
if (!config.getUsername().equals(config.getAdminUsername())) {
70+
if (logger.isInfoEnabled()) {
71+
logger.info("Initializing ManageClient with admin config, admin user: " + config.getAdminUsername());
72+
}
73+
74+
RestConfig rc = new RestConfig(config.getHost(), config.getPort(), config.getAdminUsername(), config.getAdminPassword());
75+
rc.setScheme(config.getScheme());
76+
rc.setConfigureSimpleSsl(config.isConfigureSimpleSsl());
77+
rc.setHostnameVerifier(config.getHostnameVerifier());
78+
rc.setSslContext(config.getSslContext());
79+
this.adminRestTemplate = RestTemplateUtil.newRestTemplate(rc);
80+
} else {
81+
this.adminRestTemplate = restTemplate;
82+
}
6483
}
6584

6685
/**
@@ -243,15 +262,16 @@ public HttpEntity<String> buildXmlEntity(String xml) {
243262

244263
protected void logRequest(String path, String contentType, String method) {
245264
if (logger.isInfoEnabled()) {
246-
logger.info(String.format("Sending %s %s request as user '%s' to path: %s", contentType, method,
247-
manageConfig.getUsername(), path));
265+
String username = manageConfig != null ? manageConfig.getUsername() : "(unknown)";
266+
logger.info(String.format("Sending %s %s request as user '%s' to path: %s", contentType, method, username, path));
248267
}
249268
}
250269

251270
protected void logAdminRequest(String path, String contentType, String method) {
252271
if (logger.isInfoEnabled()) {
272+
String username = manageConfig != null ? manageConfig.getUsername() : "(unknown)";
253273
logger.info(String.format("Sending %s %s request as user with admin role '%s' to path: %s", contentType,
254-
method, manageConfig.getUsername(), path));
274+
method, username, path));
255275
}
256276
}
257277

src/main/java/com/marklogic/mgmt/admin/AdminManager.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ public AdminManager() {
2828
}
2929

3030
public AdminManager(AdminConfig adminConfig) {
31-
this.adminConfig = adminConfig;
32-
this.restTemplate = RestTemplateUtil.newRestTemplate(adminConfig);
31+
setAdminConfig(adminConfig);
32+
}
33+
34+
/**
35+
* Uses the given AdminConfig instance to construct a Spring RestTemplate for communicating with Manage API
36+
* endpoints on port 8001.
37+
*
38+
* @param adminConfig
39+
*/
40+
public void setAdminConfig(AdminConfig adminConfig) {
41+
this.adminConfig = adminConfig;
42+
this.restTemplate = RestTemplateUtil.newRestTemplate(adminConfig);
3343
}
3444

3545
public void init() {

0 commit comments

Comments
 (0)