Search in sources :

Example 26 with RequestProperty

use of org.apache.sling.servlets.post.impl.helper.RequestProperty in project sling by apache.

the class AbstractAuthorizablePostServlet method collectContent.

// ------ The methods below are based on the private methods from the
// ModifyOperation class -----
/**
     * Collects the properties that form the content to be written back to the
     * repository.
     * @param properties the properties out of which to generate the {@link RequestProperty}s
     * @return the list of {@link RequestProperty}s
     */
protected Collection<RequestProperty> collectContent(Map<String, ?> properties) {
    boolean requireItemPrefix = requireItemPathPrefix(properties);
    // walk the request parameters and collect the properties (the key is the property path).
    Map<String, RequestProperty> reqProperties = new HashMap<String, RequestProperty>();
    for (Map.Entry<String, ?> e : properties.entrySet()) {
        final String paramName = e.getKey();
        // do not store parameters with names starting with sling:post
        if (paramName.startsWith(SlingPostConstants.RP_PREFIX)) {
            continue;
        }
        // SLING-298: skip form encoding parameter
        if (paramName.equals("_charset_")) {
            continue;
        }
        // skip parameters that do not start with the save prefix
        if (requireItemPrefix && !hasItemPathPrefix(paramName)) {
            continue;
        }
        // ensure the paramName is an absolute property path (i.e. starts with "/", where root refers to the authorizable's root, https://issues.apache.org/jira/browse/SLING-1577)
        String propPath;
        if (paramName.startsWith("./")) {
            propPath = paramName.substring(1);
        } else {
            propPath = "/" + paramName;
        }
        if (propPath.indexOf("..") != -1) {
            // it is not supported to set properties potentially outside of the authorizable node
            LOG.warn("Property path containing '..' is not supported, skipping parameter {}", SlingPostConstants.SUFFIX_COPY_FROM, paramName);
            // skip it.
            continue;
        }
        // causes the setProperty using the 'long' property type
        if (propPath.endsWith(SlingPostConstants.TYPE_HINT_SUFFIX)) {
            RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.TYPE_HINT_SUFFIX);
            String typeHintValue = convertToString(e.getValue());
            if (typeHintValue != null) {
                prop.setTypeHintValue(typeHintValue);
            }
            continue;
        }
        // @DefaultValue
        if (propPath.endsWith(SlingPostConstants.DEFAULT_VALUE_SUFFIX)) {
            RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.DEFAULT_VALUE_SUFFIX);
            prop.setDefaultValues(convertToRequestParameterArray(e.getValue()));
            continue;
        }
        // fulltext form field.
        if (propPath.endsWith(SlingPostConstants.VALUE_FROM_SUFFIX)) {
            RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.VALUE_FROM_SUFFIX);
            // @ValueFrom params must have exactly one value, else ignored
            String[] valueFrom = convertToStringArray(e.getValue());
            if (valueFrom.length == 1) {
                String refName = valueFrom[0];
                RequestParameter[] refValues = convertToRequestParameterArray(refName);
                if (refValues != null) {
                    prop.setValues(refValues);
                }
            }
            continue;
        }
        // causes the JCR Text property to be deleted before update
        if (propPath.endsWith(SlingPostConstants.SUFFIX_DELETE)) {
            RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_DELETE);
            prop.setDelete(true);
            continue;
        }
        // property to Text.
        if (propPath.endsWith(SlingPostConstants.SUFFIX_MOVE_FROM)) {
            // don't support @MoveFrom here
            LOG.warn("Suffix {} not supported, skipping parameter {}", SlingPostConstants.SUFFIX_MOVE_FROM, paramName);
            continue;
        }
        // property to Text.
        if (propPath.endsWith(SlingPostConstants.SUFFIX_COPY_FROM)) {
            // don't support @CopyFrom here
            LOG.warn("Suffix {} not supported, skipping parameter {}", SlingPostConstants.SUFFIX_COPY_FROM, paramName);
            continue;
        }
        // plain property, create from values
        RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, null);
        prop.setValues(convertToRequestParameterArray(e.getValue()));
    }
    return reqProperties.values();
}
Also used : HashMap(java.util.HashMap) RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) RequestParameter(org.apache.sling.api.request.RequestParameter) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)26 Test (org.junit.Test)12 RepositoryException (javax.jcr.RepositoryException)5 Resource (org.apache.sling.api.resource.Resource)5 HashMap (java.util.HashMap)4 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)4 UserManager (org.apache.jackrabbit.api.security.user.UserManager)4 RequestParameter (org.apache.sling.api.request.RequestParameter)4 Map (java.util.Map)3 Group (org.apache.jackrabbit.api.security.user.Group)3 Session (javax.jcr.Session)2 User (org.apache.jackrabbit.api.security.user.User)2 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)2 LoginException (org.apache.sling.api.resource.LoginException)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 VersioningConfiguration (org.apache.sling.servlets.post.VersioningConfiguration)2 IOException (java.io.IOException)1