Search in sources :

Example 1 with RequestParameterMap

use of org.apache.sling.api.request.RequestParameterMap in project sling by apache.

the class AbstractCreateOperation method generateName.

protected String generateName(SlingHttpServletRequest request, String basePath) throws PersistenceException {
    // SLING-1091: If a :name parameter is supplied, the (first) value of this parameter is used unmodified as the name
    //    for the new node. If the name is illegally formed with respect to JCR name requirements, an exception will be
    //    thrown when trying to create the node. The assumption with the :name parameter is, that the caller knows what
    //    he (or she) is supplying and should get the exact result if possible.
    RequestParameterMap parameters = request.getRequestParameterMap();
    RequestParameter specialParam = parameters.getValue(SlingPostConstants.RP_NODE_NAME);
    if (specialParam != null) {
        if (specialParam.getString() != null && specialParam.getString().length() > 0) {
            // If the path ends with a *, create a node under its parent, with
            // a generated node name
            basePath = basePath += "/" + specialParam.getString();
            // if the resulting path already exists then report an error
            if (request.getResourceResolver().getResource(basePath) != null) {
                throw new PersistenceException("Collision in node names for path=" + basePath);
            }
            return basePath;
        }
    }
    // no :name value was supplied, so generate a name
    boolean requirePrefix = requireItemPathPrefix(request);
    String generatedName = null;
    if (extraNodeNameGenerators != null) {
        for (NodeNameGenerator generator : extraNodeNameGenerators) {
            generatedName = generator.getNodeName(request, basePath, requirePrefix, defaultNodeNameGenerator);
            if (generatedName != null) {
                break;
            }
        }
    }
    if (generatedName == null) {
        generatedName = defaultNodeNameGenerator.getNodeName(request, basePath, requirePrefix, defaultNodeNameGenerator);
    }
    // If the path ends with a *, create a node under its parent, with
    // a generated node name
    basePath += "/" + generatedName;
    basePath = ensureUniquePath(request, basePath);
    return basePath;
}
Also used : RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) RequestParameter(org.apache.sling.api.request.RequestParameter) PersistenceException(org.apache.sling.api.resource.PersistenceException) DefaultNodeNameGenerator(org.apache.sling.servlets.post.impl.helper.DefaultNodeNameGenerator) NodeNameGenerator(org.apache.sling.servlets.post.NodeNameGenerator)

Example 2 with RequestParameterMap

use of org.apache.sling.api.request.RequestParameterMap in project sling by apache.

the class RequestPropertyTest method collectContent.

@SuppressWarnings("unchecked")
private Map<String, RequestProperty> collectContent(Param... kvs) throws Throwable {
    final List<Map.Entry<String, RequestParameter>> params = new ArrayList<Map.Entry<String, RequestParameter>>();
    for (int i = 0; i < kvs.length; i++) {
        final Param kv = kvs[i];
        final RequestParameter[] param = new RequestParameter[kv.value.length];
        for (int j = 0; j < kv.value.length; j++) {
            final String strValue = kv.value[j];
            final RequestParameter aparam = context.mock(RequestParameter.class, "requestParameter" + i + "#" + j);
            context.checking(new Expectations() {

                {
                    allowing(aparam).getString();
                    will(returnValue(strValue));
                }
            });
            param[j] = aparam;
        }
        final Map.Entry<String, RequestParameter> entry = context.mock(Map.Entry.class, "entry" + i);
        context.checking(new Expectations() {

            {
                allowing(entry).getKey();
                will(returnValue(kv.key));
                allowing(entry).getValue();
                will(returnValue(param));
            }
        });
        params.add(entry);
    }
    final Set set = context.mock(Set.class);
    context.checking(new Expectations() {

        {
            one(set).iterator();
            will(returnValue(params.iterator()));
        }
    });
    final RequestParameterMap map = context.mock(RequestParameterMap.class);
    context.checking(new Expectations() {

        {
            one(map).entrySet();
            will(returnValue(set));
        }
    });
    final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class);
    context.checking(new Expectations() {

        {
            Vector names = new Vector();
            names.add("./param");
            one(request).getParameterNames();
            will(returnValue(names.elements()));
            one(request).getRequestParameterMap();
            will(returnValue(map));
        }
    });
    final HtmlResponse response = new HtmlResponse();
    response.setPath("/test/path");
    Map<String, RequestProperty> props = (Map<String, RequestProperty>) PrivateAccessor.invoke(new ModifyOperation(), "collectContent", COLLECT_CLASSES, new Object[] { request, response });
    return props;
}
Also used : Expectations(org.jmock.Expectations) HtmlResponse(org.apache.sling.servlets.post.HtmlResponse) Set(java.util.Set) RequestParameter(org.apache.sling.api.request.RequestParameter) ArrayList(java.util.ArrayList) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) ModifyOperation(org.apache.sling.servlets.post.impl.operations.ModifyOperation) RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) Map(java.util.Map) Vector(java.util.Vector)

Example 3 with RequestParameterMap

use of org.apache.sling.api.request.RequestParameterMap in project sling by apache.

the class DefaultNodeNameGenerator method getNodeName.

/**
     * Get a "nice" node name, if possible, based on given request
     *
     * @param request the request
     * @param basePath the base path
     * @param requirePrefix <code>true</code> if the parameter names for
     *      properties requires a prefix
     * @param defaultNodeNameGenerator a default generator
     * @return a nice node name
     */
public String getNodeName(SlingHttpServletRequest request, String basePath, boolean requirePrefix, NodeNameGenerator defaultNodeNameGenerator) {
    RequestParameterMap parameters = request.getRequestParameterMap();
    String valueToUse = null;
    boolean doFilter = true;
    // our parameterNames, in order, and has a value
    if (parameters != null) {
        // we first check for the special sling parameters
        RequestParameter specialParam = parameters.getValue(SlingPostConstants.RP_NODE_NAME);
        if (specialParam != null) {
            if (specialParam.getString() != null && specialParam.getString().length() > 0) {
                valueToUse = specialParam.getString();
                doFilter = false;
            }
        }
        if (valueToUse == null) {
            specialParam = parameters.getValue(SlingPostConstants.RP_NODE_NAME_HINT);
            if (specialParam != null) {
                if (specialParam.getString() != null && specialParam.getString().length() > 0) {
                    valueToUse = specialParam.getString();
                }
            }
        }
        if (valueToUse == null) {
            for (String param : parameterNames) {
                if (valueToUse != null) {
                    break;
                }
                if (requirePrefix) {
                    param = SlingPostConstants.ITEM_PREFIX_RELATIVE_CURRENT.concat(param);
                }
                final RequestParameter[] pp = parameters.get(param);
                if (pp != null) {
                    for (RequestParameter p : pp) {
                        valueToUse = p.getString();
                        if (valueToUse != null && valueToUse.length() > 0) {
                            break;
                        }
                        valueToUse = null;
                    }
                }
            }
        }
    }
    String result;
    // should we filter?
    if (valueToUse != null) {
        if (doFilter) {
            // filter value so that it works as a node name
            result = filter.filter(valueToUse);
        } else {
            result = valueToUse;
        }
    } else {
        // default value if none provided
        result = nextCounter() + "_" + System.currentTimeMillis();
    }
    if (doFilter) {
        // max length
        if (result.length() > maxLength) {
            result = result.substring(0, maxLength);
        }
    }
    return result;
}
Also used : RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) RequestParameter(org.apache.sling.api.request.RequestParameter)

Aggregations

RequestParameter (org.apache.sling.api.request.RequestParameter)3 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)3 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Set (java.util.Set)1 Vector (java.util.Vector)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 HtmlResponse (org.apache.sling.servlets.post.HtmlResponse)1 NodeNameGenerator (org.apache.sling.servlets.post.NodeNameGenerator)1 DefaultNodeNameGenerator (org.apache.sling.servlets.post.impl.helper.DefaultNodeNameGenerator)1 RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)1 ModifyOperation (org.apache.sling.servlets.post.impl.operations.ModifyOperation)1 Expectations (org.jmock.Expectations)1