Search in sources :

Example 1 with JsonArrayBuilder

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

the class AdapterWebConsolePlugin method getJson.

private void getJson(final HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("application/json");
    try {
        Map<String, Map<String, List<String>>> values = new HashMap<>();
        for (final AdaptableDescription desc : allAdaptables) {
            final Map<String, List<String>> adaptableObj;
            if (values.containsKey(desc.adaptable)) {
                adaptableObj = values.get(desc.adaptable);
            } else {
                adaptableObj = new HashMap<>();
                values.put(desc.adaptable, adaptableObj);
            }
            for (final String adapter : desc.adapters) {
                List<String> conditions = adaptableObj.get(desc.condition == null ? "" : desc.condition);
                if (conditions == null) {
                    conditions = new ArrayList<>();
                    adaptableObj.put(desc.condition == null ? "" : desc.condition, conditions);
                }
                conditions.add(adapter);
            }
        }
        final JsonObjectBuilder obj = Json.createObjectBuilder();
        for (Map.Entry<String, Map<String, List<String>>> entry : values.entrySet()) {
            JsonObjectBuilder adaptable = Json.createObjectBuilder();
            for (Map.Entry<String, List<String>> subEnty : entry.getValue().entrySet()) {
                if (subEnty.getValue().size() > 1) {
                    JsonArrayBuilder array = Json.createArrayBuilder();
                    for (String condition : subEnty.getValue()) {
                        array.add(condition);
                    }
                    adaptable.add(subEnty.getKey(), array);
                } else {
                    adaptable.add(subEnty.getKey(), subEnty.getValue().get(0));
                }
            }
            obj.add(entry.getKey(), adaptable);
        }
        Json.createGenerator(resp.getWriter()).write(obj.build()).flush();
    } catch (final JsonException e) {
        throw new ServletException("Unable to produce JSON", e);
    }
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) JsonString(javax.json.JsonString) ServletException(javax.servlet.ServletException) List(java.util.List) ArrayList(java.util.ArrayList) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with JsonArrayBuilder

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

the class Announcement method asJSON.

/** convert a clusterview into json **/
private static JsonObject asJSON(final ClusterView clusterView) {
    JsonObjectBuilder obj = Json.createObjectBuilder();
    obj.add("id", clusterView.getId());
    JsonArrayBuilder instancesObj = Json.createArrayBuilder();
    List<InstanceDescription> instances = clusterView.getInstances();
    for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
        InstanceDescription instanceDescription = it.next();
        instancesObj.add(asJSON(instanceDescription));
    }
    obj.add("instances", instancesObj);
    return obj.build();
}
Also used : DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) NonLocalInstanceDescription(org.apache.sling.discovery.commons.providers.NonLocalInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 3 with JsonArrayBuilder

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

the class TopologyRequestValidator method encodeMessage.

/**
     * Encodes a request returning the encoded body
     *
     * @param body
     * @return the encoded body.
     * @throws IOException
     */
public String encodeMessage(String body) throws IOException {
    checkActive();
    if (encryptionEnabled) {
        try {
            JsonObjectBuilder json = Json.createObjectBuilder();
            JsonArrayBuilder array = Json.createArrayBuilder();
            for (String value : encrypt(body)) {
                array.add(value);
            }
            json.add("payload", array);
            StringWriter writer = new StringWriter();
            Json.createGenerator(writer).write(json.build()).close();
            return writer.toString();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (BadPaddingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (NoSuchPaddingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (JsonException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (InvalidKeySpecException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (InvalidParameterSpecException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        }
    }
    return body;
}
Also used : JsonException(javax.json.JsonException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 4 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder 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 5 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder 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)

Aggregations

JsonArrayBuilder (javax.json.JsonArrayBuilder)177 JsonObjectBuilder (javax.json.JsonObjectBuilder)103 JsonObject (javax.json.JsonObject)36 Map (java.util.Map)29 Path (javax.ws.rs.Path)26 GET (javax.ws.rs.GET)24 HashMap (java.util.HashMap)19 JsonArray (javax.json.JsonArray)18 ArrayList (java.util.ArrayList)15 List (java.util.List)15 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)12 IOException (java.io.IOException)12 Dataverse (edu.harvard.iq.dataverse.Dataverse)10 Dataset (edu.harvard.iq.dataverse.Dataset)9 User (edu.harvard.iq.dataverse.authorization.users.User)9 JsonValue (javax.json.JsonValue)9 StringWriter (java.io.StringWriter)8 JsonString (javax.json.JsonString)7 Date (java.util.Date)6 JsonException (javax.json.JsonException)6