/** * Make a prettyprinted JSON text of this JSONObject. * <p> * Warning: This method assumes that the data structure is acyclical. * @param indentFactor The number of spaces to add to each level of * indentation. * @return a printable, displayable, portable, transmittable * representation of the object, beginning * with <code>{</code> <small>(left brace)</small> and ending * with <code>}</code> <small>(right brace)</small>. * @throws JSONException If the object contains an invalid number. */ public String toString(int indentFactor) throws JSONException { return toString(indentFactor, 0); }
private void addListingFile(JSONObject fileListing, String cachePath, Session adminSession) throws RepositoryException, JSONException { JcrUtil.createPath(cachePath, "sling:Folder", "nt:file", adminSession, false); Node cacheContentNode = JcrUtil.createPath(cachePath + "/jcr:content", "nt:resource", adminSession); Calendar calTS = Calendar.getInstance(); cacheContentNode.setProperty("jcr:data", adminSession.getValueFactory().createBinary(new ByteArrayInputStream(fileListing.toString(3).getBytes()))); cacheContentNode.setProperty("jcr:lastModified", calTS); adminSession.save(); }
private void buildJson() { Map<String, Object> objectMap = new HashMap<>(); objectMap.put(Image.JSON_SMART_SIZES, new JSONArray(Arrays.asList(ArrayUtils.toObject(smartSizes)))); objectMap.put(Image.JSON_SMART_IMAGES, new JSONArray(Arrays.asList(smartImages))); objectMap.put(Image.JSON_LAZY_ENABLED, !disableLazyLoading); json = new JSONObject(objectMap).toString(); }
private void writeJsonResponse(final List<String> categories, final SlingHttpServletResponse response) throws JSONException, IOException { final JSONObject jsonObject = new JSONObject(); jsonObject.put("categories", new JSONArray(categories)); response.getWriter().print(jsonObject.toString()); }
public void triggerReload(String path) throws JSONException { if (group != null) { JSONObject reload = createReloadObject(path); group.flushAndWrite(new TextWebSocketFrame(reload.toString()), matcher); } }
public void triggerReload(String path) throws JSONException { if (group != null) { JSONObject reload = createReloadObject(path); group.flushAndWrite(new TextWebSocketFrame(reload.toString()), matcher); } }
/** * {@inheritDoc} */ public String getErrorJSON(final String msg) { final JSONObject json = new JSONObject(); try { json.put("status", "error"); json.put("msg", msg); return json.toString(); } catch (JSONException e) { log.error("Error creating JSON Error response message: {}", e.getMessage()); return JSON_EXCEPTION_MSG; } }
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { final Command cmd = new Command(request); response.setHeader("Content-Type", " application/json; charset=UTF-8"); try { response.getWriter().append(quicklyEngine.execute(request, response, cmd).toString()); } catch (JSONException e) { response.sendError(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.getWriter().print("{\"status:\": \"error\"}"); } } }
private void sendJSONError(SlingHttpServletResponse response, String title, String message) throws IOException { final JSONObject json = new JSONObject(); response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); try { json.put("title", title); json.put("message", message); response.getWriter().write(json.toString()); } catch (JSONException e) { String fallbackJSON = "{ \"title\": \"Error creating error response. " + "Please review AEM error logs.\" }"; response.getWriter().write(fallbackJSON); } }
@Override public String startTranscriptionJob(InputStream stream, String mimeType) { Request request = httpClientFactory.post("/speech-to-text/api/v1/recognitions?continuous=true×tamps=true"). addHeader("Content-Type", mimeType). bodyStream(stream); try { JSONObject json = httpClientFactory.getExecutor().execute(request).handleResponse(HANDLER); log.trace("content: {}", json.toString(2)); return json.getString("id"); } catch (Exception e) { log.error("error submitting job", e); return null; } }
@Override public final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); try { if (workflowInstanceRemover.getStatus() != null) { response.getWriter().write(workflowInstanceRemover.getStatus().getJSON().toString()); } else { WorkflowRemovalStatus workflowStatus = new WorkflowRemovalStatus(request.getResourceResolver()); workflowStatus.setRunning(false); response.getWriter().write(workflowStatus.getJSON().toString()); } } catch (Exception e) { log.error("Unable to create Workflow Removal status", e); response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.getWriter().write(e.getMessage()); } } }
@Override public final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); final JSONObject json = new JSONObject(); try { // Only populate the form if removal is not running. json.put("form", this.getFormJSONObject(request.getResourceResolver())); response.getWriter().write(json.toString()); } catch (Exception e) { response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.getWriter().write(e.getMessage()); } }
public static void sendJSONError(SlingHttpServletResponse response, int statusCode, String title, String message) throws IOException { response.setStatus(statusCode); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); JSONObject json = new JSONObject(); try { json.put("title", title); json.put("message", message); response.getWriter().write(json.toString()); } catch (JSONException e) { String jsonString = "{title: \"Error constructing error message\"}"; response.getWriter().write(jsonString); } } }
/** * get the current resource * * @param depth * @return */ public String currentResource(int depth) { SlingHttpServletRequest request = (SlingHttpServletRequest) context.getBindings(ScriptContext.ENGINE_SCOPE).get(SlingBindings.REQUEST); try { return JsonObjectCreator.create(request.getResource(), depth).toString(); } catch (JSONException e) { throw new TechnicalException("could not get current resource", e); } }
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { response.setHeader("Content-Type", " application/json; charset=UTF-8"); final JSONObject json = new JSONObject(); try { json.put("user", request.getResourceResolver().getUserID()); json.put("throttle", 200); response.getWriter().write(json.toString()); } catch (JSONException e) { response.sendError(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.getWriter().print("{\"status:\": \"error\"}"); } } }
/** * Serializes an OSGi {@link org.osgi.service.event.Event} into a JSON object string * * @param event the event to be serialized as * @return a serialized JSON object * @throws org.apache.sling.commons.json.JSONException */ protected static String constructMessage(Event event) throws JSONException { JSONObject obj = new JSONObject(); for (String prop : event.getPropertyNames()) { Object val = event.getProperty(prop); Object converted = convertValue(val); obj.put(prop, converted == null ? val : converted); } obj.put(PROP_TIMESTAMP, ISO8601.format(Calendar.getInstance())); return obj.toString(); }
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { if (!editorConfig.isEnabled()) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } Resource contextResource = request.getResource(); try { JSONObject result = new JSONObject(); result.putOpt("contextPath", getContextPath(contextResource)); result.put("configNames", getConfigNames(contextResource)); response.setContentType("application/json;charset=" + CharEncoding.UTF_8); response.getWriter().write(result.toString()); } catch (JSONException ex) { throw new ServletException("Unable to generate JSON.", ex); } }
/** * {@inheritDoc} */ public String getPathFilterSetPreviewJSON(final Collection<PathFilterSet> pathFilterSets) throws JSONException { final JSONObject json = new JSONObject(); json.put("status", "preview"); json.put("path", "Not applicable (Preview)"); json.put("filterSets", new JSONArray()); for (final PathFilterSet pathFilterSet : pathFilterSets) { final JSONObject tmp = new JSONObject(); tmp.put("importMode", "Not applicable (Preview)"); tmp.put("rootPath", pathFilterSet.getRoot()); json.accumulate("filterSets", tmp); } return json.toString(); }
/** * {@inheritDoc} */ public String getSuccessJSON(final JcrPackage jcrPackage) throws JSONException, RepositoryException { final JSONObject json = new JSONObject(); json.put("status", "success"); json.put("path", jcrPackage.getNode().getPath()); json.put("filterSets", new JSONArray()); final List<PathFilterSet> filterSets = jcrPackage.getDefinition().getMetaInf().getFilter().getFilterSets(); for (final PathFilterSet filterSet : filterSets) { final JSONObject jsonFilterSet = new JSONObject(); jsonFilterSet.put("importMode", filterSet.getImportMode().name()); jsonFilterSet.put("rootPath", filterSet.getRoot()); json.accumulate("filterSets", jsonFilterSet); } return json.toString(); }
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()) : ""; } }