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

Commit 7150a36

Browse files
committed
#451 Protected collection names are now URL encoded
1 parent 2a1f30e commit 7150a36

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

src/main/java/com/marklogic/mgmt/resource/rebalancer/PartitionManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public ResponseEntity<String> takePartitionOffline(String partitionName) {
4646

4747
public PartitionProperties getPartitionProperties(String partitionName) {
4848
JsonNode json = getManageClient().getJsonNode(getPropertiesPath(partitionName));
49-
System.out.println("JSON: " + json);
5049
if (json != null && json.has("partition-properties")) {
5150
JsonNode props = json.get("partition-properties");
5251
try {

src/main/java/com/marklogic/mgmt/resource/security/ProtectedCollectionsManager.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.marklogic.mgmt.resource.AbstractResourceManager;
44
import com.marklogic.mgmt.ManageClient;
55

6+
import java.io.UnsupportedEncodingException;
7+
import java.net.URLEncoder;
8+
69
public class ProtectedCollectionsManager extends AbstractResourceManager {
710

811
public ProtectedCollectionsManager(ManageClient client) {
@@ -26,12 +29,28 @@ protected String getIdFieldName() {
2629

2730
@Override
2831
public String getPropertiesPath(String resourceNameOrId, String... resourceUrlParams) {
29-
return getResourcesPath() + "/properties?collection=" + resourceNameOrId;
32+
return getResourcesPath() + "/properties?collection=" + encodeCollectionName(resourceNameOrId);
3033
}
3134

3235
@Override
3336
public String getResourcePath(String resourceNameOrId, String... resourceUrlParams) {
34-
return getResourcesPath() + "?collection=" + resourceNameOrId;
37+
return getResourcesPath() + "?collection=" + encodeCollectionName(resourceNameOrId);
3538
}
3639

40+
/**
41+
* For most Manage API resources, MarkLogic prohibits characters that require URL encoding. Collection names are
42+
* an exception though and thus require URL encoding so that their names can be used in a querystring.
43+
*
44+
* @param collectionName
45+
* @return
46+
*/
47+
private String encodeCollectionName(String collectionName) {
48+
try {
49+
return URLEncoder.encode(collectionName, "utf-8");
50+
} catch (UnsupportedEncodingException e) {
51+
logger.warn(format("Unable to encode collection: %s; will include un-encoded collection name in " +
52+
"querystring; cause: %s", collectionName, e.getMessage()));
53+
return collectionName;
54+
}
55+
}
3756
}

src/test/java/com/marklogic/appdeployer/command/security/ManageProtectedCollectionsTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
public class ManageProtectedCollectionsTest extends AbstractManageResourceTest {
99

10-
@Override
11-
protected ResourceManager newResourceManager() {
12-
return new ProtectedCollectionsManager(manageClient);
13-
}
10+
@Override
11+
protected ResourceManager newResourceManager() {
12+
return new ProtectedCollectionsManager(manageClient);
13+
}
1414

15-
@Override
16-
protected Command newCommand() {
17-
return new DeployProtectedCollectionsCommand();
18-
}
15+
@Override
16+
protected Command newCommand() {
17+
return new DeployProtectedCollectionsCommand();
18+
}
1919

20-
@Override
21-
protected String[] getResourceNames() {
22-
return new String[] { "sample-app-collection", "http://example.org" };
23-
}
20+
@Override
21+
protected String[] getResourceNames() {
22+
return new String[]{"sample-app-collection#stuff", "http://example.org"};
23+
}
2424

2525
}

src/test/resources/sample-app/src/main/ml-config/security/protected-collections/collection-1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"collection": "sample-app-collection",
2+
"collection": "sample-app-collection#stuff",
33
"permission": [
44
{
55
"role-name": "view-admin",

0 commit comments

Comments
 (0)