Search in sources :

Example 26 with JSONObject

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;
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

Example 27 with JSONObject

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;
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

Example 28 with JSONObject

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;
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ArrayList(java.util.ArrayList) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) JSONObject(org.apache.sling.commons.json.JSONObject)

Example 29 with JSONObject

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());
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Resource(org.apache.sling.api.resource.Resource) JSONException(org.apache.sling.commons.json.JSONException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 30 with JSONObject

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);
    }
}
Also used : ServletException(javax.servlet.ServletException) JSONObject(org.apache.sling.commons.json.JSONObject) 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