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();
}
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;
}
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);
}
}
}
}
}
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();
}
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();
}
Aggregations