Search in sources :

Example 66 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class ResumeServlet method doPost.

@Override
protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    final JSONObject params;
    try {
        params = new JSONObject(request.getParameter("params"));
        final Config config = request.getResource().adaptTo(Config.class);
        int throttle = params.optInt("throttle", -1);
        int interval = params.optInt("interval", -1);
        if (throttle > -1) {
            config.setThrottle(throttle);
            config.commit();
        } else if (interval > -1) {
            config.setInterval(interval);
            config.commit();
        }
        bulkWorkflowEngine.resume(config);
        response.sendRedirect(request.getResourceResolver().map(request, request.getResource().getPath()) + ".status.json");
    } catch (JSONException e) {
        log.error("Could not resume Bulk Workflow due to: {}", e);
        JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not resume Bulk Workflow.", e.getMessage());
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Config(com.adobe.acs.commons.workflow.bulk.execution.model.Config) JSONException(org.apache.sling.commons.json.JSONException)

Example 67 with JSONObject

use of org.apache.sling.commons.json.JSONObject 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)

Example 68 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class WorkflowRemovalStatus method getJSON.

public JSONObject getJSON() throws JSONException {
    final JSONObject json = new JSONObject();
    json.put(KEY_RUNNING, this.isRunning());
    json.put(KEY_INITIATED_BY, this.getInitiatedBy());
    json.put(KEY_CHECKED_COUNT, this.getChecked());
    json.put(KEY_REMOVED_COUNT, this.getRemoved());
    if (this.getStartedAt() != null) {
        json.put(KEY_STARTED_AT, this.getStartedAt());
    }
    if (this.getErredAt() != null) {
        json.put(KEY_ERRED_AT, this.getErredAt());
        json.put(KEY_DURATION, getDuration(this.startedAt, this.erredAt));
    } else if (this.getForceQuitAt() != null) {
        json.put(KEY_FORCE_QUIT_AT, this.getForceQuitAt());
        json.put(KEY_DURATION, getDuration(this.startedAt, this.forceQuitAt));
    } else if (this.getCompletedAt() != null) {
        json.put(KEY_COMPLETED_AT, this.getCompletedAt());
        json.put(KEY_DURATION, getDuration(this.startedAt, this.completedAt));
    } else {
        json.put(KEY_DURATION, getDuration(this.startedAt, Calendar.getInstance()));
    }
    return json;
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

Example 69 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class PostRedirectGetFormHelperImpl method getQueryParameterValue.

protected final String getQueryParameterValue(Form form) throws JSONException {
    boolean hasData = false;
    final JSONObject jsonData = new JSONObject();
    form = this.clean(form);
    jsonData.put(KEY_FORM_NAME, form.getName());
    if (form.hasData()) {
        final JSONObject jsonForm = new JSONObject(form.getData());
        jsonData.put(KEY_FORM, jsonForm);
        hasData = true;
    }
    if (form.hasErrors()) {
        final JSONObject jsonError = new JSONObject(form.getErrors());
        jsonData.put(KEY_ERRORS, jsonError);
        hasData = true;
    }
    return hasData ? this.encode(jsonData.toString()) : "";
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

Example 70 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class PostRedirectGetFormHelperImpl method getGetForm.

/**
 * Derives the form from the request's Query Parameters as best it can
 * <p>
 * Falls back to an empty form if it runs into problems.
 * Fallback is due to ease of (inadvertent) tampering with query params
 *
 * @param formName
 * @param request
 * @return
 */
protected Form getGetForm(final String formName, final SlingHttpServletRequest request, final SlingHttpServletResponse response) {
    Map<String, String> data = new HashMap<String, String>();
    Map<String, String> errors = new HashMap<String, String>();
    final String requestData = getRawFormData(formName, request, response);
    if (StringUtils.isBlank(requestData)) {
        return new FormImpl(formName, request.getResource().getPath());
    }
    try {
        final JSONObject jsonData = new JSONObject(requestData);
        final String incomingFormName = jsonData.optString(KEY_FORM_NAME);
        // Double-check the form names; only inject matching forms
        if (StringUtils.equals(incomingFormName, formName)) {
            final JSONObject incomingJsonForm = jsonData.optJSONObject(KEY_FORM);
            if (incomingJsonForm != null) {
                data = TypeUtil.toMap(incomingJsonForm, String.class);
                log.debug("Form data: {}", data);
            }
            final JSONObject incomingJsonErrors = jsonData.optJSONObject(KEY_ERRORS);
            if (incomingJsonErrors != null) {
                errors = TypeUtil.toMap(incomingJsonErrors, String.class);
                log.debug("Form data: {}", errors);
            }
        }
    } catch (JSONException e) {
        log.warn("Cannot parse query parameters for request: {}", requestData);
        return new FormImpl(formName, request.getResource().getPath());
    }
    return new FormImpl(formName, request.getResource().getPath(), this.getProtectedData(data), this.getProtectedErrors(errors));
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) FormImpl(com.adobe.acs.commons.forms.impl.FormImpl) JSONException(org.apache.sling.commons.json.JSONException)

Aggregations

JSONObject (org.apache.sling.commons.json.JSONObject)74 JSONArray (org.apache.sling.commons.json.JSONArray)22 Test (org.junit.Test)20 JSONException (org.apache.sling.commons.json.JSONException)16 HashMap (java.util.HashMap)15 ValueMap (org.apache.sling.api.resource.ValueMap)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 ServletException (javax.servlet.ServletException)6 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)6 Resource (org.apache.sling.api.resource.Resource)6 RepositoryException (javax.jcr.RepositoryException)5 Calendar (java.util.Calendar)4 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)4 Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 Session (javax.jcr.Session)3 ModifiableValueMapDecorator (org.apache.sling.api.wrappers.ModifiableValueMapDecorator)3 Form (com.adobe.acs.commons.forms.Form)2