本文整理汇总了Scala中play.api.libs.streams.Accumulator类的典型用法代码示例。如果您正苦于以下问题:Scala Accumulator类的具体用法?Scala Accumulator怎么用?Scala Accumulator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Accumulator类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: MessageDigestController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import play.api.libs.streams.Accumulator
import play.api.mvc.MultipartFormData.FilePart
import play.api.mvc.{Action, Controller, RequestHeader}
import play.core.parsers.Multipart.{FileInfo, FilePartHandler}
import services.MessageDigestService
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class MessageDigestController @Inject()(digestService: MessageDigestService)(implicit exec: ExecutionContext) extends Controller{
def index = Action.async {
Future.successful( Ok(views.html.MessageDigest.index(digestService.algorithms)) )
}
def upload = Action(Utility.multipartFormData(digest)){ implicit request =>
val digest = request.body.files.map(f => (f.filename, f.ref))
Ok(views.html.MessageDigest.digest(digest))
}
def digest(rh: RequestHeader): FilePartHandler[String] = {
case FileInfo(key, fileName, contentType) =>
val Some(Seq(algo, _*)) = rh.queryString.get("algorithm")
Accumulator(digestService.sink(algo)).map{ d =>
FilePart(key, fileName, contentType, d)
}
}
}
开发者ID:ejosiah,项目名称:crypto-utility,代码行数:35,代码来源:MessageDigestController.scala
示例2: KamonFilter
//设置package包名称以及导入依赖的类
package kamon.play
import akka.util.ByteString
import kamon.trace.Tracer
import kamon.util.SameThreadExecutionContext
import play.api.libs.streams.Accumulator
import play.api.mvc.{ EssentialAction, EssentialFilter, RequestHeader, Result }
class KamonFilter extends EssentialFilter {
override def apply(next: EssentialAction): EssentialAction = new EssentialAction {
override def apply(requestHeader: RequestHeader): Accumulator[ByteString, Result] = {
def onResult(result: Result): Result = {
Tracer.currentContext.collect { ctx ?
ctx.finish()
PlayExtension.httpServerMetrics.recordResponse(ctx.name, result.header.status.toString)
if (PlayExtension.includeTraceToken) result.withHeaders(PlayExtension.traceTokenHeaderName -> ctx.token)
else result
} getOrElse result
}
//override the current trace name
Tracer.currentContext.rename(PlayExtension.generateTraceName(requestHeader))
val nextAccumulator = next.apply(requestHeader)
nextAccumulator.map(onResult)(SameThreadExecutionContext)
}
}
}
开发者ID:agiledigital,项目名称:kamon-play-2.5.x,代码行数:32,代码来源:KamonFilter.scala
示例3: LogFilter
//设置package包名称以及导入依赖的类
package filters
import javax.inject.Inject
import akka.util.ByteString
import play.api.Logger
import play.api.libs.streams.Accumulator
import play.api.mvc._
import scala.concurrent.ExecutionContext
class LogFilter @Inject()(implicit ec: ExecutionContext) extends EssentialFilter {
def apply(nextFilter: EssentialAction) = new EssentialAction {
def apply(requestHeader: RequestHeader) = {
val startTime = System.currentTimeMillis
val accumulator: Accumulator[ByteString, Result] = nextFilter(requestHeader)
accumulator.map { result =>
val endTime = System.currentTimeMillis
val requestTime = endTime - startTime
val requestUri = requestHeader.uri
val filteredUri = Seq("/javascripts/", "/images/")
if (filteredUri.exists(uri => uri.containsSlice(requestUri))) {
Logger.debug(
s"${requestHeader.method} $requestUri took ${requestTime}ms and returned ${result.header.status}")
}
result
}
}
}
}
开发者ID:tweissbeck,项目名称:guild-forum,代码行数:36,代码来源:LogFilter.scala
示例4: Filters
//设置package包名称以及导入依赖的类
import akka.stream.Materializer
import akka.stream.scaladsl.Flow
import akka.util.ByteString
import javax.inject._
import play.api._
import play.api.http.HttpFilters
import play.api.libs.streams.Accumulator
import play.api.mvc._
import scala.concurrent.ExecutionContext
import scala.concurrent.Promise
@Singleton
class Filters @Inject() (
testFilter: TestFilter
) extends HttpFilters {
override val filters = Seq(testFilter)
}
class TestFilter @Inject() (
implicit val mat: Materializer,
implicit val ec: ExecutionContext
) extends EssentialFilter with BodyParsers {
override def apply(nextFilter: EssentialAction) = new EssentialAction {
lazy val log = play.api.Logger
override def apply(requestHeader: RequestHeader): Accumulator[ByteString, Result] = {
log.info(">>>> 0. applying TestFilter essential action")
val flowEntered = Promise[Boolean]()
val flow = Flow[ByteString].map { bs =>
log.info(">>>> 1. passing through flow")
flowEntered.success(true)
bs
}
val accumulator = nextFilter(requestHeader)
accumulator.through(flow).map { result =>
val msg = if(flowEntered.isCompleted == false) {
s"!!!! flow has NOT been feeded !!!!"
} else {
"flow has been feeded :)"
}
log.info(s"${requestHeader.method} ${requestHeader.path} => $msg (accumulator=$accumulator)")
result
}
}
}
}
开发者ID:le-yams,项目名称:play2-filters-accumulator-issue,代码行数:57,代码来源:Filters.scala
注:本文中的play.api.libs.streams.Accumulator类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论