Search in sources :

Example 21 with JsonArray

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

the class OrderedJsonReader method writeChildren.

@Override
protected void writeChildren(JsonObject obj, ContentCreator contentCreator) throws RepositoryException {
    if (!obj.containsKey(PN_ORDEREDCHILDREN)) {
        super.writeChildren(obj, contentCreator);
    } else {
        for (Map.Entry<String, JsonValue> entry : obj.entrySet()) {
            final String n = entry.getKey();
            // skip well known objects
            if (!ignoredNames.contains(n)) {
                Object o = entry.getValue();
                if (!handleSecurity(n, o, contentCreator)) {
                    if (n.equals(PN_ORDEREDCHILDREN)) {
                        if (o instanceof JsonArray) {
                            JsonArray children = (JsonArray) o;
                            for (int childIndex = 0; childIndex < children.size(); childIndex++) {
                                Object oc = children.get(childIndex);
                                if (oc instanceof JsonObject) {
                                    JsonObject child = (JsonObject) oc;
                                    String childName = child.getString(PN_ORDEREDCHILDNAME, null);
                                    if (StringUtils.isNotBlank(childName)) {
                                        JsonObjectBuilder builder = Json.createObjectBuilder();
                                        for (Map.Entry<String, JsonValue> e : child.entrySet()) {
                                            if (!PN_ORDEREDCHILDNAME.equals(e.getKey())) {
                                                builder.add(e.getKey(), e.getValue());
                                            }
                                        }
                                        child = builder.build();
                                        this.createNode(childName, child, contentCreator);
                                    } else {
                                        throw new JsonException(PN_ORDEREDCHILDREN + " children must have a name whose key is " + PN_ORDEREDCHILDNAME);
                                    }
                                } else {
                                    throw new JsonException(PN_ORDEREDCHILDREN + " array must only have JSONObject items");
                                }
                            }
                        } else {
                            throw new JsonException(PN_ORDEREDCHILDREN + " value must be a JSON array");
                        }
                    }
                } else {
                    this.createProperty(n, o, contentCreator);
                }
            }
        }
    }
}
Also used : JsonArray(javax.json.JsonArray) JsonException(javax.json.JsonException) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map)

Example 22 with JsonArray

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

the class JsonReader method createProperty.

protected void createProperty(String name, Object value, ContentCreator contentCreator) throws RepositoryException {
    // assume simple value
    if (value instanceof JsonArray) {
        // multivalue
        final JsonArray array = (JsonArray) value;
        if (array.size() > 0) {
            final String[] values = new String[array.size()];
            for (int i = 0; i < values.length; i++) {
                values[i] = unbox(array.get(i)).toString();
            }
            final int propertyType = getType(name, unbox(array.get(0)));
            contentCreator.createProperty(getName(name), propertyType, values);
        } else {
            contentCreator.createProperty(getName(name), PropertyType.STRING, new String[0]);
        }
    } else if (value instanceof JsonValue) {
        // single value
        value = unbox(value);
        final int propertyType = getType(name, value);
        contentCreator.createProperty(getName(name), propertyType, value.toString());
    }
}
Also used : JsonArray(javax.json.JsonArray) JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString)

Example 23 with JsonArray

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

the class JsonContentParser method convertValue.

private Object convertValue(JsonValue value) {
    switch(value.getValueType()) {
        case STRING:
            String stringValue = ((JsonString) value).getString();
            Calendar calendarValue = helper.tryParseCalendar(stringValue);
            if (calendarValue != null) {
                return calendarValue;
            } else {
                return stringValue;
            }
        case NUMBER:
            JsonNumber numberValue = (JsonNumber) value;
            if (numberValue.isIntegral()) {
                return numberValue.longValue();
            } else {
                return numberValue.bigDecimalValue();
            }
        case TRUE:
            return true;
        case FALSE:
            return false;
        case NULL:
            return null;
        case ARRAY:
            JsonArray arrayValue = (JsonArray) value;
            Object[] values = new Object[arrayValue.size()];
            for (int i = 0; i < values.length; i++) {
                values[i] = convertValue(arrayValue.get(i));
            }
            return helper.convertSingleTypeArray(values);
        case OBJECT:
            return (JsonObject) value;
        default:
            throw new ParseException("Unexpected JSON value type: " + value.getValueType());
    }
}
Also used : JsonArray(javax.json.JsonArray) Calendar(java.util.Calendar) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) ParseException(org.apache.sling.jcr.contentparser.ParseException)

Example 24 with JsonArray

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

the class GetAclTest method testEffectiveAclForUser.

/**
	 * Test for SLING-2600, Effective ACL servlet returns incorrect information
	 */
@Test
public void testEffectiveAclForUser() throws IOException, JsonException {
    testUserId = H.createTestUser();
    testUserId2 = 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);
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId2));
    postParams.add(new NameValuePair("privilege@jcr:write", "granted"));
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId2));
    postParams.add(new NameValuePair("privilege@jcr:lockManagement", "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);
    JsonObject aceObject2 = jsonObject.getJsonObject(testUserId2);
    assertNotNull(aceObject2);
    String principalString2 = aceObject2.getString("principal");
    assertEquals(testUserId2, principalString2);
    JsonArray grantedArray2 = aceObject2.getJsonArray("granted");
    assertNotNull(grantedArray2);
    assertEquals(2, 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:write");
    H.assertPrivilege(grantedPrivilegeNames2, true, "jcr:lockManagement");
    Object deniedArray2 = aceObject2.get("denied");
    assertNull(deniedArray2);
}
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 25 with JsonArray

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

the class GetAclTest method testEffectiveAclMergeForUser_SubsetOfPrivilegesDeniedOnChild2.

/**
	 * Test for SLING-2600, Effective ACL servlet returns incorrect information
	 */
@Test
public void testEffectiveAclMergeForUser_SubsetOfPrivilegesDeniedOnChild2() 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:removeNode", "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() >= 11);
    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");
    H.assertPrivilege(grantedPrivilegeNames, true, "jcr:modifyProperties");
    H.assertPrivilege(grantedPrivilegeNames, true, "jcr:addChildNodes");
    H.assertPrivilege(grantedPrivilegeNames, true, "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: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)

Aggregations

JsonArray (javax.json.JsonArray)128 JsonObject (javax.json.JsonObject)97 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)32 JsonReader (javax.json.JsonReader)31 HashMap (java.util.HashMap)29 JsonString (javax.json.JsonString)28 HashSet (java.util.HashSet)21 NameValuePair (org.apache.commons.httpclient.NameValuePair)20 Credentials (org.apache.commons.httpclient.Credentials)19 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)19 StringReader (java.io.StringReader)18 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)17 LinkedHashMap (java.util.LinkedHashMap)14 JsonValue (javax.json.JsonValue)11 Map (java.util.Map)10 JsonException (javax.json.JsonException)9 Response (javax.ws.rs.core.Response)9 IOException (java.io.IOException)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7