本文整理汇总了Scala中play.api.mvc.Codec类的典型用法代码示例。如果您正苦于以下问题:Scala Codec类的具体用法?Scala Codec怎么用?Scala Codec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Codec类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: scalaTagsContentType
//设置package包名称以及导入依赖的类
import play.api.http.{ContentTypeOf, ContentTypes, Writeable}
import play.api.mvc.Codec
package object controllers {
// Allows us to automatically render a Result of type [[scalatags.Text.Frag]]
implicit def scalaTagsContentType(
implicit codec: Codec): ContentTypeOf[scalatags.Text.Frag] = {
ContentTypeOf[scalatags.Text.Frag](Some(ContentTypes.HTML))
}
implicit def scalaTagsWritable(
implicit codec: Codec): Writeable[scalatags.Text.Frag] = {
Writeable(frag => codec.encode(frag.render))
}
}
示例2: MultipartFormDataWritable
//设置package包名称以及导入依赖的类
package helpers
import java.nio.file.{Files, Paths}
import play.api.http.{HeaderNames, Writeable}
import play.api.libs.Files.TemporaryFile
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.mvc.MultipartFormData.FilePart
import play.api.mvc.{AnyContentAsMultipartFormData, Codec, MultipartFormData}
//Check -> http://tech.fongmun.com/post/125479939452/test-multipartformdata-in-play
object MultipartFormDataWritable {
val boundary = "--------ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
def formatDataParts(data: Map[String, Seq[String]]) = {
val dataParts = data.flatMap {
case (key, values) =>
values.map { value =>
val name = s""""$key""""
s"--$boundary\r\n${HeaderNames.CONTENT_DISPOSITION}: form-data; name=$name\r\n\r\n$value\r\n"
}
}.mkString("")
Codec.utf_8.encode(dataParts)
}
def filePartHeader(file: FilePart[TemporaryFile]) = {
val name = s""""${file.key}""""
val filename = s""""${file.filename}""""
val contentType = file.contentType.map { ct =>
s"${HeaderNames.CONTENT_TYPE}: $ct\r\n"
}.getOrElse("")
Codec.utf_8.encode(s"--$boundary\r\n${HeaderNames.CONTENT_DISPOSITION}: form-data; name=$name; filename=$filename\r\n$contentType\r\n")
}
val singleton = Writeable[MultipartFormData[TemporaryFile]](
transform = { form: MultipartFormData[TemporaryFile] =>
formatDataParts(form.dataParts) ++
form.files.flatMap { file =>
val fileBytes = Files.readAllBytes(Paths.get(file.ref.file.getAbsolutePath))
filePartHeader(file) ++ fileBytes ++ Codec.utf_8.encode("\r\n")
} ++
Codec.utf_8.encode(s"--$boundary--")
},
contentType = Some(s"multipart/form-data; boundary=$boundary")
)
implicit val anyContentAsMultipartFormWritable: Writeable[AnyContentAsMultipartFormData] = {
MultipartFormDataWritable.singleton.map(_.mdf)
}
}
示例3: MultipartFormDataWritable
//设置package包名称以及导入依赖的类
package de.zalando.play.controllers
import java.nio.file.{ Files, Paths }
import akka.util.ByteString
import play.api.http.{ HeaderNames, Writeable }
import play.api.libs.Files.TemporaryFile
import play.api.mvc.MultipartFormData.FilePart
import play.api.mvc.{ Codec, MultipartFormData }
object MultipartFormDataWritable {
val boundary = "--------ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
def formatDataParts(data: Map[String, Seq[String]]): ByteString = {
val dataParts = data.flatMap {
case (key, values) =>
values.map { value =>
val name = s""""$key""""
s"--$boundary\r\n${HeaderNames.CONTENT_DISPOSITION}: form-data; name=$name\r\n\r\n$value\r\n"
}
}.mkString("")
val bytes: ByteString = Codec.utf_8.encode(dataParts)
bytes
}
def filePartHeader(file: FilePart[TemporaryFile]): ByteString = {
val name = s""""${file.key}""""
val filename = s""""${file.filename}""""
val contentType = file.contentType.map { ct =>
s"${HeaderNames.CONTENT_TYPE}: $ct\r\n"
}.getOrElse("")
Codec.utf_8.encode(s"--$boundary\r\n${HeaderNames.CONTENT_DISPOSITION}: form-data; name=$name; filename=$filename\r\n$contentType\r\n")
}
val singleton = Writeable[MultipartFormData[TemporaryFile]](
transform = { form: MultipartFormData[TemporaryFile] =>
formatDataParts(form.dataParts) ++
form.files.flatMap { file =>
val fileBytes = Files.readAllBytes(Paths.get(file.ref.file.getAbsolutePath))
filePartHeader(file) ++ fileBytes ++ Codec.utf_8.encode("\r\n")
} ++
Codec.utf_8.encode(s"--$boundary--")
},
contentType = Some(s"multipart/form-data; boundary=$boundary")
)
}
示例4: Csv
//设置package包名称以及导入依赖的类
package views
import play.api.http.ContentTypeOf
import play.api.mvc.Codec
import play.twirl.api._
import scala.collection.immutable
import scala.collection.immutable.Seq
class Csv(buffer: immutable.Seq[Csv],text:String,escape:Boolean) extends BufferedContent[Csv](buffer, text) {
val contentType = Csv.contentType
def this(text: String) = this(Nil, Formats.safe(text),false)
def this(buffer: immutable.Seq[Csv]) = this(buffer, "",false)
override protected def buildString(builder: StringBuilder) {
if (elements.nonEmpty) {
elements.foreach { e =>
e.buildString(builder)
}
} else if (escape) {
// Using our own algorithm here because commons lang escaping wasn't designed for protecting against XSS, and there
// don't seem to be any other good generic escaping tools out there.
text.foreach {
case '"' => builder.append("\"\"")
case c => builder += c
}
} else {
builder.append(text)
}
}
}
object Csv {
val contentType = "text/csv"
implicit def contentTypeCsv(implicit codec: Codec): ContentTypeOf[Csv] = ContentTypeOf[Csv](Some(Csv.contentType))
def apply(text: String): Csv = new Csv(text)
def empty: Csv = new Csv("")
}
object CsvFormat extends Format[Csv] {
def raw(text: String): Csv = Csv(text)
def escape(text: String): Csv = {
new Csv(Nil, text, true)
}
def empty: Csv = new Csv("")
def fill(elements: Seq[Csv]): Csv = new Csv(elements)
}
示例5: wUnit
//设置package包名称以及导入依赖的类
package controllers
import lila.api._
import play.api.http._
import play.api.mvc.Codec
trait ResponseWriter {
implicit def wUnit(implicit codec: Codec): Writeable[Unit] =
Writeable[Unit]((_: Unit) => codec encode "ok")
implicit def ctoUnit: ContentTypeOf[Unit] =
ContentTypeOf[Unit](Some(ContentTypes.TEXT))
implicit def wFloat(implicit codec: Codec): Writeable[Float] =
Writeable[Float]((f: Float) => codec encode f.toString)
implicit def ctoFloat: ContentTypeOf[Float] =
ContentTypeOf[Float](Some(ContentTypes.TEXT))
implicit def wLong(implicit codec: Codec): Writeable[Long] =
Writeable[Long]((a: Long) => codec encode a.toString)
implicit def ctoLong: ContentTypeOf[Long] =
ContentTypeOf[Long](Some(ContentTypes.TEXT))
implicit def wInt(implicit codec: Codec): Writeable[Int] =
Writeable[Int]((i: Int) => codec encode i.toString)
implicit def ctoInt: ContentTypeOf[Int] =
ContentTypeOf[Int](Some(ContentTypes.TEXT))
implicit def wOptionString(implicit codec: Codec): Writeable[Option[String]] =
Writeable[Option[String]]((i: Option[String]) => codec encode ~i)
implicit def ctoOptionString: ContentTypeOf[Option[String]] =
ContentTypeOf[Option[String]](Some(ContentTypes.TEXT))
}