<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.sling.ujax.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.jcr.NamespaceException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.api.wrappers.SlingRequestPaths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Holds various states and encapsulates methods that are needed to handle a
 * ujax post request.
 */
public class UjaxPostProcessor {

    /**
     * default log
     */
    private static final Logger log = LoggerFactory.getLogger(UjaxPostProcessor.class);

    public static final String ORDER_FIRST = "first";
    public static final String ORDER_BEFORE = "before ";
    public static final String ORDER_AFTER = "after ";
    public static final String ORDER_LAST = "last";

    /**
     * handler that deals with properties
     */
    private final UjaxPropertyValueHandler propHandler;

    /**
     * handler that deals with file upload
     */
    private final UjaxFileUploadHandler uploadHandler;

    /**
     * utility class for generating node names
     */
    private final NodeNameGenerator nodeNameGenerator;

    // TODO
    private final String rootPath;

    /**
     * utility class for parsing date strings
     */
    private final DateParser dateParser;

    /**
     * the sling http servlet request
     */
    private final SlingHttpServletRequest request;

    /**
     * the jcr session to operate on
     */
    private final Session session;

    /**
     * prefix of which the names of request properties must start with
     * in order to be regardes as input values.
     */
    private String savePrefix;

    private HtmlResponse htmlResponse;

    /**
     * map of properties that form the content
     */
    private Map<String, RequestProperty> reqProperties = new LinkedHashMap<String, RequestProperty>();


    /**
     * Creates a new post processor
     * @param request the request to operate on
     * @param session jcr session to operate on
     * @param nodeNameGenerator the node name generator. use a servlet scoped one,
     *        so that it can hold states.
     * @param dateParser helper for parsing date strings
     * @param servletContext The ServletContext to use for file upload
     */
    public UjaxPostProcessor(SlingHttpServletRequest request, Session session,
                             NodeNameGenerator nodeNameGenerator,
                             DateParser dateParser,
                             ServletContext servletContext) {
        this.request = request;
        this.session = session;
        this.htmlResponse = new HtmlResponse();

        htmlResponse.setReferer(request.getHeader("referer"));

        // default to non-creating request (trailing DEFAULT_CREATE_SUFFIX)
        htmlResponse.setCreateRequest(false);

        // calculate the paths
        StringBuffer rootPathBuf = new StringBuffer();
        String suffix;
        Resource currentResource = request.getResource();
        if (Resource.RESOURCE_TYPE_NON_EXISTING.equals(currentResource.getResourceType())) {

            // no resource, treat the missing resource path as suffix
            suffix = currentResource.getPath();

        } else {

            // resource for part of the path, use request suffix
            suffix = request.getRequestPathInfo().getSuffix();

            // and preset the path buffer with the resource path
            rootPathBuf.append(currentResource.getPath());

        }

        // check for extensions or create suffix in the suffix
        if (suffix != null) {

            // cut off any selectors/extension from the suffix
            int dotPos = suffix.indexOf('.');<commit_msg>SLING-344 Treat non-existing and star resources equally and use the new JcrResoureUtil.is* methods to check for these resources.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.sling.ujax.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.jcr.NamespaceException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.api.wrappers.SlingRequestPaths;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Holds various states and encapsulates methods that are needed to handle a
 * ujax post request.
 */
public class UjaxPostProcessor {

    /**
     * default log
     */
    private static final Logger log = LoggerFactory.getLogger(UjaxPostProcessor.class);

    public static final String ORDER_FIRST = "first";
    public static final String ORDER_BEFORE = "before ";
    public static final String ORDER_AFTER = "after ";
    public static final String ORDER_LAST = "last";

    /**
     * handler that deals with properties
     */
    private final UjaxPropertyValueHandler propHandler;

    /**
     * handler that deals with file upload
     */
    private final UjaxFileUploadHandler uploadHandler;

    /**
     * utility class for generating node names
     */
    private final NodeNameGenerator nodeNameGenerator;

    // TODO
    private final String rootPath;

    /**
     * utility class for parsing date strings
     */
    private final DateParser dateParser;

    /**
     * the sling http servlet request
     */
    private final SlingHttpServletRequest request;

    /**
     * the jcr session to operate on
     */
    private final Session session;

    /**
     * prefix of which the names of request properties must start with
     * in order to be regardes as input values.
     */
    private String savePrefix;

    private HtmlResponse htmlResponse;

    /**
     * map of properties that form the content
     */
    private Map<String, RequestProperty> reqProperties = new LinkedHashMap<String, RequestProperty>();


    /**
     * Creates a new post processor
     * @param request the request to operate on
     * @param session jcr session to operate on
     * @param nodeNameGenerator the node name generator. use a servlet scoped one,
     *        so that it can hold states.
     * @param dateParser helper for parsing date strings
     * @param servletContext The ServletContext to use for file upload
     */
    public UjaxPostProcessor(SlingHttpServletRequest request, Session session,
                             NodeNameGenerator nodeNameGenerator,
                             DateParser dateParser,
                             ServletContext servletContext) {
        this.request = request;
        this.session = session;
        this.htmlResponse = new HtmlResponse();

        htmlResponse.setReferer(request.getHeader("referer"));

        // default to non-creating request (trailing DEFAULT_CREATE_SUFFIX)
        htmlResponse.setCreateRequest(false);

        // calculate the paths
        StringBuffer rootPathBuf = new StringBuffer();
        String suffix;
        Resource currentResource = request.getResource();
        if (JcrResourceUtil.isNonExistingResource(currentResource)
                || JcrResourceUtil.isStarResource(currentResource)) {

            // no resource, treat the missing resource path as suffix
            suffix = currentResource.getPath();

        } else {

            // resource for part of the path, use request suffix
            suffix = request.getRequestPathInfo().getSuffix();

            // and preset the path buffer with the resource path
            rootPathBuf.append(currentResource.getPath());

        }

        // check for extensions or create suffix in the suffix
        if (suffix != null) {

            // cut off any selectors/extension from the suffix
            int dotPos = suffix.indexOf('.');
{'fraction_non_alphanumeric': 0.07198306280875089, 'fraction_numerical': 0.001512249218671237, 'mean_word_length': 2.841982958946553, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '831780', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2432, 'n_words': 911}