<commit_before><commit_msg>CELDEV-977 - add celements action 'ajax' (#284)<commit_after>package com.celements.ajax;

import static com.google.common.base.Predicates.*;
import static com.google.common.collect.ImmutableList.*;
import static java.util.stream.Collectors.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.apache.velocity.VelocityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Splitter;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.web.Utils;
import com.xpn.xwiki.web.XWikiAction;

public class AjaxAction extends XWikiAction {

  private static final String CEL_AJAX_CONTEXT_PROPERTY = "celAjaxScript";
  private static final String CEL_AJAX_SCRIPT_DIR_PROPERTY = "celAjax";
  private static final String AJAX_SCRIPT_ACTION = "ajax";

  private static final Logger LOGGER = LoggerFactory.getLogger(AjaxAction.class);

  /**
   * The identifier of the view action.
   */
  public static final String VIEW_ACTION = "view";

  /**
   * {@inheritDoc}
   *
   * @see XWikiAction#action(com.xpn.xwiki.XWikiContext)
   */
  @Override
  public boolean action(XWikiContext context) throws XWikiException {
    boolean shouldRender = true;
    context.put("action", VIEW_ACTION);
    context.put("ajaxAction", true);
    getAjaxScript(context).ifPresent(celAjaxScript -> {
      LOGGER.error("ajax: found script path '{}'.", celAjaxScript);
      context.put(CEL_AJAX_CONTEXT_PROPERTY, celAjaxScript);
      VelocityContext vcontext = (VelocityContext) context.get("vcontext");
      vcontext.put(CEL_AJAX_CONTEXT_PROPERTY, celAjaxScript);
    });
    return shouldRender;
  }

  /**
   * {@inheritDoc}
   *
   * @see XWikiAction#render(com.xpn.xwiki.XWikiContext)
   */
  @Override
  public String render(XWikiContext context) throws XWikiException {
    String page = Utils.getPage(context.getRequest(), AJAX_SCRIPT_ACTION);
    final String celAjaxScript = getAjaxScript(context).orElse(page);
    LOGGER.error("Ajax: render page '{}', script: '{}'", page, celAjaxScript);
    Utils.parseTemplate(celAjaxScript, !page.equals("direct"), context);
    return null;
  }

  private Optional<String> getAjaxScript(XWikiContext context) {
    List<String> pathParts = parseRequestPath(context)
        .limit(3).collect(toImmutableList());
    if ((pathParts.size() > 2) && pathParts.get(0).equals(AJAX_SCRIPT_ACTION)) {
      String celAjaxScript = "/" + Stream.concat(
          Stream.of(CEL_AJAX_SCRIPT_DIR_PROPERTY),
          parseRequestPath(context).skip(1))
          .collect(joining("/"));
      LOGGER.debug("ajax: found script path '{}'", celAjaxScript);
      return Optional.of(celAjaxScript);
    }
    return Optional.empty();
  }

  private Stream<String> parseRequestPath(XWikiContext context) {
    return Splitter.on('/')
        .splitToStream(context.getRequest().getPathInfo())
        .filter(not(String::isEmpty));
  }

}
{'fraction_non_alphanumeric': 0.12566844919786097, 'fraction_numerical': 0.004010695187165776, 'mean_word_length': 4.316163410301954, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3011181', 'n_tokens_mistral': 990, 'n_tokens_neox': 970, 'n_words': 176}