Search in sources :

Example 16 with JSONArray

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();
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) JSONArray(org.apache.sling.commons.json.JSONArray)

Example 17 with JSONArray

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());
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Resource(org.apache.sling.api.resource.Resource) JSONArray(org.apache.sling.commons.json.JSONArray) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with JSONArray

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);
    }
}
Also used : XSSAPI(org.apache.sling.xss.XSSAPI) ServletException(javax.servlet.ServletException) JSONObject(org.apache.sling.commons.json.JSONObject) JSONArray(org.apache.sling.commons.json.JSONArray) JSONException(org.apache.sling.commons.json.JSONException) ServiceReference(org.osgi.framework.ServiceReference)

Example 19 with JSONArray

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());
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) JSONObject(org.apache.sling.commons.json.JSONObject) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) JSONArray(org.apache.sling.commons.json.JSONArray) ServletException(javax.servlet.ServletException) WorkflowRemovalForceQuitException(com.adobe.acs.commons.workflow.bulk.removal.WorkflowRemovalForceQuitException) IOException(java.io.IOException) WorkflowRemovalForceQuitException(com.adobe.acs.commons.workflow.bulk.removal.WorkflowRemovalForceQuitException)

Example 20 with JSONArray

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;
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) WorkflowSession(com.day.cq.workflow.WorkflowSession) JSONArray(org.apache.sling.commons.json.JSONArray) WorkflowModel(com.day.cq.workflow.model.WorkflowModel) Session(javax.jcr.Session) WorkflowSession(com.day.cq.workflow.WorkflowSession)

Aggregations

JSONArray (org.apache.sling.commons.json.JSONArray)23 JSONObject (org.apache.sling.commons.json.JSONObject)22 Map (java.util.Map)4 ValueMap (org.apache.sling.api.resource.ValueMap)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 PathFilterSet (org.apache.jackrabbit.vault.fs.api.PathFilterSet)3 Resource (org.apache.sling.api.resource.Resource)3 Page (com.day.cq.wcm.api.Page)2 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 Session (javax.jcr.Session)2 ServletException (javax.servlet.ServletException)2 RequestParameter (org.apache.sling.api.request.RequestParameter)2 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)2 JSONException (org.apache.sling.commons.json.JSONException)2 Result (com.adobe.acs.commons.quickly.results.Result)1 WorkflowRemovalForceQuitException (com.adobe.acs.commons.workflow.bulk.removal.WorkflowRemovalForceQuitException)1 ClientLibrary (com.adobe.granite.ui.clientlibs.ClientLibrary)1