Search in sources :

Example 71 with JSONObject

use of org.apache.sling.commons.json.JSONObject 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 72 with JSONObject

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

the class WorkflowModelFilterPageInfoProvider method updatePageInfo.

@Override
public void updatePageInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
    if (info.has(KEY_WORKFLOWS)) {
        final JSONObject workflows = info.getJSONObject(KEY_WORKFLOWS);
        final String resourcePath = resource.getPath();
        final ResourceResolver resourceResolver = resource.getResourceResolver();
        for (final Iterator<String> types = workflows.keys(); types.hasNext(); ) {
            final String type = types.next();
            final JSONObject typeObject = workflows.getJSONObject(type);
            filter(typeObject, resourcePath, resourceResolver);
        }
    } else {
        log.warn("No workflows found in existing page info. Check order of cq:infoProviders.");
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Example 73 with JSONObject

use of org.apache.sling.commons.json.JSONObject 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 74 with JSONObject

use of org.apache.sling.commons.json.JSONObject 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

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