Search in sources :

Example 36 with JsonObject

use of javax.json.JsonObject 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 37 with JsonObject

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

the class AbstractGetAclServlet method internalGetAcl.

@SuppressWarnings("unchecked")
protected JsonObject internalGetAcl(Session jcrSession, String resourcePath) throws RepositoryException {
    if (jcrSession == null) {
        throw new RepositoryException("JCR Session not found");
    }
    Item item = jcrSession.getItem(resourcePath);
    if (item != null) {
        resourcePath = item.getPath();
    } else {
        throw new ResourceNotFoundException("Resource is not a JCR Node");
    }
    // Calculate a map of privileges to all the aggregate privileges it is contained in.
    // Use for fast lookup during the mergePrivilegeSets calls below.
    AccessControlManager accessControlManager = AccessControlUtil.getAccessControlManager(jcrSession);
    Map<Privilege, Set<Privilege>> privilegeToAncestorMap = new HashMap<Privilege, Set<Privilege>>();
    Privilege[] supportedPrivileges = accessControlManager.getSupportedPrivileges(item.getPath());
    for (Privilege privilege : supportedPrivileges) {
        if (privilege.isAggregate()) {
            Privilege[] ap = privilege.getAggregatePrivileges();
            for (Privilege privilege2 : ap) {
                Set<Privilege> set = privilegeToAncestorMap.get(privilege2);
                if (set == null) {
                    set = new HashSet<Privilege>();
                    privilegeToAncestorMap.put(privilege2, set);
                }
                set.add(privilege);
            }
        }
    }
    AccessControlEntry[] declaredAccessControlEntries = getAccessControlEntries(jcrSession, resourcePath);
    Map<String, Map<String, Object>> aclMap = new LinkedHashMap<String, Map<String, Object>>();
    int sequence = 0;
    for (AccessControlEntry ace : declaredAccessControlEntries) {
        Principal principal = ace.getPrincipal();
        Map<String, Object> map = aclMap.get(principal.getName());
        if (map == null) {
            map = new LinkedHashMap<String, Object>();
            aclMap.put(principal.getName(), map);
            map.put("order", sequence++);
        }
    }
    //evaluate these in reverse order so the most entries with highest specificity are last
    for (int i = declaredAccessControlEntries.length - 1; i >= 0; i--) {
        AccessControlEntry ace = declaredAccessControlEntries[i];
        Principal principal = ace.getPrincipal();
        Map<String, Object> map = aclMap.get(principal.getName());
        Set<Privilege> grantedSet = (Set<Privilege>) map.get("granted");
        if (grantedSet == null) {
            grantedSet = new LinkedHashSet<Privilege>();
            map.put("granted", grantedSet);
        }
        Set<Privilege> deniedSet = (Set<Privilege>) map.get("denied");
        if (deniedSet == null) {
            deniedSet = new LinkedHashSet<Privilege>();
            map.put("denied", deniedSet);
        }
        boolean allow = AccessControlUtil.isAllow(ace);
        if (allow) {
            Privilege[] privileges = ace.getPrivileges();
            for (Privilege privilege : privileges) {
                mergePrivilegeSets(privilege, privilegeToAncestorMap, grantedSet, deniedSet);
            }
        } else {
            Privilege[] privileges = ace.getPrivileges();
            for (Privilege privilege : privileges) {
                mergePrivilegeSets(privilege, privilegeToAncestorMap, deniedSet, grantedSet);
            }
        }
    }
    List<JsonObject> aclList = new ArrayList<>();
    Set<Entry<String, Map<String, Object>>> entrySet = aclMap.entrySet();
    for (Entry<String, Map<String, Object>> entry : entrySet) {
        String principalName = entry.getKey();
        Map<String, Object> value = entry.getValue();
        JsonObjectBuilder aceObject = Json.createObjectBuilder();
        aceObject.add("principal", principalName);
        Set<Privilege> grantedSet = (Set<Privilege>) value.get("granted");
        if (grantedSet != null && !grantedSet.isEmpty()) {
            JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
            for (Privilege v : grantedSet) {
                arrayBuilder.add(v.getName());
            }
            aceObject.add("granted", arrayBuilder);
        }
        Set<Privilege> deniedSet = (Set<Privilege>) value.get("denied");
        if (deniedSet != null && !deniedSet.isEmpty()) {
            JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
            for (Privilege v : deniedSet) {
                arrayBuilder.add(v.getName());
            }
            aceObject.add("denied", arrayBuilder);
        }
        aceObject.add("order", (Integer) value.get("order"));
        aclList.add(aceObject.build());
    }
    JsonObjectBuilder jsonAclMap = Json.createObjectBuilder();
    for (Map.Entry<String, Map<String, Object>> entry : aclMap.entrySet()) {
        JsonObjectBuilder builder = Json.createObjectBuilder();
        for (Map.Entry<String, Object> inner : entry.getValue().entrySet()) {
            addTo(builder, inner.getKey(), inner.getValue());
        }
        jsonAclMap.add(entry.getKey(), builder);
    }
    for (JsonObject jsonObj : aclList) {
        jsonAclMap.add(jsonObj.getString("principal"), jsonObj);
    }
    return jsonAclMap.build();
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) LinkedHashMap(java.util.LinkedHashMap) Item(javax.jcr.Item) AccessControlEntry(javax.jcr.security.AccessControlEntry) Entry(java.util.Map.Entry) JsonArrayBuilder(javax.json.JsonArrayBuilder) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) JsonObjectBuilder(javax.json.JsonObjectBuilder) AccessControlEntry(javax.jcr.security.AccessControlEntry) RepositoryException(javax.jcr.RepositoryException) JsonObject(javax.json.JsonObject) Privilege(javax.jcr.security.Privilege) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Principal(java.security.Principal)

Example 38 with JsonObject

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

the class JsonResponseTest method testSend.

public void testSend() throws Exception {
    res.onChange("modified", "argument1");
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    res.send(response, true);
    JsonObject result = Json.createReader(new StringReader(response.getOutput().toString())).readObject();
    assertProperty(result, HtmlResponse.PN_STATUS_CODE, Integer.toString(HttpServletResponse.SC_OK));
    assertEquals(JSONResponse.RESPONSE_CONTENT_TYPE, response.getContentType());
    assertEquals(JSONResponse.RESPONSE_CHARSET, response.getCharacterEncoding());
}
Also used : StringReader(java.io.StringReader) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) JsonObject(javax.json.JsonObject)

Example 39 with JsonObject

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

the class JsonResponseTest method testSetError.

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
public void testSetError() throws IOException {
    String errMsg = "Dummy error";
    res.setError(new Error(errMsg));
    MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
    res.send(resp, true);
    JsonObject json = res.getJson();
    JsonValue error = assertProperty(json, "error");
    assertProperty((JsonObject) error, "class", Error.class.getName());
    assertProperty((JsonObject) error, "message", errMsg);
}
Also used : JsonValue(javax.json.JsonValue) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 40 with JsonObject

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

the class JSONGroovyBuilderTest method testRichObject.

public void testRichObject() throws IOException, JsonException {
    final String toDelete = uploadTestScript("builder_rich_object.groovy", "json.groovy");
    try {
        final String content = getContent(displayUrl + ".json", CONTENT_TYPE_JSON);
        log.debug("{} content: {}", displayUrl, content);
        JsonObject jo = Json.createReader(new StringReader(content)).readObject();
        assertEquals("Content contained wrong number of items", 2, jo.size());
        assertEquals("Content contained wrong data", testText, jo.getString("text"));
        assertEquals("Content contained wrong data", "bar", ((JsonObject) jo.getJsonObject("obj")).getString("foo"));
    } finally {
        testClient.delete(toDelete);
    }
}
Also used : StringReader(java.io.StringReader) 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