Search in sources :

Example 1 with FormField

use of com.adobe.acs.commons.mcp.form.FormField in project acs-aem-commons by Adobe-Consulting-Services.

the class AnnotatedFieldDeserializer method parseInput.

@SuppressWarnings("squid:S3776")
private static void parseInput(Object target, ValueMap input, Field field) throws ReflectiveOperationException, ParseException {
    FormField inputAnnotation = field.getAnnotation(FormField.class);
    Object value;
    if (input.get(field.getName()) == null) {
        if (inputAnnotation != null && inputAnnotation.required()) {
            if (field.getType() == Boolean.class || field.getType() == Boolean.TYPE) {
                value = false;
            } else {
                throw new NullPointerException("Required field missing: " + field.getName());
            }
        } else {
            return;
        }
    } else {
        value = input.get(field.getName());
    }
    if (hasMultipleValues(field.getType())) {
        parseInputList(target, serializeToStringArray(value), field);
    } else {
        Object val = value;
        if (value.getClass().isArray()) {
            val = ((Object[]) value)[0];
        }
        if (val instanceof RequestParameter) {
            /**
             * Special case handling uploaded files; Method call ~ copied from parseInputValue(..) *
             */
            if (field.getType() == RequestParameter.class) {
                FieldUtils.writeField(field, target, val, true);
            } else {
                try {
                    FieldUtils.writeField(field, target, ((RequestParameter) val).getInputStream(), true);
                } catch (IOException ex) {
                    LOG.error("Unable to get InputStream for uploaded file [ {} ]", ((RequestParameter) val).getName(), ex);
                }
            }
        } else {
            parseInputValue(target, String.valueOf(val), field);
        }
    }
}
Also used : RequestParameter(org.apache.sling.api.request.RequestParameter) IOException(java.io.IOException) FormField(com.adobe.acs.commons.mcp.form.FormField)

Aggregations

FormField (com.adobe.acs.commons.mcp.form.FormField)1 IOException (java.io.IOException)1 RequestParameter (org.apache.sling.api.request.RequestParameter)1