Search in sources :

Example 1 with NodeNameGenerator

use of org.apache.sling.servlets.post.NodeNameGenerator in project sling by apache.

the class SlingPostServlet method configure.

@Modified
private void configure(final Config configuration) {
    this.baseVersioningConfiguration = createBaseVersioningConfiguration(configuration);
    final DateParser dateParser = new DateParser();
    final String[] dateFormats = configuration.servlet_post_dateFormats();
    for (String dateFormat : dateFormats) {
        try {
            dateParser.register(dateFormat);
        } catch (Throwable t) {
            log.warn("configure: Ignoring DateParser format {} because it is invalid: {}", dateFormat, t);
        }
    }
    final String[] nameHints = configuration.servlet_post_nodeNameHints();
    final int nameMax = configuration.servlet_post_nodeNameMaxLength();
    final NodeNameGenerator nodeNameGenerator = new DefaultNodeNameGenerator(nameHints, nameMax);
    final String paramMatch = configuration.servlet_post_ignorePattern();
    final Pattern paramMatchPattern = Pattern.compile(paramMatch);
    this.modifyOperation.setDateParser(dateParser);
    this.modifyOperation.setDefaultNodeNameGenerator(nodeNameGenerator);
    this.modifyOperation.setIgnoredParameterNamePattern(paramMatchPattern);
    if (this.importOperation != null) {
        this.importOperation.setDefaultNodeNameGenerator(nodeNameGenerator);
        this.importOperation.setIgnoredParameterNamePattern(paramMatchPattern);
    }
}
Also used : DateParser(org.apache.sling.servlets.post.impl.helper.DateParser) DefaultNodeNameGenerator(org.apache.sling.servlets.post.impl.helper.DefaultNodeNameGenerator) Pattern(java.util.regex.Pattern) DefaultNodeNameGenerator(org.apache.sling.servlets.post.impl.helper.DefaultNodeNameGenerator) NodeNameGenerator(org.apache.sling.servlets.post.NodeNameGenerator) Modified(org.osgi.service.component.annotations.Modified)

Example 2 with NodeNameGenerator

use of org.apache.sling.servlets.post.NodeNameGenerator 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)

Aggregations

NodeNameGenerator (org.apache.sling.servlets.post.NodeNameGenerator)2 DefaultNodeNameGenerator (org.apache.sling.servlets.post.impl.helper.DefaultNodeNameGenerator)2 Pattern (java.util.regex.Pattern)1 RequestParameter (org.apache.sling.api.request.RequestParameter)1 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 DateParser (org.apache.sling.servlets.post.impl.helper.DateParser)1 Modified (org.osgi.service.component.annotations.Modified)1