Search in sources :

Example 11 with JsonException

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

the class ValidationMojo method validate.

private void validate(final File directory, final String fileName) throws MojoExecutionException {
    getLog().debug("Validating " + fileName);
    final File file = new File(directory, fileName);
    if (file.isFile()) {
        if (fileName.endsWith(".json") && !this.skipJson) {
            getLog().debug("Validation JSON file " + fileName);
            FileInputStream fis = null;
            String json = null;
            try {
                fis = new FileInputStream(file);
                json = IOUtils.toString(fis, CharEncoding.UTF_8);
            } catch (IOException e) {
                throw new MojoExecutionException("An Error occured while validating the file '" + fileName + "'", e);
            } finally {
                IOUtils.closeQuietly(fis);
            }
            // validate JSON
            try {
                JsonSupport.validateJsonStructure(json, jsonQuoteTick);
            } catch (JsonException e) {
                throw new MojoExecutionException("An Error occured while validating the file '" + fileName + "'", e);
            }
        }
    }
}
Also used : JsonException(javax.json.JsonException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with JsonException

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

the class SlingInfoServlet method renderJson.

private void renderJson(final SlingHttpServletResponse response, final Map<String, String> data) throws IOException {
    // render data in JSON format
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    final Writer out = response.getWriter();
    final JsonGenerator w = Json.createGenerator(out);
    try {
        w.writeStartObject();
        for (final Map.Entry<String, String> e : data.entrySet()) {
            w.write(e.getKey(), e.getValue());
        }
        w.writeEnd();
    } catch (JsonException jse) {
        out.write(jse.toString());
        out.flush();
    } finally {
        w.flush();
    }
}
Also used : JsonException(javax.json.JsonException) JsonGenerator(javax.json.stream.JsonGenerator) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter) JsonWriter(javax.json.JsonWriter) Writer(java.io.Writer)

Example 13 with JsonException

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

the class JsonRenderer method setup.

public void setup(HttpServletResponse response, String pageTitle) throws IOException, UnsupportedEncodingException {
    if (writer != null) {
        throw new IllegalStateException("Output Writer already set");
    }
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    writer = Json.createGenerator(response.getWriter());
    try {
        writer.writeStartArray();
    } catch (JsonException jex) {
        throw (IOException) new IOException().initCause(jex);
    }
}
Also used : JsonException(javax.json.JsonException) IOException(java.io.IOException)

Example 14 with JsonException

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

the class PostServletOrderTest method verifyOrder.

/**
     * Verify node order
     */
private void verifyOrder(String parentUrl, String[] names) throws IOException {
    // check that nodes appear in creation order in their parent's list of children
    final String content = getContent(parentUrl + ".1.json", CONTENT_TYPE_JSON);
    String expected = "";
    for (String n : names) {
        expected += n + ",";
    }
    //assertJavascript(expected, content, TEST_SCRIPT);
    try {
        String actual = "";
        JsonObject obj = JsonUtil.parseObject(content);
        for (String name : obj.keySet()) {
            Object o = obj.get(name);
            if (o instanceof JsonObject) {
                actual += name + ",";
            }
        }
        assertEquals(expected, actual);
    } catch (JsonException e) {
        throw new IOException(e.toString());
    }
}
Also used : JsonException(javax.json.JsonException) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) IOException(java.io.IOException)

Example 15 with JsonException

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

the class JsonObjectCreator method createProperty.

/**
     * Write a single property
     */
private static void createProperty(final JsonObjectBuilder obj, final ValueMap valueMap, final String key, final Object value) {
    Object[] values = null;
    if (value.getClass().isArray()) {
        if (value instanceof long[]) {
            values = ArrayUtils.toObject((long[]) value);
        } else if (value instanceof int[]) {
            values = ArrayUtils.toObject((int[]) value);
        } else if (value instanceof double[]) {
            values = ArrayUtils.toObject((double[]) value);
        } else if (value instanceof byte[]) {
            values = ArrayUtils.toObject((byte[]) value);
        } else if (value instanceof float[]) {
            values = ArrayUtils.toObject((float[]) value);
        } else if (value instanceof short[]) {
            values = ArrayUtils.toObject((short[]) value);
        } else if (value instanceof long[]) {
            values = ArrayUtils.toObject((long[]) value);
        } else if (value instanceof boolean[]) {
            values = ArrayUtils.toObject((boolean[]) value);
        } else if (value instanceof char[]) {
            values = ArrayUtils.toObject((char[]) value);
        } else {
            values = (Object[]) value;
        }
        // write out empty array
        if (values.length == 0) {
            obj.add(key, Json.createArrayBuilder());
            return;
        }
    }
    // special handling for binaries: we dump the length and not the data!
    if (value instanceof InputStream || (values != null && values[0] instanceof InputStream)) {
        // in the name, and the value should be the size of the binary data
        try {
            if (values == null) {
                obj.add(":" + key, getLength(valueMap, -1, key, (InputStream) value));
            } else {
                final JsonArrayBuilder result = Json.createArrayBuilder();
                for (int i = 0; i < values.length; i++) {
                    result.add(getLength(valueMap, i, key, (InputStream) values[i]));
                }
                obj.add(":" + key, result);
            }
        } catch (final JsonException ignore) {
            // we ignore this
            LoggerFactory.getLogger(JsonObjectCreator.class).warn("Unable to create JSON value", ignore);
        }
        return;
    }
    try {
        if (!value.getClass().isArray()) {
            obj.add(key, getValue(value));
        } else {
            final JsonArrayBuilder result = Json.createArrayBuilder();
            for (Object v : values) {
                result.add(getValue(v));
            }
            obj.add(key, result);
        }
    } catch (final JsonException ignore) {
        // we ignore this
        LoggerFactory.getLogger(JsonObjectCreator.class).warn("Unable to create JSON value", ignore);
    }
}
Also used : JsonException(javax.json.JsonException) InputStream(java.io.InputStream) JsonObject(javax.json.JsonObject) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Aggregations

JsonException (javax.json.JsonException)27 IOException (java.io.IOException)11 JsonObject (javax.json.JsonObject)11 HashMap (java.util.HashMap)9 JsonString (javax.json.JsonString)7 Map (java.util.Map)6 JsonArray (javax.json.JsonArray)6 JsonObjectBuilder (javax.json.JsonObjectBuilder)6 StringReader (java.io.StringReader)5 Resource (org.apache.sling.api.resource.Resource)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 JsonArrayBuilder (javax.json.JsonArrayBuilder)4 LoginException (org.apache.sling.api.resource.LoginException)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 File (java.io.File)3 StringWriter (java.io.StringWriter)3 JsonValue (javax.json.JsonValue)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 FileInputStream (java.io.FileInputStream)2