Search in sources :

Example 21 with JSONArray

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

the class RTEConfigurationServlet method writeConfigResource.

private void writeConfigResource(Resource resource, String rteName, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, JSONException, ServletException {
    JSONObject widget = createEmptyWidget(rteName);
    // these two size properties seem to be necessary to get the size correct
    // in a component dialog
    widget.put("width", RTE_WIDTH);
    widget.put("height", RTE_HEIGHT);
    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 || EXTERNAL_STYLESHEETS_PROPERTY.equals(key)) {
                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());
            }
        }
    }
    if (widget.has("fieldLabel")) {
        widget.remove("hideLabel");
    }
    JSONObject config = toJSONObject(resource);
    if (config == null) {
        config = new JSONObject();
    }
    if (config.optBoolean("includeDefault")) {
        config = underlay(config, resource.getResourceResolver().getResource(DEFAULT_CONFIG));
    }
    widget.put("rtePlugins", config);
    JSONObject parent = new JSONObject();
    parent.put("xtype", "dialogfieldset");
    parent.put("border", false);
    parent.put("padding", 0);
    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 22 with JSONArray

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

the class SharedComponentPropertiesPageInfoProvider method updatePageInfo.

/**
 * Add a "sharedComponentProperties" section to pageInfo so that JS libs in the authoring interface
 * can determine whether or not to enable shared/global properties for a component on a site.
 */
@Override
public void updatePageInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
    if (scheduledSharedComponentsMapUpdate > 0 && System.currentTimeMillis() > scheduledSharedComponentsMapUpdate) {
        scheduledSharedComponentsMapUpdate = -1L;
        updateSharedComponentsMap();
    }
    JSONObject props = new JSONObject();
    props.put("enabled", false);
    Page page = pageRootProvider.getRootPage(resource);
    if (page != null) {
        Session session = request.getResourceResolver().adaptTo(Session.class);
        try {
            AccessControlManager accessControlManager = AccessControlUtil.getAccessControlManager(session);
            Privilege privilegeAddChild = accessControlManager.privilegeFromName("jcr:addChildNodes");
            Privilege privilegeModifyProps = accessControlManager.privilegeFromName("jcr:modifyProperties");
            Privilege[] requiredPrivs = new Privilege[] { privilegeAddChild, privilegeModifyProps };
            if (accessControlManager.hasPrivileges(page.getPath() + "/jcr:content", requiredPrivs)) {
                props.put("enabled", true);
                props.put("root", page.getPath());
                props.put("components", Maps.transformValues(componentsWithSharedProperties, (Function<List<Boolean>, Object>) JSONArray::new));
            } else {
                log.debug("User does not have [ {} ] on [ {} ]", requiredPrivs, page.getPath() + "/jcr:content");
            }
        } catch (RepositoryException e) {
            log.error("Unexpected error checking permissions to modify shared component properties", e);
        }
    } else {
        log.debug("No Page Root could be found for [ {} ]", resource.getPath());
    }
    info.put("sharedComponentProperties", props);
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) Function(com.google.common.base.Function) JSONObject(org.apache.sling.commons.json.JSONObject) JSONArray(org.apache.sling.commons.json.JSONArray) Page(com.day.cq.wcm.api.Page) RepositoryException(javax.jcr.RepositoryException) Privilege(javax.jcr.security.Privilege) Session(javax.jcr.Session)

Example 23 with JSONArray

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

the class DataSourceBuilderImpl method writeDataSourceOptions.

@Override
public void writeDataSourceOptions(final SlingHttpServletRequest slingRequest, final SlingHttpServletResponse slingResponse) throws JSONException, IOException {
    final DataSource datasource = (DataSource) slingRequest.getAttribute(DataSource.class.getName());
    final JSONArray jsonArray = new JSONArray();
    if (datasource != null) {
        final Iterator<Resource> iterator = datasource.iterator();
        if (iterator != null) {
            while (iterator.hasNext()) {
                final Resource dataResource = iterator.next();
                if (dataResource != null) {
                    final ValueMap dataProps = dataResource.adaptTo(ValueMap.class);
                    if (dataProps != null) {
                        final JSONObject json = new JSONObject();
                        json.put(TEXT, dataProps.get(TEXT, ""));
                        json.put(VALUE, dataProps.get(VALUE, ""));
                        jsonArray.put(json);
                    }
                }
            }
        }
    }
    slingResponse.setContentType("application/json; charset=UTF-8");
    slingResponse.setCharacterEncoding("UTF-8");
    slingResponse.getWriter().write(jsonArray.toString());
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) ValueMap(org.apache.sling.api.resource.ValueMap) JSONArray(org.apache.sling.commons.json.JSONArray) ValueMapResource(com.adobe.granite.ui.components.ds.ValueMapResource) Resource(org.apache.sling.api.resource.Resource) EmptyDataSource(com.adobe.granite.ui.components.ds.EmptyDataSource) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource)

Aggregations

JSONArray (org.apache.sling.commons.json.JSONArray)23 JSONObject (org.apache.sling.commons.json.JSONObject)22 Map (java.util.Map)4 ValueMap (org.apache.sling.api.resource.ValueMap)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 PathFilterSet (org.apache.jackrabbit.vault.fs.api.PathFilterSet)3 Resource (org.apache.sling.api.resource.Resource)3 Page (com.day.cq.wcm.api.Page)2 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 Session (javax.jcr.Session)2 ServletException (javax.servlet.ServletException)2 RequestParameter (org.apache.sling.api.request.RequestParameter)2 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)2 JSONException (org.apache.sling.commons.json.JSONException)2 Result (com.adobe.acs.commons.quickly.results.Result)1 WorkflowRemovalForceQuitException (com.adobe.acs.commons.workflow.bulk.removal.WorkflowRemovalForceQuitException)1 ClientLibrary (com.adobe.granite.ui.clientlibs.ClientLibrary)1