use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class MultiFieldPanelFunctions method getMultiFieldPanelValues.
/**
* Extract the value of a MultiFieldPanel property into a list of maps. Will never return
* a null map, but may return an empty one. Invalid property values are logged and skipped.
*
* @param resource the resource
* @param name the property name
* @return a list of maps.
*/
@Function
public static List<Map<String, String>> getMultiFieldPanelValues(Resource resource, String name) {
ValueMap map = resource.adaptTo(ValueMap.class);
List<Map<String, String>> results = new ArrayList<Map<String, String>>();
if (map != null && map.containsKey(name)) {
String[] values = map.get(name, new String[0]);
for (String value : values) {
try {
JSONObject parsed = new JSONObject(value);
Map<String, String> columnMap = new HashMap<String, String>();
for (Iterator<String> iter = parsed.keys(); iter.hasNext(); ) {
String key = iter.next();
String innerValue = parsed.getString(key);
columnMap.put(key, innerValue);
}
results.add(columnMap);
} catch (JSONException e) {
log.error(String.format("Unable to parse JSON in %s property of %s", name, resource.getPath()), e);
}
}
}
return results;
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class StatusServlet method getSystemStats.
@SuppressWarnings("squid:S1192")
private JSONObject getSystemStats() throws JSONException {
JSONObject json = new JSONObject();
try {
json.put("cpu", MessageFormat.format("{0,number,#%}", ttrs.getCpuLevel()));
} catch (InstanceNotFoundException e) {
log.error("Could not collect CPU stats", e);
json.put("cpu", -1);
} catch (ReflectionException e) {
log.error("Could not collect CPU stats", e);
json.put("cpu", -1);
}
json.put("mem", MessageFormat.format("{0,number,#%}", ttrs.getMemoryUsage()));
json.put("maxCpu", MessageFormat.format("{0,number,#%}", ttrs.getMaxCpu()));
json.put("maxMem", MessageFormat.format("{0,number,#%}", ttrs.getMaxHeap()));
return json;
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class StatusServlet method doGet.
@Override
@SuppressWarnings({ "squid:S3776", "squid:S1192" })
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss aaa");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
Config config = request.getResource().adaptTo(Config.class);
Workspace workspace = config.getWorkspace();
final JSONObject json = new JSONObject();
try {
json.put("initialized", workspace.isInitialized());
json.put("status", workspace.getStatus());
if (workspace.getSubStatus() != null) {
json.put("subStatus", workspace.getSubStatus());
}
json.put("runnerType", config.getRunnerType());
json.put("queryType", config.getQueryType());
json.put("queryStatement", config.getQueryStatement());
json.put("workflowModel", StringUtils.removeEnd(config.getWorkflowModelId(), "/jcr:content/model"));
json.put("batchSize", config.getBatchSize());
json.put("autoThrottle", config.isAutoThrottle());
json.put("purgeWorkflow", config.isPurgeWorkflow());
json.put("interval", config.getInterval());
json.put("retryCount", config.getRetryCount());
json.put("timeout", config.getTimeout());
json.put("throttle", config.getThrottle());
json.put("message", workspace.getMessage());
if (config.isUserEventData()) {
json.put("userEventData", config.getUserEventData());
}
ActionManager actionManager = actionManagerFactory.getActionManager(workspace.getActionManagerName());
if (actionManager != null && !Status.COMPLETED.equals(workspace.getStatus())) {
// If Complete, then look to JCR for final accounts as ActionManager may be gone
addActionManagerTrackedCounts(workspace.getActionManagerName(), json);
for (com.adobe.acs.commons.fam.Failure failure : actionManager.getFailureList()) {
JSONObject failureJSON = new JSONObject();
failureJSON.put(Failure.PN_PAYLOAD_PATH, failure.getNodePath());
failureJSON.put(Failure.PN_FAILED_AT, sdf.format(failure.getTime().getTime()));
json.accumulate("failures", failureJSON);
}
} else {
addWorkspaceTrackedCounts(workspace, json);
// Failures
for (Failure failure : workspace.getFailures()) {
json.accumulate("failures", failure.toJSON());
}
}
// Times
if (workspace.getStartedAt() != null) {
json.put("startedAt", sdf.format(workspace.getStartedAt().getTime()));
json.put("timeTakenInMillis", (Calendar.getInstance().getTime().getTime() - workspace.getStartedAt().getTime().getTime()));
}
if (workspace.getStoppedAt() != null) {
json.put("stoppedAt", sdf.format(workspace.getStoppedAt().getTime()));
json.put("timeTakenInMillis", (workspace.getStoppedAt().getTime().getTime() - workspace.getStartedAt().getTime().getTime()));
}
if (workspace.getCompletedAt() != null) {
json.put("completedAt", sdf.format(workspace.getCompletedAt().getTime()));
json.put("timeTakenInMillis", (workspace.getCompletedAt().getTime().getTime() - workspace.getStartedAt().getTime().getTime()));
}
if (AEMWorkflowRunnerImpl.class.getName().equals(config.getRunnerType())) {
for (Payload payload : config.getWorkspace().getActivePayloads()) {
json.accumulate("activePayloads", payload.toJSON());
}
}
json.put("systemStats", getSystemStats());
response.getWriter().write(json.toString());
} catch (JSONException e) {
log.error("Could not collect Bulk Workflow status due to: {}", e);
JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not collect Bulk Workflow status.", e.getMessage());
}
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class Payload method toJSON.
/**
* Renditions *
*/
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put(PN_STATUS, getStatus().toString());
json.put(PN_PATH, getPayloadPath());
return json;
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class WCMViewsServlet method doGet.
@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
if (WCMMode.DISABLED.equals(WCMMode.fromRequest(request))) {
response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);
response.getWriter().write("");
return;
}
/* Valid WCMMode */
final PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
final Page page = pageManager.getContainingPage(request.getResource());
final WCMViewsResourceVisitor visitor = new WCMViewsResourceVisitor();
visitor.accept(page.getContentResource());
final Set<String> viewSet = new HashSet<String>(visitor.getWCMViews());
// Get the Views provided by the Servlet
for (final Map.Entry<String, String[]> entry : this.defaultViews.entrySet()) {
if (StringUtils.startsWith(page.getPath(), entry.getKey())) {
viewSet.addAll(Arrays.asList(entry.getValue()));
}
}
final List<String> views = new ArrayList<String>(viewSet);
Collections.sort(views);
log.debug("Collected WCM Views {} for Page [ {} ]", views, page.getPath());
final JSONArray jsonArray = new JSONArray();
for (final String view : views) {
final JSONObject json = new JSONObject();
try {
json.put("title", StringUtils.capitalize(view) + " View");
json.put("value", view);
jsonArray.put(json);
} catch (JSONException e) {
log.error("Unable to build WCM Views JSON output.", e);
}
}
response.getWriter().write(jsonArray.toString());
}
Aggregations