use of org.apache.sling.commons.json.JSONArray in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImpl method getPathFilterSetPreviewJSON.
/**
* {@inheritDoc}
*/
public String getPathFilterSetPreviewJSON(final Collection<PathFilterSet> pathFilterSets) throws JSONException {
final JSONObject json = new JSONObject();
json.put(KEY_STATUS, "preview");
json.put(KEY_PATH, "Not applicable (Preview)");
json.put(KEY_FILTER_SETS, new JSONArray());
for (final PathFilterSet pathFilterSet : pathFilterSets) {
final JSONObject tmp = new JSONObject();
tmp.put(KEY_IMPORT_MODE, "Not applicable (Preview)");
tmp.put(KEY_ROOT_PATH, pathFilterSet.getRoot());
json.accumulate("filterSets", tmp);
}
return json.toString();
}
use of org.apache.sling.commons.json.JSONArray in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImplTest method testGetPreviewJSON.
@Test
public void testGetPreviewJSON() throws Exception {
final Set<Resource> resources = new HashSet<Resource>();
resources.add(slingContext.create().resource("/a/b/c"));
resources.add(slingContext.create().resource("/d/e/f"));
resources.add(slingContext.create().resource("/g/h/i"));
final String actual = packageHelper.getPreviewJSON(resources);
final JSONObject json = new JSONObject(actual);
assertEquals("preview", json.getString("status"));
assertEquals("Not applicable (Preview)", json.getString("path"));
final String[] expectedFilterSets = new String[] { "/a/b/c", "/d/e/f", "/g/h/i" };
JSONArray actualArray = json.getJSONArray("filterSets");
for (int i = 0; i < actualArray.length(); i++) {
JSONObject tmp = actualArray.getJSONObject(i);
assertTrue(ArrayUtils.contains(expectedFilterSets, tmp.get("rootPath")));
}
assertEquals(expectedFilterSets.length, actualArray.length());
}
use of org.apache.sling.commons.json.JSONArray in project acs-aem-commons by Adobe-Consulting-Services.
the class CustomPollingImporterListServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
XSSAPI xssApi = request.adaptTo(XSSAPI.class);
try {
JSONObject result = new JSONObject();
JSONArray list = new JSONArray();
result.put("list", list);
ServiceReference[] services = tracker.getServiceReferences();
if (services != null) {
for (ServiceReference service : services) {
String displayName = PropertiesUtil.toString(service.getProperty("displayName"), null);
String[] schemes = PropertiesUtil.toStringArray(service.getProperty(Importer.SCHEME_PROPERTY));
if (displayName != null && schemes != null) {
for (String scheme : schemes) {
JSONObject obj = new JSONObject();
obj.put("qtip", "");
obj.put("text", displayName);
obj.put("text_xss", xssApi.encodeForJSString(displayName));
obj.put("value", scheme);
list.put(obj);
}
}
}
}
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
result.write(response.getWriter());
} catch (JSONException e) {
throw new ServletException("Unable to generate importer list", e);
}
}
use of org.apache.sling.commons.json.JSONArray in project acs-aem-commons by Adobe-Consulting-Services.
the class RemoveServlet method doPost.
@Override
public final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
List<String> statuses = new ArrayList<String>();
List<String> models = new ArrayList<String>();
List<Pattern> payloads = new ArrayList<Pattern>();
Calendar olderThan = null;
try {
JSONObject params = new JSONObject(request.getParameter("params"));
JSONArray jsonArray = params.optJSONArray(PARAM_WORKFLOW_STATUSES);
for (int i = 0; i < jsonArray.length(); i++) {
statuses.add(jsonArray.getString(i));
}
jsonArray = params.optJSONArray(PARAM_WORKFLOW_MODELS);
for (int i = 0; i < jsonArray.length(); i++) {
models.add(jsonArray.getString(i));
}
jsonArray = params.optJSONArray(PARAM_WORKFLOW_PAYLOADS);
for (int i = 0; i < jsonArray.length(); i++) {
final JSONObject tmp = jsonArray.getJSONObject(i);
final String pattern = tmp.optString("pattern");
if (StringUtils.isNotBlank(pattern)) {
payloads.add(Pattern.compile(pattern));
}
}
final Long ts = params.optLong(PARAM_OLDER_THAN);
if (ts != null && ts > 0) {
olderThan = Calendar.getInstance();
olderThan.setTimeInMillis(ts * MS_IN_SECOND);
}
int batchSize = params.optInt(PARAM_BATCH_SIZE);
if (batchSize < 1) {
batchSize = DEFAULT_BATCH_SIZE;
}
int maxDuration = params.optInt(PARAM_MAX_DURATION);
if (maxDuration < 1) {
maxDuration = DEFAULT_MAX_DURATION;
}
workflowInstanceRemover.removeWorkflowInstances(request.getResourceResolver(), models, statuses, payloads, olderThan, batchSize, maxDuration);
} catch (WorkflowRemovalForceQuitException e) {
response.setStatus(599);
response.getWriter().write("Workflow removal force quit");
} catch (Exception e) {
log.error("An error occurred while attempting to remove workflow instances.", e);
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
if (response.getWriter() != null) {
response.getWriter().write(e.getMessage());
}
}
}
use of org.apache.sling.commons.json.JSONArray in project acs-aem-commons by Adobe-Consulting-Services.
the class InitServlet method getFormJSONObject.
/**
* Get the JSON data to populate the Workflow Removal form.
*
* @param resourceResolver
* @return
* @throws WorkflowException
* @throws JSONException
*/
private JSONObject getFormJSONObject(final ResourceResolver resourceResolver) throws WorkflowException, JSONException {
final JSONObject json = new JSONObject();
final WorkflowSession workflowSession = workflowService.getWorkflowSession(resourceResolver.adaptTo(Session.class));
final WorkflowModel[] workflowModels = workflowSession.getModels();
for (final WorkflowModel workflowModel : workflowModels) {
final JSONObject jsonWorkflow = new JSONObject();
jsonWorkflow.put("title", workflowModel.getTitle());
jsonWorkflow.put("id", workflowModel.getId());
json.accumulate("workflowModels", jsonWorkflow);
}
json.put("statuses", new JSONArray(Arrays.asList(WORKFLOW_STATUSES)));
return json;
}
Aggregations