Search in sources :

Example 31 with JSONObject

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

the class WorkflowModelFilterPageInfoProvider method filter.

@SuppressWarnings("squid:S3776")
private void filter(JSONObject typeObject, String resourcePath, ResourceResolver resourceResolver) throws JSONException {
    final JSONArray models = typeObject.getJSONArray(KEY_MODELS);
    final JSONArray newModels = new JSONArray();
    for (int i = 0; i < models.length(); i++) {
        final JSONObject modelObject = models.getJSONObject(i);
        final String path = modelObject.getString(KEY_MODEL_PATH);
        final Resource modelResource = resourceResolver.getResource(path);
        if (modelResource != null) {
            // we're looking for the appliesTo property on the jcr:content node, the wid value
            // is the path to the jcr:content/model node.
            final ValueMap properties = modelResource.getParent().getValueMap();
            final String[] allowedPaths = properties.get(PN_ALLOWED_PATHS, String[].class);
            if (allowedPaths == null) {
                newModels.put(modelObject);
            } else {
                for (final String allowedPath : allowedPaths) {
                    if (resourcePath.matches(allowedPath)) {
                        newModels.put(modelObject);
                        break;
                    }
                }
            }
        }
    }
    typeObject.put(KEY_MODELS, newModels);
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) ValueMap(org.apache.sling.api.resource.ValueMap) JSONArray(org.apache.sling.commons.json.JSONArray) Resource(org.apache.sling.api.resource.Resource)

Example 32 with JSONObject

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

the class CQIncludePropertyNamespaceServlet method doGet.

// @formatter:on
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    if (!this.accepts(request)) {
        response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);
        response.getWriter().write(new JSONObject().toString());
    }
    /* Servlet accepts this request */
    RequestUtil.setRequestAttribute(request, REQ_ATTR, true);
    final String namespace = URLDecoder.decode(PathInfoUtil.getSelector(request, NAME_PROPERTY_SELECTOR_INDEX), "UTF-8");
    final RequestDispatcherOptions options = new RequestDispatcherOptions();
    options.setReplaceSelectors(AEM_CQ_INCLUDE_SELECTORS);
    final BufferingResponse bufferingResponse = new BufferingResponse(response);
    request.getRequestDispatcher(request.getResource(), options).forward(request, bufferingResponse);
    try {
        final JSONObject json = new JSONObject(bufferingResponse.getContents());
        final PropertyNamespaceUpdater propertyNamespaceUpdater = new PropertyNamespaceUpdater(namespace);
        propertyNamespaceUpdater.accept(json);
        response.getWriter().write(json.toString());
    } catch (JSONException e) {
        log.error("Error composing the cqinclude JSON representation of the widget overlay for [ {} ]", request.getRequestURI(), e);
        response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        response.getWriter().write(new JSONObject().toString());
    }
}
Also used : BufferingResponse(com.adobe.acs.commons.util.BufferingResponse) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) JSONObject(org.apache.sling.commons.json.JSONObject) JSONException(org.apache.sling.commons.json.JSONException)

Example 33 with JSONObject

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

the class RTEConfigurationServlet method createEmptyWidget.

@Override
protected JSONObject createEmptyWidget(String rteName) throws JSONException {
    JSONObject object = new JSONObject();
    object.put("xtype", "richtext");
    object.put("name", "./" + xssApi.encodeForJSString(rteName));
    object.put("hideLabel", true);
    object.put("jcr:primaryType", "cq:Widget");
    return object;
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

Example 34 with JSONObject

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

the class TagWidgetConfigurationServlet method writeConfigResource.

private void writeConfigResource(Resource resource, String propertyName, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, JSONException, ServletException {
    JSONObject widget = createEmptyWidget(propertyName);
    RequestParameterMap map = request.getRequestParameterMap();
    for (Map.Entry<String, RequestParameter[]> entry : map.entrySet()) {
        String key = entry.getKey();
        RequestParameter[] params = entry.getValue();
        if (params != null) {
            if (params.length > 1) {
                JSONArray arr = new JSONArray();
                for (int i = 0; i < params.length; i++) {
                    arr.put(params[i].getString());
                }
                widget.put(key, arr);
            } else if (params.length == 1) {
                widget.put(key, params[0].getString());
            }
        }
    }
    widget = underlay(widget, resource);
    JSONObject parent = new JSONObject();
    parent.put("xtype", "dialogfieldset");
    parent.put("border", false);
    parent.put("padding", 0);
    parent.put("style", "padding: 0px");
    parent.accumulate("items", widget);
    parent.write(response.getWriter());
}
Also used : RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) JSONObject(org.apache.sling.commons.json.JSONObject) RequestParameter(org.apache.sling.api.request.RequestParameter) JSONArray(org.apache.sling.commons.json.JSONArray) RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) Map(java.util.Map)

Example 35 with JSONObject

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

the class TagWidgetConfigurationServlet method createEmptyWidget.

@Override
protected JSONObject createEmptyWidget(String propertyName) throws JSONException {
    JSONObject object = new JSONObject();
    object.put("xtype", "tags");
    object.put("name", "./" + xssApi.encodeForJSString(propertyName));
    object.put("fieldLabel", "Tags/Keywords");
    object.put("jcr:primaryType", "cq:Widget");
    return object;
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

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