Search in sources :

Example 1 with FormImpl

use of com.adobe.acs.commons.forms.impl.FormImpl in project acs-aem-commons by Adobe-Consulting-Services.

the class AbstractFormHelperImpl method getPostForm.

/**
 * Gets the Form from POST requests.
 *
 * @param formName
 * @param request
 * @return
 */
protected final Form getPostForm(final String formName, final SlingHttpServletRequest request) {
    final Map<String, String> map = new HashMap<String, String>();
    final RequestParameterMap requestMap = request.getRequestParameterMap();
    for (final String key : requestMap.keySet()) {
        // POST LookupKey formName param does not matter
        if (StringUtils.equals(key, this.getPostLookupKey(null))) {
            continue;
        }
        final RequestParameter[] values = requestMap.getValues(key);
        if (values == null || values.length == 0) {
            log.debug("Value did not exist for key: {}", key);
        } else if (values.length == 1) {
            log.debug("Adding to form data: {} ~> {}", key, values[0].toString());
            map.put(key, values[0].getString());
        } else {
            // Requires support for transporting them and re-writing them back into HTML Form on error
            for (final RequestParameter value : values) {
                // Use the first non-blank value, or use the last value (which will be blank or not-blank)
                final String tmp = value.toString();
                map.put(key, tmp);
                if (StringUtils.isNotBlank(tmp)) {
                    break;
                }
            }
        }
    }
    return this.clean(new FormImpl(formName, request.getResource().getPath(), map));
}
Also used : RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) HashMap(java.util.HashMap) FormImpl(com.adobe.acs.commons.forms.impl.FormImpl) RequestParameter(org.apache.sling.api.request.RequestParameter)

Example 2 with FormImpl

use of com.adobe.acs.commons.forms.impl.FormImpl 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)

Aggregations

FormImpl (com.adobe.acs.commons.forms.impl.FormImpl)2 HashMap (java.util.HashMap)2 RequestParameter (org.apache.sling.api.request.RequestParameter)1 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)1 JSONException (org.apache.sling.commons.json.JSONException)1 JSONObject (org.apache.sling.commons.json.JSONObject)1