Search in sources :

Example 1 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project karaf by apache.

the class InstancePlugin method writeJSON.

private void writeJSON(final PrintWriter pw) {
    final JSONWriter jw = new JSONWriter(pw);
    final Instance[] instances = instanceService.getInstances();
    try {
        jw.object();
        jw.key("status");
        jw.value(getStatusLine());
        jw.key("instances");
        jw.array();
        for (Instance i : instances) {
            instanceInfo(jw, i);
        }
        jw.endArray();
        jw.endObject();
    } catch (IOException ex) {
        Logger.getLogger(InstancePlugin.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(InstancePlugin.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) Instance(org.apache.karaf.instance.core.Instance) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project karaf by apache.

the class HttpPlugin method writeJSON.

private void writeJSON(final PrintWriter pw) throws IOException {
    final List<ServletDetails> servlets = this.getServletDetails();
    final List<WebDetail> web = this.getWebDetails();
    final String statusLine = this.getStatusLine(servlets, web);
    final JSONWriter jw = new JSONWriter(pw);
    jw.object();
    jw.key("status");
    jw.value(statusLine);
    jw.key("contexts");
    jw.array();
    for (ServletDetails servlet : servlets) {
        jw.object();
        jw.key("id");
        jw.value(servlet.getId());
        jw.key("servlet");
        jw.value(servlet.getServlet());
        jw.key("servletName");
        jw.value(servlet.getServletName());
        jw.key("state");
        jw.value(servlet.getState());
        jw.key("alias");
        jw.value(servlet.getAlias());
        jw.key("urls");
        jw.array();
        for (String url : servlet.getUrls()) {
            jw.value(url);
        }
        jw.endArray();
        jw.endObject();
    }
    jw.endArray();
    jw.key("web");
    jw.array();
    for (WebDetail webDetail : web) {
        jw.object();
        jw.key("id");
        jw.value(webDetail.getBundleId());
        jw.key("bundlestate");
        jw.value(webDetail.getState());
        jw.key("contextpath");
        jw.value(webDetail.getContextPath());
        jw.key("state");
        jw.value(webDetail.getWebState());
        jw.endObject();
    }
    jw.endArray();
    jw.endObject();
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter)

Example 3 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project sling by apache.

the class MappingInventoryPrinter method renderJson.

private void renderJson(PrintWriter out) throws IOException {
    final List<Mapping> data = mapper.getActiveMappings();
    final Map<String, List<Mapping>> byUser = getMappingsByUser(data);
    final JSONWriter w = new JSONWriter(out);
    w.object();
    w.key("title").value("Service User Mappings");
    w.key("mappingsCount").value(data.size());
    w.key("uniqueUsersCount").value(byUser.keySet().size());
    w.key("mappingsByUser");
    w.object();
    for (Map.Entry<String, List<Mapping>> e : byUser.entrySet()) {
        w.key(e.getKey());
        w.array();
        for (Mapping m : e.getValue()) {
            asJSON(w, m);
        }
        w.endArray();
    }
    w.endObject();
    w.endObject();
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) List(java.util.List) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 4 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project sling by apache.

the class ModifyUserServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    ValueMap requestParameters = request.adaptTo(ValueMap.class);
    String[] resourceTypeValues = requestParameters.get("sling:resourceType", String[].class);
    String resourceType = null;
    if (resourceTypeValues != null && resourceTypeValues.length > 0) {
        resourceType = resourceTypeValues[0];
    }
    if (resourceType != null && !"".equals(resourceType)) {
        String resourcePath = request.getRequestPathInfo().getResourcePath();
        ValidationModel vm = validationService.getValidationModel(resourceType, resourcePath, false);
        if (vm != null) {
            ValidationResult vr = validationService.validate(requestParameters, vm);
            if (vr.isValid()) {
                RequestDispatcherOptions options = new RequestDispatcherOptions();
                options.setReplaceSelectors(" ");
                request.getRequestDispatcher(request.getResource(), options).forward(request, response);
            } else {
                response.setContentType("application/json");
                JSONWriter writer = new JSONWriter(response.getWriter());
                writer.object();
                writer.key("success").value(false);
                writer.key("failures").array();
                for (ValidationFailure failure : vr.getFailures()) {
                    writer.object();
                    writer.key("message").value(failure.getMessage(request.getResourceBundle(Locale.US)));
                    writer.key("location").value(failure.getLocation());
                    writer.endObject();
                }
                writer.endArray();
                writer.endObject();
                response.setStatus(400);
            }
        }
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) ValidationModel(org.apache.sling.validation.model.ValidationModel) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) ValueMap(org.apache.sling.api.resource.ValueMap) ValidationResult(org.apache.sling.validation.ValidationResult) ValidationFailure(org.apache.sling.validation.ValidationFailure)

Example 5 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project sling by apache.

the class ValidationPostResponse method doSend.

@Override
protected void doSend(HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    PrintWriter printWriter = response.getWriter();
    JSONWriter writer = new JSONWriter(printWriter);
    writer.object();
    boolean validationError = false;
    if (validationResult != null) {
        try {
            writer.key("valid").value(validationResult.isValid());
            writer.key("failures").array();
            for (ValidationFailure failure : validationResult.getFailures()) {
                writer.object();
                writer.key("message").value(failure.getMessage(resourceBundle));
                writer.key("location").value(failure.getLocation());
                writer.key("severity").value(failure.getSeverity());
                writer.endObject();
            }
            writer.endArray();
        } catch (IOException e) {
            LOG.error("JSON error during response send operation.", e);
        }
    } else {
        validationError = true;
    }
    writer.endObject();
    if (validationError) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) ValidationFailure(org.apache.sling.validation.ValidationFailure)

Aggregations

JSONWriter (org.apache.felix.utils.json.JSONWriter)17 IOException (java.io.IOException)10 ServletException (javax.servlet.ServletException)6 PrintWriter (java.io.PrintWriter)4 Map (java.util.Map)2 ScriptEngineFactory (javax.script.ScriptEngineFactory)2 ValidationFailure (org.apache.sling.validation.ValidationFailure)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 RandomAccessFile (java.io.RandomAccessFile)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1