<commit_before>package play.core.j

import play.api.mvc._
import play.api.libs.json._
import play.api.libs.Files.{ TemporaryFile }

import scala.xml._
import scala.collection.JavaConverters._

/**
 * provides Java centric BodyParsers
 */
object JavaParsers extends BodyParsers {

  import play.mvc.Http.{ RequestBody }

  case class DefaultRequestBody(
      urlFormEncoded: Option[Map[String, Seq[String]]] = None,
      raw: Option[RawBuffer] = None,
      text: Option[String] = None,
      json: Option[JsValue] = None,
      xml: Option[NodeSeq] = None,
      multipart: Option[MultipartFormData[TemporaryFile]] = None,
      override val isMaxSizeExceeded: Boolean = false) extends RequestBody {

    override lazy val asFormUrlEncoded = {
      urlFormEncoded.map(_.mapValues(_.toArray).asJava).orNull
    }

    override def asRaw = {
      raw.map { rawBuffer =>
        new play.mvc.Http.RawBuffer {
          def size = rawBuffer.size
          def asBytes(maxLength: Int) = rawBuffer.asBytes(maxLength).orNull
          def asBytes = rawBuffer.asBytes().orNull
          def asFile = rawBuffer.asFile
          override def toString = rawBuffer.toString
        }
      }.orNull
    }

    override def asText = {
      text.orNull
    }

    override lazy val asJson = {
      json.map { json =>
        play.libs.Json.parse(json.toString)
      }.orNull
    }

    override lazy val asXml = {
      xml.map { xml =>
        play.libs.XML.fromString(xml.toString)
      }.orNull
    }

    override lazy val asMultipartFormData = {
      multipart.map { multipart =>

        new play.mvc.Http.MultipartFormData {

          lazy val asFormUrlEncoded = {
            multipart.asFormUrlEncoded.mapValues(_.toArray).asJava
          }

          lazy val getFiles = {
            multipart.files.map { file =>
              new play.mvc.Http.MultipartFormData.FilePart(
                file.key, file.filename, file.contentType.orNull, file.ref.file)
            }.asJava
          }

        }

      }.orNull
    }

  }

  def anyContent(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.anyContent).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      anyContent =>
        DefaultRequestBody(
          anyContent.asFormUrlEncoded,
          anyContent.asRaw,
          anyContent.asText,
          anyContent.asJson,
          anyContent.asXml,
          anyContent.asMultipartFormData)
    )
  }

  def json(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.json(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      json =>
        DefaultRequestBody(json = Some(json))
    )
  }

  def tolerantJson(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.tolerantJson(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      json =>
        DefaultRequestBody(json = Some(json))
    )
  }

  def xml(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.xml(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      xml =>
        DefaultRequestBody(xml = Some(xml))
    )
  }

  def tolerantXml(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.tolerantXml(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      xml =>
        DefaultRequestBody(xml = Some(xml))
    )
  }

  def text(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.text(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      text =>
        DefaultRequestBody(text = Some(text))
    )
  }

  def tolerantText(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.tolerantText(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      text =>
        DefaultRequestBody(text = Some(text))
    )
  }

  def formUrlEncoded(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.urlFormEncoded(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      urlFormEncoded =>
        DefaultRequestBody(urlFormEncoded = Some(urlFormEncoded))
    )
  }

  def multipartFormData(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.multipartFormData).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      multipart =>
        DefaultRequestBody(multipart = Some(multipart))
    )
  }

  def raw(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.raw(Integer.MAX_VALUE)).map { body =><commit_msg>#1378 Java raw parser shouldn't buffer 2GB<commit_after>package play.core.j

import play.api.mvc._
import play.api.libs.json._
import play.api.libs.Files.{ TemporaryFile }

import scala.xml._
import scala.collection.JavaConverters._

/**
 * provides Java centric BodyParsers
 */
object JavaParsers extends BodyParsers {

  import play.mvc.Http.{ RequestBody }

  case class DefaultRequestBody(
      urlFormEncoded: Option[Map[String, Seq[String]]] = None,
      raw: Option[RawBuffer] = None,
      text: Option[String] = None,
      json: Option[JsValue] = None,
      xml: Option[NodeSeq] = None,
      multipart: Option[MultipartFormData[TemporaryFile]] = None,
      override val isMaxSizeExceeded: Boolean = false) extends RequestBody {

    override lazy val asFormUrlEncoded = {
      urlFormEncoded.map(_.mapValues(_.toArray).asJava).orNull
    }

    override def asRaw = {
      raw.map { rawBuffer =>
        new play.mvc.Http.RawBuffer {
          def size = rawBuffer.size
          def asBytes(maxLength: Int) = rawBuffer.asBytes(maxLength).orNull
          def asBytes = rawBuffer.asBytes().orNull
          def asFile = rawBuffer.asFile
          override def toString = rawBuffer.toString
        }
      }.orNull
    }

    override def asText = {
      text.orNull
    }

    override lazy val asJson = {
      json.map { json =>
        play.libs.Json.parse(json.toString)
      }.orNull
    }

    override lazy val asXml = {
      xml.map { xml =>
        play.libs.XML.fromString(xml.toString)
      }.orNull
    }

    override lazy val asMultipartFormData = {
      multipart.map { multipart =>

        new play.mvc.Http.MultipartFormData {

          lazy val asFormUrlEncoded = {
            multipart.asFormUrlEncoded.mapValues(_.toArray).asJava
          }

          lazy val getFiles = {
            multipart.files.map { file =>
              new play.mvc.Http.MultipartFormData.FilePart(
                file.key, file.filename, file.contentType.orNull, file.ref.file)
            }.asJava
          }

        }

      }.orNull
    }

  }

  def anyContent(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.anyContent).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      anyContent =>
        DefaultRequestBody(
          anyContent.asFormUrlEncoded,
          anyContent.asRaw,
          anyContent.asText,
          anyContent.asJson,
          anyContent.asXml,
          anyContent.asMultipartFormData)
    )
  }

  def json(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.json(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      json =>
        DefaultRequestBody(json = Some(json))
    )
  }

  def tolerantJson(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.tolerantJson(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      json =>
        DefaultRequestBody(json = Some(json))
    )
  }

  def xml(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.xml(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      xml =>
        DefaultRequestBody(xml = Some(xml))
    )
  }

  def tolerantXml(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.tolerantXml(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      xml =>
        DefaultRequestBody(xml = Some(xml))
    )
  }

  def text(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.text(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      text =>
        DefaultRequestBody(text = Some(text))
    )
  }

  def tolerantText(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.tolerantText(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      text =>
        DefaultRequestBody(text = Some(text))
    )
  }

  def formUrlEncoded(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.urlFormEncoded(Integer.MAX_VALUE)).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      urlFormEncoded =>
        DefaultRequestBody(urlFormEncoded = Some(urlFormEncoded))
    )
  }

  def multipartFormData(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.multipartFormData).map {
    _.fold(
      _ => DefaultRequestBody(isMaxSizeExceeded = true),
      multipart =>
        DefaultRequestBody(multipart = Some(multipart))
    )
  }

  def raw(maxLength: Int): BodyParser[RequestBody] = parse.maxLength(orDefault(maxLength), parse.raw).map { body =>
{'fraction_non_alphanumeric': 0.11271203760474147, 'fraction_numerical': 0.0005109339873288372, 'mean_word_length': 2.8899046104928456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, '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': '3108350', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2970, 'n_words': 502}