Search in sources :

Example 11 with JsonObjectBuilder

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

the class ResultJsonSerializer method getJsonForSimpleResult.

private JsonObject getJsonForSimpleResult(final HealthCheckExecutionResult healthCheckResult, boolean includeDebug) {
    JsonObjectBuilder result = Json.createObjectBuilder();
    result.add("name", healthCheckResult.getHealthCheckMetadata().getName());
    result.add("status", healthCheckResult.getHealthCheckResult().getStatus().toString());
    result.add("timeInMs", healthCheckResult.getElapsedTimeInMs());
    result.add("finishedAt", healthCheckResult.getFinishedAt().toString());
    JsonArrayBuilder tagsArray = Json.createArrayBuilder();
    for (final String tag : healthCheckResult.getHealthCheckMetadata().getTags()) {
        tagsArray.add(tag);
    }
    result.add("tags", tagsArray);
    JsonArrayBuilder messagesArr = Json.createArrayBuilder();
    for (ResultLog.Entry entry : healthCheckResult.getHealthCheckResult()) {
        if (!includeDebug && entry.getStatus() == Result.Status.DEBUG) {
            continue;
        }
        JsonObjectBuilder jsonEntry = Json.createObjectBuilder();
        jsonEntry.add("status", entry.getStatus().toString());
        jsonEntry.add("message", entry.getMessage());
        Exception exception = entry.getException();
        if (exception != null) {
            StringWriter stringWriter = new StringWriter();
            exception.printStackTrace(new PrintWriter(stringWriter));
            jsonEntry.add("exception", stringWriter.toString());
        }
        messagesArr.add(jsonEntry);
    }
    result.add("messages", messagesArr);
    return result.build();
}
Also used : StringWriter(java.io.StringWriter) ResultLog(org.apache.sling.hc.api.ResultLog) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonException(javax.json.JsonException) PrintWriter(java.io.PrintWriter)

Example 12 with JsonObjectBuilder

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

the class ResultJsonSerializer method serialize.

public String serialize(final Result overallResult, final List<HealthCheckExecutionResult> executionResults, final String jsonpCallback, boolean includeDebug) {
    LOG.debug("Sending json response... ");
    JsonObjectBuilder result = Json.createObjectBuilder();
    try {
        result.add("overallResult", overallResult.getStatus().toString());
        JsonArrayBuilder resultsJsonArr = Json.createArrayBuilder();
        for (HealthCheckExecutionResult healthCheckResult : executionResults) {
            resultsJsonArr.add(getJsonForSimpleResult(healthCheckResult, includeDebug));
        }
        result.add("results", resultsJsonArr);
    } catch (JsonException e) {
        LOG.info("Could not serialize health check result: " + e, e);
    }
    StringWriter writer = new StringWriter();
    Json.createGenerator(writer).write(result.build()).close();
    String resultStr = writer.toString();
    if (StringUtils.isNotBlank(jsonpCallback)) {
        resultStr = jsonpCallback + "(" + resultStr + ");";
    }
    return resultStr;
}
Also used : JsonException(javax.json.JsonException) StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 13 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder 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 14 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder 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 15 with JsonObjectBuilder

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

the class JSONResponse method getJson.

JsonObject getJson() {
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    for (Map.Entry<String, Object> entry : json.entrySet()) {
        if (entry.getValue() != null) {
            jsonBuilder.add(entry.getKey(), entry.getValue().toString());
        } else {
            jsonBuilder.addNull(entry.getKey());
        }
    }
    if (this.error != null) {
        jsonBuilder.add("error", Json.createObjectBuilder().add("class", error.getClass().getName()).add("message", error.getMessage()));
    }
    JsonArrayBuilder changesBuilder = Json.createArrayBuilder();
    for (Map<String, Object> entry : changes) {
        JsonObjectBuilder entryBuilder = Json.createObjectBuilder();
        entryBuilder.add(PROP_TYPE, (String) entry.get(PROP_TYPE));
        Object arguments = entry.get(PROP_ARGUMENT);
        if (arguments != null) {
            if (arguments instanceof List) {
                JsonArrayBuilder argumentsBuilder = Json.createArrayBuilder();
                for (String argument : ((List<String>) arguments)) {
                    argumentsBuilder.add(argument);
                }
                entryBuilder.add(PROP_ARGUMENT, argumentsBuilder);
            } else {
                entryBuilder.add(PROP_ARGUMENT, (String) arguments);
            }
        }
        changesBuilder.add(entryBuilder);
    }
    jsonBuilder.add(PROP_CHANGES, changesBuilder);
    return jsonBuilder.build();
}
Also used : JsonObject(javax.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

JsonObjectBuilder (javax.json.JsonObjectBuilder)34 JsonArrayBuilder (javax.json.JsonArrayBuilder)21 Map (java.util.Map)11 JsonObject (javax.json.JsonObject)10 JsonException (javax.json.JsonException)7 HashMap (java.util.HashMap)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)5 List (java.util.List)3 Entry (java.util.Map.Entry)3 JsonValue (javax.json.JsonValue)3 JavaSource (de.strullerbaumann.visualee.source.entity.JavaSource)2 File (java.io.File)2 Privilege (javax.jcr.security.Privilege)2 JsonArray (javax.json.JsonArray)2 JsonString (javax.json.JsonString)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 AttributeReference (com.torodb.core.language.AttributeReference)1 IndexType (com.torodb.core.model.IndexedAttributes.IndexType)1