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

Commit b20d0cd

Browse files
committed
#174 Handling role with permission referring to itself
1 parent b767c6e commit b20d0cd

File tree

13 files changed

+382
-179
lines changed

13 files changed

+382
-179
lines changed

src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected SaveReceipt saveResource(ResourceManager mgr, CommandContext context,
116116
* @param context
117117
*/
118118
protected void storeTokenForResourceId(SaveReceipt receipt, CommandContext context) {
119-
URI location = receipt.getResponse().getHeaders().getLocation();
119+
URI location = receipt.getResponse() != null ? receipt.getResponse().getHeaders().getLocation() : null;
120120

121121
String idValue = null;
122122
String resourceName = null;

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.marklogic.rest.util.ResourcesFragment;
55
import org.springframework.http.ResponseEntity;
66

7+
import javax.xml.ws.Response;
8+
79
/**
810
* This class makes a number of assumptions in order to simplify the implementation of common operations for a MarkLogic
911
* management resource. Feel free to override the methods in here in a subclass when those assumptions don't work for a
@@ -75,30 +77,41 @@ public String getPropertiesAsJson(String resourceNameOrId, String... resourceUrl
7577
*/
7678
public SaveReceipt save(String payload) {
7779
String resourceId = getResourceId(payload);
78-
String label = getResourceName();
79-
String path = null;
80-
ResponseEntity<String> response = null;
8180
if (exists(resourceId)) {
8281
if (updateAllowed) {
8382
return updateResource(payload, resourceId);
8483
} else {
8584
logger.info("Resource already exists and updates are not supported, so not updating: " + resourceId);
85+
/**
86+
* Prior to version 2.9.0, this was always returning a non-null SaveReceipt with a null path and null
87+
* response. Not sure of the consequences of returning null instead, so keeping this behavior for now.
88+
*/
89+
return new SaveReceipt(resourceId, payload, null, null);
8690
}
8791
} else {
88-
logger.info(format("Creating %s: %s", label, resourceId));
89-
path = getCreateResourcePath(payload);
90-
response = postPayload(manageClient, path, payload);
91-
logger.info(format("Created %s: %s", label, resourceId));
92+
return createNewResource(payload, resourceId);
9293
}
93-
return new SaveReceipt(resourceId, payload, path, response);
94+
}
95+
96+
protected SaveReceipt createNewResource(String payload, String resourceId) {
97+
String label = getResourceName();
98+
if (logger.isInfoEnabled()) {
99+
logger.info(format("Creating %s: %s", label, resourceId));
100+
}
101+
String path = getCreateResourcePath(payload);
102+
ResponseEntity<String> response = postPayload(manageClient, path, payload);
103+
if (logger.isInfoEnabled()) {
104+
logger.info(format("Created %s: %s", label, resourceId));
105+
}
106+
return new SaveReceipt(resourceId, payload, path, response);
94107
}
95108

96109
/**
97110
* Mimetypes are likely to have a "+" in them, which the Management REST API won't support in a path - it needs to
98111
* be encoded. Other resources could have a "+" in their ID value as well. However, doing a full encoding doesn't
99112
* seem to be a great idea, as that will e.g. encode a forward slash in a mimetype as well, which will result in a
100113
* 404.
101-
*
114+
*
102115
* @param idValue
103116
* @return
104117
*/
@@ -109,7 +122,7 @@ protected String encodeResourceId(String idValue) {
109122
/**
110123
* Most clients should just use the save method, but this is public for scenarios where a client knows an update
111124
* should be performed.
112-
*
125+
*
113126
* @param payload
114127
* @param resourceId
115128
* @return
@@ -159,7 +172,7 @@ protected void beforeDelete(String resourceId, String path, String... resourceUr
159172

160173
/**
161174
* Convenience method for performing a delete once the correct path for the resource has been constructed.
162-
*
175+
*
163176
* @param path
164177
*/
165178
public void deleteAtPath(String path) {
@@ -175,7 +188,7 @@ public void deleteAtPath(String path) {
175188

176189
/**
177190
* TODO Could use something nicer here, particularly to properly encode the parameter values.
178-
*
191+
*
179192
* @param path
180193
* @param paramsAndValues
181194
* @return
@@ -203,7 +216,7 @@ protected String appendParamsAndValuesToPath(String path, String... paramsAndVal
203216

204217
/**
205218
* Can be overridden by subclass to provide custom querystring parameters.
206-
*
219+
*
207220
* @param payload
208221
* XML or JSON payload
209222
* @return
@@ -214,7 +227,7 @@ protected String[] getUpdateResourceParams(String payload) {
214227

215228
/**
216229
* Defaults to the "update" resource parameters.
217-
*
230+
*
218231
* @param payload
219232
* XML or JSON payload
220233
* @return

src/main/java/com/marklogic/mgmt/api/API.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ public API(ManageClient client, ObjectMapper mapper) {
7474
}
7575

7676
protected void initializeAdminManager() {
77-
ManageConfig mc = manageClient.getManageConfig();
78-
if (mc.getAdminUsername() != null && mc.getAdminPassword() != null) {
79-
AdminConfig ac = new AdminConfig(mc.getHost(), 8001, mc.getAdminUsername(), mc.getAdminPassword());
80-
this.adminManager = new AdminManager(ac);
81-
}
77+
if (manageClient != null) {
78+
ManageConfig mc = manageClient.getManageConfig();
79+
if (mc.getAdminUsername() != null && mc.getAdminPassword() != null) {
80+
AdminConfig ac = new AdminConfig(mc.getHost(), 8001, mc.getAdminUsername(), mc.getAdminPassword());
81+
this.adminManager = new AdminManager(ac);
82+
}
83+
}
8284
}
8385

8486
protected ObjectMapper buildDefaultObjectMapper() {
@@ -96,7 +98,7 @@ protected ObjectMapper buildDefaultObjectMapper() {
9698
* Connect to a (presumably) different host, using the same username/password combos that were used for the same
9799
* connection. Useful in a development environment where you may have multiple clusters with the same admin
98100
* username/password combo, and you want to switch quickly from one to another.
99-
*
101+
*
100102
* @param host
101103
*/
102104
public void connect(String host) {
@@ -107,7 +109,7 @@ public void connect(String host) {
107109
/**
108110
* Connect to a (presumably) different MarkLogic Management API. The username/password are assumed to work for both
109111
* the Management API and the Admin API on port 8001.
110-
*
112+
*
111113
* @param host
112114
* @param username
113115
* @param password
@@ -118,7 +120,7 @@ public void connect(String host, String username, String password) {
118120

119121
/**
120122
* Connect to a (presumably) different MarkLogic Management API.
121-
*
123+
*
122124
* @param host
123125
* @param username
124126
* @param password
@@ -140,7 +142,7 @@ public void connect(String host, String username, String password, String adminU
140142

141143
/**
142144
* Constructs a new ClientHelper, using newClient().
143-
*
145+
*
144146
* @return
145147
*/
146148
public ClientHelper clientHelper() {
@@ -150,7 +152,7 @@ public ClientHelper clientHelper() {
150152
/**
151153
* Constructs a new DatabaseClient, using system properties to create an AppConfig instance which is used to create
152154
* a DatabaseClient.
153-
*
155+
*
154156
* @return
155157
*/
156158
public DatabaseClient newClient() {
@@ -159,7 +161,7 @@ public DatabaseClient newClient() {
159161

160162
/**
161163
* Construct a new DatabaseClient, assuming DIGEST authentication.
162-
*
164+
*
163165
* @param host
164166
* @param port
165167
* @param user
@@ -203,7 +205,7 @@ public String createRestApi(String name, Integer port) {
203205
/**
204206
* Convenience method since the "Default" group is usually the group that's wanted (and often the only one that
205207
* exists).
206-
*
208+
*
207209
* @return
208210
*/
209211
public Group group() {
@@ -222,7 +224,7 @@ public Group getGroup() {
222224
/**
223225
* The vast majority of the time, configuring trace events will be on the default group, so a convenience method is
224226
* exposed for this use case.
225-
*
227+
*
226228
* @param events
227229
*/
228230
public void trace(String... events) {
@@ -232,7 +234,7 @@ public void trace(String... events) {
232234
/**
233235
* The vast majority of the time, configuring trace events will be on the default group, so a convenience method is
234236
* exposed for this use case.
235-
*
237+
*
236238
* @param events
237239
*/
238240
public void untrace(String... events) {
@@ -276,7 +278,7 @@ public Server getServer() {
276278
* In order to get an amp, we need up to 4 things to uniquely identify based on
277279
* http://docs.marklogic.com/REST/GET/manage/v2/amps/[id-or-name] . TODO Could make this nicer, where a call is made
278280
* to the amps endpoint to find an amp with the given local name, and if only one exists, just use that.
279-
*
281+
*
280282
* @param localName
281283
* @return
282284
*/

src/main/java/com/marklogic/mgmt/api/Resource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public String save() {
4343
logger.info(format("Saved %s %s", name, label));
4444
}
4545
return format("[Path: %s; Resource ID: %s; HTTP status: %s]", receipt.getPath(), receipt.getResourceId(),
46-
receipt.getResponse().getStatusCode());
46+
receipt.getResponse() != null ? receipt.getResponse().getStatusCode() : "(none)");
4747
}
4848

4949
/**
@@ -68,7 +68,7 @@ public String delete() {
6868
* TODO Not totally convinced about putting this method here, as it means that an instance of this is needed to get
6969
* a list of names. The other choices would be a method on the API class, but then that means the API class needs a
7070
* method per resource for getting a list of names, whereas with this approach, we have a single method.
71-
*
71+
*
7272
* @return a list of names of all resources of this type.
7373
*/
7474
public List<String> list() {
@@ -87,7 +87,7 @@ public boolean exists() {
8787
/**
8888
* Some resources, such as amps, require additional parameters in the URL to uniquely identify the resource. A
8989
* subclass can override this to provide those parameters.
90-
*
90+
*
9191
* @return
9292
*/
9393
public String[] getResourceUrlParams() {
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
package com.marklogic.mgmt.api.security;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import javax.xml.bind.annotation.XmlAccessType;
6+
import javax.xml.bind.annotation.XmlAccessorType;
7+
import javax.xml.bind.annotation.XmlElement;
8+
9+
@XmlAccessorType(XmlAccessType.FIELD)
310
public class Permission {
411

5-
private String roleName;
6-
private String capability;
12+
@JsonProperty("role-name")
13+
@XmlElement(name = "role-name")
14+
private String roleName;
15+
private String capability;
716

8-
public String getRoleName() {
9-
return roleName;
10-
}
17+
public String getRoleName() {
18+
return roleName;
19+
}
1120

12-
public void setRoleName(String roleName) {
13-
this.roleName = roleName;
14-
}
21+
public void setRoleName(String roleName) {
22+
this.roleName = roleName;
23+
}
1524

16-
public String getCapability() {
17-
return capability;
18-
}
25+
public String getCapability() {
26+
return capability;
27+
}
1928

20-
public void setCapability(String capability) {
21-
this.capability = capability;
22-
}
29+
public void setCapability(String capability) {
30+
this.capability = capability;
31+
}
2332

2433
}

0 commit comments

Comments
 (0)