Search in sources :

Example 16 with RequestProperty

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

the class ModifyOperation method writeContent.

/**
     * Writes back the content
     *
     * @throws PersistenceException if a persistence error occurs
     */
private void writeContent(final ResourceResolver resolver, final Map<String, RequestProperty> reqProperties, final List<Modification> changes, final VersioningConfiguration versioningConfiguration) throws PersistenceException {
    final SlingPropertyValueHandler propHandler = new SlingPropertyValueHandler(dateParser, this.jcrSsupport, changes);
    for (final RequestProperty prop : reqProperties.values()) {
        if (prop.hasValues()) {
            final Resource parent = deepGetOrCreateResource(resolver, prop.getParentPath(), reqProperties, changes, versioningConfiguration);
            this.jcrSsupport.checkoutIfNecessary(parent, changes, versioningConfiguration);
            // skip jcr special properties
            if (prop.getName().equals(JcrConstants.JCR_PRIMARYTYPE) || prop.getName().equals(JcrConstants.JCR_MIXINTYPES)) {
                continue;
            }
            if (prop.isFileUpload()) {
                uploadHandler.setFile(parent, prop, changes);
            } else {
                propHandler.setProperty(parent, prop);
            }
        }
    }
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) SlingPropertyValueHandler(org.apache.sling.servlets.post.impl.helper.SlingPropertyValueHandler) Resource(org.apache.sling.api.resource.Resource)

Example 17 with RequestProperty

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

the class RequestPropertyTest method testSingleValueWithDefaultToIgnore.

@Test
public void testSingleValueWithDefaultToIgnore() throws Throwable {
    Map<String, RequestProperty> props = collectContent(p("./param", ""), p("./param@DefaultValue", ":ignore"));
    assertEquals(1, props.size());
    RequestProperty prop = props.get("/test/path/param");
    assertTrue(prop.hasValues());
    assertFalse(prop.providesValue());
    assertEquals(0, prop.getStringValues().length);
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) Test(org.junit.Test)

Example 18 with RequestProperty

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

the class RequestPropertyTest method testMultiValueWithBlankIgnoringBlanks.

@Test
public void testMultiValueWithBlankIgnoringBlanks() throws Throwable {
    Map<String, RequestProperty> props = collectContent(p("./param", "true", ""));
    assertEquals(1, props.size());
    RequestProperty prop = props.get("/test/path/param");
    assertTrue(prop.hasValues());
    assertTrue(prop.providesValue());
    assertEquals(2, prop.getStringValues().length);
    assertEquals("true", prop.getStringValues()[0]);
    assertEquals("", prop.getStringValues()[1]);
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) Test(org.junit.Test)

Example 19 with RequestProperty

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

the class RequestPropertyTest method testSingleValueIgnoringBlanks.

@Test
public void testSingleValueIgnoringBlanks() throws Throwable {
    Map<String, RequestProperty> props = collectContent(p("./param", ""), p("./param@IgnoreBlanks", "true"));
    assertEquals(1, props.size());
    RequestProperty prop = props.get("/test/path/param");
    assertFalse(prop.hasValues());
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) Test(org.junit.Test)

Example 20 with RequestProperty

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

the class AbstractCreateOperation method collectContent.

/**
     * Collects the properties that form the content to be written back to the
     * resource tree.
     */
protected Map<String, RequestProperty> collectContent(final SlingHttpServletRequest request, final PostResponse response) {
    final boolean requireItemPrefix = requireItemPathPrefix(request);
    // walk the request parameters and collect the properties
    final LinkedHashMap<String, RequestProperty> reqProperties = new LinkedHashMap<>();
    for (final Map.Entry<String, RequestParameter[]> e : request.getRequestParameterMap().entrySet()) {
        final String paramName = e.getKey();
        if (ignoreParameter(paramName)) {
            continue;
        }
        // skip parameters that do not start with the save prefix
        if (requireItemPrefix && !hasItemPathPrefix(paramName)) {
            continue;
        }
        // ensure the paramName is an absolute property name
        final String propPath = toPropertyPath(paramName, response);
        // causes the setProperty using the 'long' property type
        if (propPath.endsWith(SlingPostConstants.TYPE_HINT_SUFFIX)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.TYPE_HINT_SUFFIX);
            final RequestParameter[] rp = e.getValue();
            if (rp.length > 0) {
                prop.setTypeHintValue(rp[0].getString());
            }
            continue;
        }
        // @DefaultValue
        if (propPath.endsWith(SlingPostConstants.DEFAULT_VALUE_SUFFIX)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.DEFAULT_VALUE_SUFFIX);
            prop.setDefaultValues(e.getValue());
            continue;
        }
        // fulltext form field.
        if (propPath.endsWith(SlingPostConstants.VALUE_FROM_SUFFIX)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.VALUE_FROM_SUFFIX);
            // @ValueFrom params must have exactly one value, else ignored
            if (e.getValue().length == 1) {
                final String refName = e.getValue()[0].getString();
                final RequestParameter[] refValues = request.getRequestParameters(refName);
                if (refValues != null) {
                    prop.setValues(refValues);
                }
            }
            continue;
        }
        // causes the JCR Text property to be deleted before update
        if (propPath.endsWith(SlingPostConstants.SUFFIX_DELETE)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_DELETE);
            prop.setDelete(true);
            continue;
        }
        // property to Text.
        if (propPath.endsWith(SlingPostConstants.SUFFIX_MOVE_FROM)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_MOVE_FROM);
            // @MoveFrom params must have exactly one value, else ignored
            if (e.getValue().length == 1) {
                final String sourcePath = e.getValue()[0].getString();
                prop.setRepositorySource(sourcePath, true);
            }
            continue;
        }
        // property to Text.
        if (propPath.endsWith(SlingPostConstants.SUFFIX_COPY_FROM)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_COPY_FROM);
            // @MoveFrom params must have exactly one value, else ignored
            if (e.getValue().length == 1) {
                final String sourcePath = e.getValue()[0].getString();
                prop.setRepositorySource(sourcePath, false);
            }
            continue;
        }
        // property to Text.
        if (propPath.endsWith(SlingPostConstants.SUFFIX_IGNORE_BLANKS)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_IGNORE_BLANKS);
            if (e.getValue().length == 1) {
                prop.setIgnoreBlanks(true);
            }
            continue;
        }
        if (propPath.endsWith(SlingPostConstants.SUFFIX_USE_DEFAULT_WHEN_MISSING)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_USE_DEFAULT_WHEN_MISSING);
            if (e.getValue().length == 1) {
                prop.setUseDefaultWhenMissing(true);
            }
            continue;
        }
        // <input name="tags"          value="-orange" type="hidden" />
        if (propPath.endsWith(SlingPostConstants.SUFFIX_PATCH)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_PATCH);
            prop.setPatch(true);
            continue;
        }
        if (propPath.endsWith(SlingPostConstants.SUFFIX_OFFSET)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_OFFSET);
            if (e.getValue().length == 1) {
                Chunk chunk = prop.getChunk();
                if (chunk == null) {
                    chunk = new Chunk();
                }
                chunk.setOffsetValue(Long.parseLong(e.getValue()[0].toString()));
                prop.setChunk(chunk);
            }
            continue;
        }
        if (propPath.endsWith(SlingPostConstants.SUFFIX_COMPLETED)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_COMPLETED);
            if (e.getValue().length == 1) {
                Chunk chunk = prop.getChunk();
                if (chunk == null) {
                    chunk = new Chunk();
                }
                chunk.setCompleted(Boolean.parseBoolean((e.getValue()[0].toString())));
                prop.setChunk(chunk);
            }
            continue;
        }
        if (propPath.endsWith(SlingPostConstants.SUFFIX_LENGTH)) {
            final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, SlingPostConstants.SUFFIX_LENGTH);
            if (e.getValue().length == 1) {
                Chunk chunk = prop.getChunk();
                if (chunk == null) {
                    chunk = new Chunk();
                }
                chunk.setLength(Long.parseLong(e.getValue()[0].toString()));
                prop.setChunk(chunk);
            }
            continue;
        }
        // plain property, create from values
        final RequestProperty prop = getOrCreateRequestProperty(reqProperties, propPath, null);
        prop.setValues(e.getValue());
    }
    return reqProperties;
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) RequestParameter(org.apache.sling.api.request.RequestParameter) Chunk(org.apache.sling.servlets.post.impl.helper.Chunk) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

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