use of javax.json.JsonArray in project sling by apache.
the class TeleporterHttpClient method verifyCorrectBundleState.
void verifyCorrectBundleState(String bundleSymbolicName, int timeoutInSeconds) throws IOException {
final String url = baseUrl + "/system/console/bundles/" + bundleSymbolicName + ".json";
final long end = System.currentTimeMillis() + timeoutInSeconds * 1000;
final ExponentialBackoffDelay d = new ExponentialBackoffDelay(50, 250);
while (System.currentTimeMillis() < end) {
String jsonBody = waitForStatus(url, 200, timeoutInSeconds * 1000);
// deserialize json (https://issues.apache.org/jira/browse/SLING-6536)
try (JsonReader jsonReader = Json.createReader(new StringReader(jsonBody))) {
// extract state
JsonArray jsonArray = jsonReader.readObject().getJsonArray("data");
if (jsonArray == null) {
throw new JsonException("Could not find 'data' array");
}
JsonObject bundleObject = jsonArray.getJsonObject(0);
String state = bundleObject.getString("state");
if ("Active".equals(state)) {
return;
}
// otherwise evaluate the import section
JsonArray propsArray = bundleObject.getJsonArray("props");
if (propsArray == null) {
throw new JsonException("Could not find 'props' object");
}
// iterate through all of them until key="Imported Packages" is found
for (JsonValue propValue : propsArray) {
if (propValue.getValueType().equals(ValueType.OBJECT)) {
JsonObject propObject = (JsonObject) propValue;
if ("Imported Packages".equals(propObject.getString("key"))) {
JsonArray importedPackagesArray = propObject.getJsonArray("value");
String reason = null;
for (JsonValue importedPackageValue : importedPackagesArray) {
if (importedPackageValue.getValueType().equals(ValueType.STRING)) {
String importedPackage = ((JsonString) importedPackageValue).getString();
if (importedPackage.startsWith("ERROR:")) {
reason = importedPackage;
}
}
}
// only if ERROR is found there is no more need to wait for the bundle to become active, otherwise it might just be started in the background
if (reason != null) {
throw new IllegalStateException("The test bundle '" + bundleSymbolicName + "' is in state '" + state + "'. This is due to unresolved import-packages: " + reason);
}
}
}
}
} catch (JsonException | IndexOutOfBoundsException e) {
throw new IllegalArgumentException("Test bundle '" + bundleSymbolicName + "' not correctly installed. Could not parse JSON response though to expose further information: " + jsonBody, e);
}
d.waitNextDelay();
}
throw new IOException("Bundle '" + bundleSymbolicName + "' was not started after " + timeoutInSeconds + " seconds. The check at " + url + " was not successfull. Probably some dependent bundle was not started.");
}
use of javax.json.JsonArray in project sling by apache.
the class AuthorizablePipe method addMembers.
/**
* Add to current authorizable (that should be a group) the configured members in addMembers expression
* @param auth group to which members should be added
*/
protected void addMembers(Authorizable auth) {
try {
if (auth.isGroup()) {
Group group = (Group) auth;
String uids = bindings.instantiateExpression(addMembers);
JsonArray array = JsonUtil.parseArray(uids);
for (int index = 0; index < array.size(); index++) {
String uid = array.getString(index);
Authorizable member = userManager.getAuthorizable(uid);
if (member != null) {
logger.info("adding {} to group {}", member.getID(), group.getID());
if (!isDryRun()) {
group.addMember(member);
}
} else {
logger.error("computed uid {} doesn't exist, doing nothing", uid);
}
}
} else {
logger.error("{} is not a group, can't add members", auth.getID());
}
} catch (Exception e) {
logger.error("unable to add members {}", addMembers, e);
}
}
use of javax.json.JsonArray in project sling by apache.
the class GetAclTest method testEffectiveAclMergeForUser_FewerPrivilegesGrantedOnChild.
/**
* Test for SLING-2600, Effective ACL servlet returns incorrect information
*/
@Test
public void testEffectiveAclMergeForUser_FewerPrivilegesGrantedOnChild() throws IOException, JsonException {
testUserId = H.createTestUser();
String testFolderUrl = H.createTestFolder("{ \"jcr:primaryType\": \"nt:unstructured\", \"propOne\" : \"propOneValue\", \"child\" : { \"childPropOne\" : true } }");
String postUrl = testFolderUrl + ".modifyAce.html";
//1. create an initial set of privileges
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("principalId", testUserId));
postParams.add(new NameValuePair("privilege@jcr:all", "granted"));
Credentials creds = new UsernamePasswordCredentials("admin", "admin");
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("principalId", testUserId));
postParams.add(new NameValuePair("privilege@jcr:write", "granted"));
postUrl = testFolderUrl + "/child.modifyAce.html";
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
//fetch the JSON for the eacl to verify the settings.
String getUrl = testFolderUrl + "/child.eacl.json";
String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
assertNotNull(json);
JsonObject jsonObject = JsonUtil.parseObject(json);
JsonObject aceObject = jsonObject.getJsonObject(testUserId);
assertNotNull(aceObject);
String principalString = aceObject.getString("principal");
assertEquals(testUserId, principalString);
JsonArray grantedArray = aceObject.getJsonArray("granted");
assertNotNull(grantedArray);
assertEquals(1, grantedArray.size());
Set<String> grantedPrivilegeNames = new HashSet<String>();
for (int i = 0; i < grantedArray.size(); i++) {
grantedPrivilegeNames.add(grantedArray.getString(i));
}
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:all");
Object deniedArray = aceObject.get("denied");
assertNull(deniedArray);
}
use of javax.json.JsonArray in project sling by apache.
the class GetAclTest method testEffectiveAclMergeForUser_SupersetOfPrivilegesDeniedOnChild.
/**
* Test for SLING-2600, Effective ACL servlet returns incorrect information
*/
@Test
public void testEffectiveAclMergeForUser_SupersetOfPrivilegesDeniedOnChild() throws IOException, JsonException {
testUserId = H.createTestUser();
String testFolderUrl = H.createTestFolder("{ \"jcr:primaryType\": \"nt:unstructured\", \"propOne\" : \"propOneValue\", \"child\" : { \"childPropOne\" : true } }");
String postUrl = testFolderUrl + ".modifyAce.html";
//1. create an initial set of privileges
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("principalId", testUserId));
postParams.add(new NameValuePair("privilege@jcr:write", "granted"));
Credentials creds = new UsernamePasswordCredentials("admin", "admin");
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("principalId", testUserId));
postParams.add(new NameValuePair("privilege@jcr:all", "denied"));
postUrl = testFolderUrl + "/child.modifyAce.html";
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
//fetch the JSON for the eacl to verify the settings.
String getUrl = testFolderUrl + "/child.eacl.json";
String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
assertNotNull(json);
JsonObject jsonObject = JsonUtil.parseObject(json);
JsonObject aceObject = jsonObject.getJsonObject(testUserId);
assertNotNull(aceObject);
String principalString = aceObject.getString("principal");
assertEquals(testUserId, principalString);
Object grantedArray = aceObject.get("granted");
assertNull(grantedArray);
JsonArray deniedArray = aceObject.getJsonArray("denied");
assertNotNull(deniedArray);
assertEquals(1, deniedArray.size());
Set<String> deniedPrivilegeNames = new HashSet<String>();
for (int i = 0; i < deniedArray.size(); i++) {
deniedPrivilegeNames.add(deniedArray.getString(i));
}
H.assertPrivilege(deniedPrivilegeNames, true, "jcr:all");
}
use of javax.json.JsonArray in project sling by apache.
the class GetAclTest method testEffectiveAclMergeForUser_SubsetOfPrivilegesDeniedOnChild.
/**
* Test for SLING-2600, Effective ACL servlet returns incorrect information
*/
@Test
// TODO: fails on Oak
@Ignore
public void testEffectiveAclMergeForUser_SubsetOfPrivilegesDeniedOnChild() throws IOException, JsonException {
testUserId = H.createTestUser();
String testFolderUrl = H.createTestFolder("{ \"jcr:primaryType\": \"nt:unstructured\", \"propOne\" : \"propOneValue\", \"child\" : { \"childPropOne\" : true } }");
String postUrl = testFolderUrl + ".modifyAce.html";
//1. create an initial set of privileges
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("principalId", testUserId));
postParams.add(new NameValuePair("privilege@jcr:all", "granted"));
Credentials creds = new UsernamePasswordCredentials("admin", "admin");
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("principalId", testUserId));
postParams.add(new NameValuePair("privilege@jcr:write", "denied"));
postUrl = testFolderUrl + "/child.modifyAce.html";
H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
//fetch the JSON for the eacl to verify the settings.
String getUrl = testFolderUrl + "/child.eacl.json";
String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
assertNotNull(json);
JsonObject jsonObject = JsonUtil.parseObject(json);
JsonObject aceObject = jsonObject.getJsonObject(testUserId);
assertNotNull(aceObject);
String principalString = aceObject.getString("principal");
assertEquals(testUserId, principalString);
JsonArray grantedArray = aceObject.getJsonArray("granted");
assertNotNull(grantedArray);
assertTrue(grantedArray.size() >= 8);
Set<String> grantedPrivilegeNames = new HashSet<String>();
for (int i = 0; i < grantedArray.size(); i++) {
grantedPrivilegeNames.add(grantedArray.getString(i));
}
H.assertPrivilege(grantedPrivilegeNames, false, "jcr:all");
H.assertPrivilege(grantedPrivilegeNames, false, "jcr:write");
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:read");
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:readAccessControl");
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:modifyAccessControl");
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:lockManagement");
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:versionManagement");
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:nodeTypeManagement");
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:retentionManagement");
H.assertPrivilege(grantedPrivilegeNames, true, "jcr:lifecycleManagement");
//jcr:write aggregate privileges should be denied
H.assertPrivilege(grantedPrivilegeNames, false, "jcr:modifyProperties");
H.assertPrivilege(grantedPrivilegeNames, false, "jcr:addChildNodes");
H.assertPrivilege(grantedPrivilegeNames, false, "jcr:removeNode");
H.assertPrivilege(grantedPrivilegeNames, false, "jcr:removeChildNodes");
JsonArray deniedArray = aceObject.getJsonArray("denied");
assertNotNull(deniedArray);
assertEquals(1, deniedArray.size());
Set<String> deniedPrivilegeNames = new HashSet<String>();
for (int i = 0; i < deniedArray.size(); i++) {
deniedPrivilegeNames.add(deniedArray.getString(i));
}
H.assertPrivilege(deniedPrivilegeNames, true, "jcr:write");
}
Aggregations