Search in sources :

Example 46 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class GetAclTest method testEffectiveAclMergeForUser_ReplacePrivilegeOnChild.

/**
	 * Test for SLING-2600, Effective ACL servlet returns incorrect information
	 */
@Test
public void testEffectiveAclMergeForUser_ReplacePrivilegeOnChild() 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", "denied"));
    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:write");
    Object deniedArray = aceObject.get("denied");
    assertNull(deniedArray);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) HashSet(java.util.HashSet) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 47 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class ModifyAceTest method testMergeAceForUserCombineAggregatePrivilege.

/**
	 * Test for SLING-997, preserve privileges that were not posted with the modifyAce 
	 * request.
	 */
@Test
public void testMergeAceForUserCombineAggregatePrivilege() throws IOException, JsonException {
    testUserId = H.createTestUser();
    testFolderUrl = H.createTestFolder();
    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:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:removeNode", "denied"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertEquals(1, jsonObject.size());
    JsonObject aceObject = jsonObject.getJsonObject(testUserId);
    assertNotNull(aceObject);
    assertEquals(testUserId, aceObject.getString("principal"));
    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:read");
    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:removeNode");
    //2. post a new set of privileges to merge with the existing privileges
    List<NameValuePair> postParams2 = new ArrayList<NameValuePair>();
    postParams2.add(new NameValuePair("principalId", testUserId));
    //jcr:read is not posted, so it should remain in the granted ACE
    //deny the full jcr:write aggregate privilege, which should merge with the
    //existing part.
    //add a new privilege
    postParams2.add(new NameValuePair("privilege@jcr:write", "denied"));
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams2, null);
    //fetch the JSON for the acl to verify the settings.
    String json2 = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json2);
    JsonObject jsonObject2 = JsonUtil.parseObject(json2);
    assertEquals(1, jsonObject2.size());
    JsonObject aceObject2 = jsonObject2.getJsonObject(testUserId);
    assertNotNull(aceObject2);
    assertEquals(testUserId, aceObject.getString("principal"));
    JsonArray grantedArray2 = aceObject2.getJsonArray("granted");
    assertNotNull(grantedArray2);
    assertEquals(1, grantedArray2.size());
    Set<String> grantedPrivilegeNames2 = new HashSet<String>();
    for (int i = 0; i < grantedArray2.size(); i++) {
        grantedPrivilegeNames2.add(grantedArray2.getString(i));
    }
    H.assertPrivilege(grantedPrivilegeNames2, true, "jcr:read");
    JsonArray deniedArray2 = aceObject2.getJsonArray("denied");
    assertNotNull(deniedArray2);
    assertEquals(1, deniedArray2.size());
    Set<String> deniedPrivilegeNames2 = new HashSet<String>();
    for (int i = 0; i < deniedArray2.size(); i++) {
        deniedPrivilegeNames2.add(deniedArray2.getString(i));
    }
    H.assertPrivilege(deniedPrivilegeNames2, true, "jcr:write");
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) HashSet(java.util.HashSet) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 48 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class ModifyAceTest method testModifyAceForGroup.

@Test
public void testModifyAceForGroup() throws IOException, JsonException {
    testGroupId = H.createTestGroup();
    testFolderUrl = H.createTestFolder();
    String postUrl = testFolderUrl + ".modifyAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testGroupId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:write", "denied"));
    //invalid value should be ignored.
    postParams.add(new NameValuePair("privilege@jcr:modifyAccessControl", "bogus"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertEquals(1, jsonObject.size());
    JsonObject aceObject = jsonObject.getJsonObject(testGroupId);
    assertNotNull(aceObject);
    int order = aceObject.getInt("order");
    assertEquals(0, order);
    String principalString = aceObject.getString("principal");
    assertEquals(testGroupId, principalString);
    JsonArray grantedArray = aceObject.getJsonArray("granted");
    assertNotNull(grantedArray);
    assertEquals(1, grantedArray.size());
    assertEquals("jcr:read", grantedArray.getString(0));
    JsonArray deniedArray = aceObject.getJsonArray("denied");
    assertNotNull(deniedArray);
    assertEquals("jcr:write", deniedArray.getString(0));
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 49 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class ModifyAceTest method testMergeAceForUser.

/**
	 * Test for SLING-997, preserve privileges that were not posted with the modifyAce 
	 * request.
	 */
@Test
public void testMergeAceForUser() throws IOException, JsonException {
    testUserId = H.createTestUser();
    testFolderUrl = H.createTestFolder();
    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:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:readAccessControl", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:addChildNodes", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:modifyAccessControl", "denied"));
    postParams.add(new NameValuePair("privilege@jcr:removeChildNodes", "denied"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertEquals(1, jsonObject.size());
    JsonObject aceObject = jsonObject.getJsonObject(testUserId);
    assertNotNull(aceObject);
    String principalString = aceObject.getString("principal");
    assertEquals(testUserId, principalString);
    int order = aceObject.getInt("order");
    assertEquals(0, order);
    JsonArray grantedArray = aceObject.getJsonArray("granted");
    assertNotNull(grantedArray);
    assertEquals(3, 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:read");
    H.assertPrivilege(grantedPrivilegeNames, true, "jcr:readAccessControl");
    H.assertPrivilege(grantedPrivilegeNames, true, "jcr:addChildNodes");
    JsonArray deniedArray = aceObject.getJsonArray("denied");
    assertNotNull(deniedArray);
    assertEquals(2, 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:modifyAccessControl");
    H.assertPrivilege(deniedPrivilegeNames, true, "jcr:removeChildNodes");
    //2. post a new set of privileges to merge with the existing privileges
    List<NameValuePair> postParams2 = new ArrayList<NameValuePair>();
    postParams2.add(new NameValuePair("principalId", testUserId));
    //jcr:read and jcr:addChildNodes are not posted, so they should remain in the granted ACE
    //clear the existing privilege
    postParams2.add(new NameValuePair("privilege@jcr:readAccessControl", "none"));
    //add a new privilege
    postParams2.add(new NameValuePair("privilege@jcr:modifyProperties", "granted"));
    //jcr:modifyAccessControl is not posted, so it should remain in the denied ACE
    //deny the modifyAccessControl privilege
    postParams2.add(new NameValuePair("privilege@jcr:modifyAccessControl", "denied"));
    //clear the existing privilege
    postParams2.add(new NameValuePair("privilege@jcr:removeChildNodes", "none"));
    //deny a new privilege
    postParams2.add(new NameValuePair("privilege@jcr:removeNode", "denied"));
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams2, null);
    //fetch the JSON for the acl to verify the settings.
    String json2 = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json2);
    JsonObject jsonObject2 = JsonUtil.parseObject(json2);
    assertEquals(1, jsonObject2.size());
    JsonObject aceObject2 = jsonObject2.getJsonObject(testUserId);
    assertNotNull(aceObject2);
    String principalString2 = aceObject2.getString("principal");
    assertEquals(testUserId, principalString2);
    JsonArray grantedArray2 = aceObject2.getJsonArray("granted");
    assertNotNull(grantedArray2);
    assertEquals(3, grantedArray2.size());
    Set<String> grantedPrivilegeNames2 = new HashSet<String>();
    for (int i = 0; i < grantedArray2.size(); i++) {
        grantedPrivilegeNames2.add(grantedArray2.getString(i));
    }
    H.assertPrivilege(grantedPrivilegeNames2, true, "jcr:read");
    H.assertPrivilege(grantedPrivilegeNames2, true, "jcr:addChildNodes");
    H.assertPrivilege(grantedPrivilegeNames2, true, "jcr:modifyProperties");
    JsonArray deniedArray2 = aceObject2.getJsonArray("denied");
    assertNotNull(deniedArray2);
    assertEquals(2, deniedArray2.size());
    Set<String> deniedPrivilegeNames2 = new HashSet<String>();
    for (int i = 0; i < deniedArray2.size(); i++) {
        deniedPrivilegeNames2.add(deniedArray2.getString(i));
    }
    H.assertPrivilege(deniedPrivilegeNames2, true, "jcr:modifyAccessControl");
    H.assertPrivilege(deniedPrivilegeNames2, true, "jcr:removeNode");
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) HashSet(java.util.HashSet) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 50 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class PropertyRenderingTest method testMultiValuedTextJson.

public void testMultiValuedTextJson() throws Exception {
    final String json = getContent(displayUrl + "/multiText.json", CONTENT_TYPE_JSON);
    final JsonObject obj = JsonUtil.parseObject(json);
    assertEquals("[\"" + testMultiText1 + "\",\"" + testMultiText2 + "\"]", JsonUtil.toString(obj.getJsonArray("multiText")));
}
Also used : JsonObject(javax.json.JsonObject)

Aggregations

JsonObject (javax.json.JsonObject)302 Test (org.junit.Test)110 JsonArray (javax.json.JsonArray)97 HashMap (java.util.HashMap)68 StringReader (java.io.StringReader)66 ArrayList (java.util.ArrayList)58 Credentials (org.apache.commons.httpclient.Credentials)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)52 JsonString (javax.json.JsonString)50 JsonReader (javax.json.JsonReader)49 NameValuePair (org.apache.commons.httpclient.NameValuePair)43 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)41 HashSet (java.util.HashSet)20 Map (java.util.Map)20 JsonException (javax.json.JsonException)20 JsonObjectBuilder (javax.json.JsonObjectBuilder)20 Response (javax.ws.rs.core.Response)19 LinkedHashMap (java.util.LinkedHashMap)17 JsonValue (javax.json.JsonValue)14 StringWriter (java.io.StringWriter)13