use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class AbstractResultSerializer method toJSON.
public JSONObject toJSON(final Result result) throws JSONException {
final JSONObject json = new JSONObject();
json.put("title", result.getTitle());
json.put("type", result.getResultType());
json.put("description", result.getDescription());
json.put("path", result.getPath());
json.put("action", this.toJSON(result.getAction()));
json.put("secondaryAction", this.toJSON(result.getSecondaryAction()));
return json;
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class AbstractResultSerializer method toJSON.
public JSONObject toJSON(final Action action) throws JSONException {
final JSONObject json = new JSONObject();
if (action != null) {
json.put("uri", action.getUri());
json.put("method", action.getMethod());
json.put("target", action.getTarget());
json.put("xhr", false);
json.put("script", action.getScript());
json.put("params", new JSONObject(action.getParams()));
}
return json;
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class ChildrenAsPropertyResource method deserializeToSyntheticChildResources.
/**
* Converts a JSONObject to the list of SyntheticChildAsPropertyResources.
*
* @param jsonObject the JSONObject to deserialize.
* @return the list of SyntheticChildAsPropertyResources the jsonObject represents.
* @throws JSONException
*/
protected final List<SyntheticChildAsPropertyResource> deserializeToSyntheticChildResources(JSONObject jsonObject) throws JSONException {
final List<SyntheticChildAsPropertyResource> resources = new ArrayList<SyntheticChildAsPropertyResource>();
final Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
final String nodeName = keys.next();
JSONObject entryJSON = jsonObject.optJSONObject(nodeName);
if (entryJSON == null) {
continue;
}
final ValueMap properties = new ValueMapDecorator(new HashMap<String, Object>());
final Iterator<String> propertyNames = entryJSON.keys();
while (propertyNames.hasNext()) {
final String propName = propertyNames.next();
properties.put(propName, entryJSON.optString(propName));
}
resources.add(new SyntheticChildAsPropertyResource(this.getParent(), nodeName, properties));
}
return resources;
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class ChildrenAsPropertyResource method serialize.
/**
* Serializes all children data as JSON to the resource's propertyName.
*
* @throws InvalidDataFormatException
*/
private void serialize() throws InvalidDataFormatException {
final long start = System.currentTimeMillis();
final ModifiableValueMap modifiableValueMap = this.resource.adaptTo(ModifiableValueMap.class);
JSONObject childrenJSON = new JSONObject();
try {
// Add the new entries to the JSON
for (Resource childResource : this.orderedCache) {
childrenJSON.put(childResource.getName(), this.serializeToJSON(childResource));
}
if (childrenJSON.length() > 0) {
// Persist the JSON back to the Node
modifiableValueMap.put(this.propertyName, childrenJSON.toString());
} else {
// Nothing to persist; delete the property
modifiableValueMap.remove(this.propertyName);
}
log.debug("Persist operation for [ {} ] in [ {} ms ]", this.resource.getPath() + "/" + this.propertyName, System.currentTimeMillis() - start);
} catch (JSONException e) {
throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
} catch (NoSuchMethodException e) {
throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
} catch (IllegalAccessException e) {
throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
} catch (InvocationTargetException e) {
throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
}
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class QrCodeServlet method doGet.
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
if (externalizer == null) {
log.warn("Externalizer is not configured. This is required for QR Code servlet to work.");
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} else if (request.getResource().getValueMap().get(PN_ENABLED, false)) {
final JSONObject json = new JSONObject();
final String publishUrl = externalizer.publishLink(request.getResourceResolver(), request.getRequestPathInfo().getSuffix());
log.debug("Externalized path [ {} ] for QR Code generation to [ {} ]", request.getRequestPathInfo().getSuffix(), publishUrl);
if (StringUtils.isNotBlank(publishUrl)) {
try {
json.put(JSON_KEY_ENABLED, true);
json.put(JSON_KEY_PUBLISH_URL, publishUrl);
} catch (JSONException e) {
log.error("Could not construct the QR Code Servlet JSON response", e);
throw new ServletException(e);
}
response.getWriter().write(json.toString());
response.getWriter().flush();
} else {
log.warn("Externalizer configuration for AEM Publish did not yield a valid URL");
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
} else {
log.warn("Externalizer configuration for AEM Publish did not yield a valid URL");
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
Aggregations