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