Search in sources :

Example 21 with JSONException

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

the class PostRedirectGetFormHelperImpl method getGetForm.

/**
 * Derives the form from the request's Query Parameters as best it can
 * <p>
 * Falls back to an empty form if it runs into problems.
 * Fallback is due to ease of (inadvertent) tampering with query params
 *
 * @param formName
 * @param request
 * @return
 */
protected Form getGetForm(final String formName, final SlingHttpServletRequest request, final SlingHttpServletResponse response) {
    Map<String, String> data = new HashMap<String, String>();
    Map<String, String> errors = new HashMap<String, String>();
    final String requestData = getRawFormData(formName, request, response);
    if (StringUtils.isBlank(requestData)) {
        return new FormImpl(formName, request.getResource().getPath());
    }
    try {
        final JSONObject jsonData = new JSONObject(requestData);
        final String incomingFormName = jsonData.optString(KEY_FORM_NAME);
        // Double-check the form names; only inject matching forms
        if (StringUtils.equals(incomingFormName, formName)) {
            final JSONObject incomingJsonForm = jsonData.optJSONObject(KEY_FORM);
            if (incomingJsonForm != null) {
                data = TypeUtil.toMap(incomingJsonForm, String.class);
                log.debug("Form data: {}", data);
            }
            final JSONObject incomingJsonErrors = jsonData.optJSONObject(KEY_ERRORS);
            if (incomingJsonErrors != null) {
                errors = TypeUtil.toMap(incomingJsonErrors, String.class);
                log.debug("Form data: {}", errors);
            }
        }
    } catch (JSONException e) {
        log.warn("Cannot parse query parameters for request: {}", requestData);
        return new FormImpl(formName, request.getResource().getPath());
    }
    return new FormImpl(formName, request.getResource().getPath(), this.getProtectedData(data), this.getProtectedErrors(errors));
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) FormImpl(com.adobe.acs.commons.forms.impl.FormImpl) JSONException(org.apache.sling.commons.json.JSONException)

Example 22 with JSONException

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

the class RTEConfigurationServlet method doGet.

@Override
@SuppressWarnings("squid:S3776")
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    String componentPath = request.getResource().getPath();
    String configName = PathInfoUtil.getSelector(request, 1, DEFAULT_CONFIG_NAME);
    // the actual property name
    String rteName = PathInfoUtil.getSelector(request, 2, "text");
    Resource root = request.getResourceResolver().getResource(rootPath);
    if (root != null) {
        Iterator<Resource> children = root.listChildren();
        while (children.hasNext()) {
            Resource child = children.next();
            if (matches(componentPath, child)) {
                Resource config = child.getChild(configName);
                if (config == null) {
                    config = child.getChild(DEFAULT_CONFIG_NAME);
                }
                if (config != null) {
                    try {
                        writeConfigResource(config, rteName, request, response);
                    } catch (JSONException e) {
                        throw new ServletException(e);
                    }
                    return;
                }
            }
        }
    }
    returnDefault(rteName, request, response);
}
Also used : ServletException(javax.servlet.ServletException) Resource(org.apache.sling.api.resource.Resource) JSONException(org.apache.sling.commons.json.JSONException)

Example 23 with JSONException

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

the class RTEConfigurationServlet method returnDefault.

private void returnDefault(String rteName, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, ServletException {
    response.setContentType("application/json");
    try {
        Resource root = request.getResourceResolver().getResource(DEFAULT_CONFIG);
        if (root == null) {
            writeEmptyWidget(rteName, response);
            return;
        }
        writeConfigResource(root, rteName, request, response);
    } catch (JSONException e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) Resource(org.apache.sling.api.resource.Resource) JSONException(org.apache.sling.commons.json.JSONException)

Example 24 with JSONException

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

the class TagWidgetConfigurationServlet method returnDefault.

private void returnDefault(String propertyName, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, ServletException {
    response.setContentType("application/json");
    try {
        Resource root = request.getResourceResolver().getResource(DEFAULT_CONFIG);
        if (root == null) {
            writeEmptyWidget(propertyName, response);
            return;
        }
        writeConfigResource(root, propertyName, request, response);
    } catch (JSONException e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) Resource(org.apache.sling.api.resource.Resource) JSONException(org.apache.sling.commons.json.JSONException)

Example 25 with JSONException

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

the class TagWidgetConfigurationServlet method doGet.

@Override
@SuppressWarnings("squid:S3776")
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    String componentPath = request.getResource().getPath();
    String configName = PathInfoUtil.getSelector(request, 1, DEFAULT_CONFIG_NAME);
    // the actual property name
    String propertyName = PathInfoUtil.getSelector(request, 2, "tags");
    Resource root = request.getResourceResolver().getResource(rootPath);
    if (root != null) {
        Iterator<Resource> children = root.listChildren();
        while (children.hasNext()) {
            Resource child = children.next();
            if (matches(componentPath, child)) {
                Resource config = child.getChild(configName);
                if (config == null) {
                    config = child.getChild(DEFAULT_CONFIG_NAME);
                }
                if (config != null) {
                    try {
                        writeConfigResource(config, propertyName, request, response);
                    } catch (JSONException e) {
                        throw new ServletException(e);
                    }
                    return;
                }
            }
        }
    }
    returnDefault(propertyName, request, response);
}
Also used : ServletException(javax.servlet.ServletException) Resource(org.apache.sling.api.resource.Resource) JSONException(org.apache.sling.commons.json.JSONException)

Aggregations

JSONException (org.apache.sling.commons.json.JSONException)25 JSONObject (org.apache.sling.commons.json.JSONObject)15 Resource (org.apache.sling.api.resource.Resource)10 ServletException (javax.servlet.ServletException)9 RepositoryException (javax.jcr.RepositoryException)6 ValueMap (org.apache.sling.api.resource.ValueMap)6 HashMap (java.util.HashMap)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 Map (java.util.Map)3 Page (com.day.cq.wcm.api.Page)2 PageManager (com.day.cq.wcm.api.PageManager)2 PathFilterSet (org.apache.jackrabbit.vault.fs.api.PathFilterSet)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 JSONArray (org.apache.sling.commons.json.JSONArray)2 JSONWriter (org.apache.sling.commons.json.io.JSONWriter)2 ActionManager (com.adobe.acs.commons.fam.ActionManager)1 FormImpl (com.adobe.acs.commons.forms.impl.FormImpl)1