<commit_before>package skinny.controller.feature

import java.lang.reflect.Modifier
import org.scalatra.ScalatraBase
import skinny.controller.Params
import skinny.exception.RequestScopeConflictException
import java.util.Locale
import org.joda.time._
import skinny.I18n

object RequestScopeFeature {

  /**
   * Key for request scope.
   */
  val REQUEST_SCOPE_KEY = "__SKINNY_FRAMEWORK_REQUEST_SCOPE__"

  // skinny has this request scope as a member
  val ATTR_SKINNY = "s"

  val ATTR_CONTEXT_PATH = "contextPath"
  val ATTR_REQUEST_PATH = "requestPath"
  val ATTR_REQUEST_PATH_WITH_QUERY_STRING = "requestPathWithQueryString"
  val ATTR_PARAMS = "params"
  val ATTR_MULTI_PARAMS = "multiParams"
  val ATTR_FLASH = "flash"
  val ATTR_ERROR_MESSAGES = "errorMessages"
  val ATTR_KEY_AND_ERROR_MESSAGES = "keyAndErrorMessages"
  val ATTR_I18N = "i18n"

  val ATTR_CSRF_KEY = "csrfKey"
  val ATTR_CSRF_TOKEN = "csrfToken"

  val ATTR_RESOURCE_NAME = "resourceName"
  val ATTR_RESOURCES_NAME = "resourcesName"
}

/**
 * Request scope support.
 */
trait RequestScopeFeature extends ScalatraBase with SessionLocaleFeature {

  import RequestScopeFeature._

  /**
   * Registers default attributes in the request scope.
   */
  before() {
    if (requestScope().isEmpty) {
      set(ATTR_SKINNY, skinny.Skinny(requestScope()))
      // requestPath/contextPath
      val requestPathWithContext = contextPath + requestPath
      val queryStringPart = Option(request.getQueryString).map(qs => "?" + qs).getOrElse("")
      set(ATTR_CONTEXT_PATH -> contextPath)
      set(ATTR_REQUEST_PATH -> requestPathWithContext)
      set(ATTR_REQUEST_PATH_WITH_QUERY_STRING -> s"${requestPathWithContext}${queryStringPart}")
      // for forms/validator
      set(ATTR_PARAMS -> skinny.controller.Params(params))
      set(ATTR_MULTI_PARAMS -> skinny.controller.MultiParams(multiParams))
      set(ATTR_ERROR_MESSAGES -> Seq())
      set(ATTR_KEY_AND_ERROR_MESSAGES -> Map[String, Seq[String]]())
      // i18n in view templates
      setI18n()
    }
  }

  /**
   * Returns whole request scope attributes.
   *
   * @return whole attributes
   */
  def requestScope(): scala.collection.mutable.Map[String, Any] = {
    request.getAttribute(REQUEST_SCOPE_KEY) match {
      case null =>
        val values = collection.mutable.Map[String, Any]()
        request.setAttribute(REQUEST_SCOPE_KEY, values)
        values
      case values: collection.mutable.Map[_, _] => values.asInstanceOf[collection.mutable.Map[String, Any]]
      case _ => throw new RequestScopeConflictException(
        s"Don't use '${REQUEST_SCOPE_KEY}' for request attribute key name.")
    }
  }

  /**
   * Set attribute to request scope.
   *<commit_msg>Changed requestScope container to collection.concurrent.TriMap<commit_after>package skinny.controller.feature

import java.lang.reflect.Modifier
import org.scalatra.ScalatraBase
import skinny.controller.Params
import skinny.exception.RequestScopeConflictException
import java.util.Locale
import org.joda.time._
import skinny.I18n

object RequestScopeFeature {

  /**
   * Key for request scope.
   */
  val REQUEST_SCOPE_KEY = "__SKINNY_FRAMEWORK_REQUEST_SCOPE__"

  // skinny has this request scope as a member
  val ATTR_SKINNY = "s"

  val ATTR_CONTEXT_PATH = "contextPath"
  val ATTR_REQUEST_PATH = "requestPath"
  val ATTR_REQUEST_PATH_WITH_QUERY_STRING = "requestPathWithQueryString"
  val ATTR_PARAMS = "params"
  val ATTR_MULTI_PARAMS = "multiParams"
  val ATTR_FLASH = "flash"
  val ATTR_ERROR_MESSAGES = "errorMessages"
  val ATTR_KEY_AND_ERROR_MESSAGES = "keyAndErrorMessages"
  val ATTR_I18N = "i18n"

  val ATTR_CSRF_KEY = "csrfKey"
  val ATTR_CSRF_TOKEN = "csrfToken"

  val ATTR_RESOURCE_NAME = "resourceName"
  val ATTR_RESOURCES_NAME = "resourcesName"
}

/**
 * Request scope support.
 */
trait RequestScopeFeature extends ScalatraBase with SessionLocaleFeature {

  import RequestScopeFeature._

  /**
   * Registers default attributes in the request scope.
   */
  before() {
    if (requestScope().isEmpty) {
      set(ATTR_SKINNY, skinny.Skinny(requestScope()))
      // requestPath/contextPath
      val requestPathWithContext = contextPath + requestPath
      val queryStringPart = Option(request.getQueryString).map(qs => "?" + qs).getOrElse("")
      set(ATTR_CONTEXT_PATH -> contextPath)
      set(ATTR_REQUEST_PATH -> requestPathWithContext)
      set(ATTR_REQUEST_PATH_WITH_QUERY_STRING -> s"${requestPathWithContext}${queryStringPart}")
      // for forms/validator
      set(ATTR_PARAMS -> skinny.controller.Params(params))
      set(ATTR_MULTI_PARAMS -> skinny.controller.MultiParams(multiParams))
      set(ATTR_ERROR_MESSAGES -> Seq())
      set(ATTR_KEY_AND_ERROR_MESSAGES -> Map[String, Seq[String]]())
      // i18n in view templates
      setI18n()
    }
  }

  /**
   * Returns whole request scope attributes.
   *
   * @return whole attributes
   */
  def requestScope(): scala.collection.concurrent.Map[String, Any] = {
    request.getAttribute(REQUEST_SCOPE_KEY) match {
      case null =>
        val values = scala.collection.concurrent.TrieMap[String, Any]()
        request.setAttribute(REQUEST_SCOPE_KEY, values)
        values
      case values: scala.collection.concurrent.Map[_, _] =>
        values.asInstanceOf[scala.collection.concurrent.Map[String, Any]]
      case _ => throw new RequestScopeConflictException(
        s"Don't use '${REQUEST_SCOPE_KEY}' for request attribute key name.")
    }
  }

  /**
   * Set attribute to request scope.
   *
{'fraction_non_alphanumeric': 0.09892902523143947, 'fraction_numerical': 0.0036304229442730078, 'mean_word_length': 3.8847517730496453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '535980', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1720, 'n_words': 352}