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