本文整理汇总了Scala中play.api.mvc.EssentialFilter类的典型用法代码示例。如果您正苦于以下问题:Scala EssentialFilter类的具体用法?Scala EssentialFilter怎么用?Scala EssentialFilter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EssentialFilter类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: AppLoader
//设置package包名称以及导入依赖的类
import models.{AuthorizeStoreCache, PendingContentStoreCache}
import play.api.ApplicationLoader.Context
import play.api.cache.EhCacheComponents
import play.api.mvc.EssentialFilter
import play.api.routing.Router
import play.api._
import play.filters.gzip.GzipFilter
import router.Routes
class AppLoader extends ApplicationLoader {
override def load(context: Context): Application = {
LoggerConfigurator(context.environment.classLoader)
.foreach(_.configure(context.environment))
new AppComponents(context).application
}
}
class AppComponents(context: Context)
extends BuiltInComponentsFromContext(context)
with EhCacheComponents {
implicit val executionContext =
play.api.libs.concurrent.Execution.Implicits.defaultContext
implicit lazy val authorizeStoreCache = AuthorizeStoreCache(
cacheApi("authorizeStore"))
implicit lazy val pendingConsentStoreCache = PendingContentStoreCache(
cacheApi("pendingConsentStore"))
implicit val config = configuration.underlying
lazy val applicationController = new controllers.Application
lazy val assets = new controllers.Assets(httpErrorHandler)
// Routes is a generated class
override def router: Router =
new Routes(httpErrorHandler, applicationController, assets)
val gzipFilter = new GzipFilter(shouldGzip = (request, response) => {
val contentType = response.header.headers.get("Content-Type")
contentType.exists(_.startsWith("text/html")) ||
request.path.endsWith("jsroutes.js")
})
override lazy val httpFilters: Seq[EssentialFilter] = Seq(gzipFilter)
}
示例2: AppShutdown
//设置package包名称以及导入依赖的类
package org.andrewconner.spot.modules
import com.sun.media.sound.ModelSource
import org.andrewconner.spot.AppComponents
import org.andrewconner.spot.playapp.TarpitFilter
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import play.api.libs.ws.WSClient
import play.api.libs.ws.ahc.AhcWSClient
import play.api.mvc.EssentialFilter
import com.softwaremill.macwire._
import scala.concurrent.{ ExecutionContext, Future }
import scala.util.{ Failure, Success }
trait PlayAppModule { self: AppComponents =>
lazy val appShutdown: AppShutdown = new AppShutdown(applicationLifecycle)
override lazy val httpFilters: Seq[EssentialFilter] = if (environment.mode == play.api.Mode.Test) {
Seq()
} else {
Seq(wire[TarpitFilter])
}
}
class AppShutdown(applicationLifecycle: ApplicationLifecycle)(implicit val ec: ExecutionContext) {
// On app shutdown, calls `f` on a separate thread, not blocking the rest of application shutdown.
// Useful when we really don't care about the result.
def onStopAsync(f: => Unit): Unit = {
applicationLifecycle.addStopHook { () =>
Future { f }(ec).onComplete {
case Success(s) =>
Logger.info("Success stopping")
case Failure(ex) =>
Logger.error("Error executing stop hook", ex)
}
Future.successful(())
}
}
def onStopBlock(f: => Unit) = {
applicationLifecycle.addStopHook { () =>
Future.successful(f)
}
}
def onStartAsync(f: => Unit) = {
println("")
}
}
示例3: GriffinApiLoader
//设置package包名称以及导入依赖的类
import com.softwaremill.macwire._
import controllers.Assets
import filters.LoggingFilter
import play.api.ApplicationLoader.Context
import play.api._
import play.api.i18n._
import play.api.libs.ws.ahc.AhcWSComponents
import play.api.mvc.EssentialFilter
import play.api.routing.Router
import play.filters.cors.{CORSConfig, CORSFilter}
import router.Routes
import services.ServicesModule
class GriffinApiLoader extends ApplicationLoader {
def load(context: Context): Application = new GriffinComponents(context).application
}
class GriffinComponents(context: Context)
extends BuiltInComponentsFromContext(context)
with ServicesModule
with I18nComponents
with AhcWSComponents
{
// set up logger
LoggerConfigurator(context.environment.classLoader).foreach {
_.configure(context.environment)
}
lazy val assets: Assets = wire[Assets]
lazy val router: Router = {
// add the prefix string in local scope for the Routes constructor
val prefix: String = "/"
wire[Routes]
}
lazy val corsFilter = CORSFilter(CORSConfig.fromConfiguration(configuration))
lazy val loggingFilter = new LoggingFilter
lazy val filters = wire[Filters]
override lazy val httpFilters: Seq[EssentialFilter] =
Seq(loggingFilter,corsFilter)
}
示例4: SilhouetteMacwireApplicationLoader
//设置package包名称以及导入依赖的类
package com.github.jacobono
import _root_.controllers.Assets
import _root_.controllers.WebJarAssets
import com.softwaremill.macwire._
import org.flywaydb.play.FlywayPlayComponents
import play.api._
import play.api.ApplicationLoader.Context
import play.api.cache.EhCacheComponents
import play.api.db.slick.SlickComponents
import play.api.i18n.I18nComponents
import play.api.libs.openid.OpenIDComponents
import play.api.libs.ws.ning.NingWSComponents
import play.api.mvc.EssentialFilter
import play.api.routing.Router
import play.filters.csrf.CSRFComponents
import play.filters.headers.SecurityHeadersComponents
import router.Routes
import com.github.jacobono.modules._
class SilhouetteMacwireApplicationLoader extends ApplicationLoader {
def load(context: Context): Application = {
Logger.configure(context.environment)
new SilhouetteMacwireComponents(context).application
}
}
class SilhouetteMacwireComponents(context: Context) extends BuiltInComponentsFromContext(context)
with ControllerModule with UtilModule with UserModule with SilhouetteModule
with DAOModule with DatabaseModule
with I18nComponents with NingWSComponents with CSRFComponents with SecurityHeadersComponents
with SlickComponents with EhCacheComponents with OpenIDComponents with FlywayPlayComponents {
flywayPlayInitializer
// for the optional Router param in error handler
// if this is trying to use the Router value from BuiltInComponentsFromContext
// it results in a circular dependency between the Router and the HttpErrorHandler
// the application obviously won't start if this happens
lazy val routerOption = None
override lazy val httpErrorHandler = errorHandler
override lazy val httpFilters: Seq[EssentialFilter] = filters.filters
lazy val webJarAssets: WebJarAssets = wire[WebJarAssets]
lazy val assets: Assets = wire[Assets]
lazy val router: Router = {
lazy val prefix = "/"
wire[Routes]
}
}
示例5: 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)
}
}
}
示例6: DapperApplicationLoader
//设置package包名称以及导入依赖的类
package me.dapperware
import play.api.mvc.EssentialFilter
import play.api.routing.Router
import play.api.{Application, ApplicationLoader, BuiltInComponentsFromContext}
import com.softwaremill.macwire._
import me.dapperware.config.{MongoConfig, Neo4jConfig}
import me.dapperware.controllers.DapperController
import me.dapperware.module.{MongoModule, Neo4jModule}
import me.dapperware.schema.DapperSchemaBuilder
import play.filters.cors.CORSComponents
import router.Routes
class DapperApplicationLoader extends ApplicationLoader {
override def load(context: ApplicationLoader.Context): Application =
new DapperComponents(context).application
}
class DapperComponents(context: ApplicationLoader.Context)
extends BuiltInComponentsFromContext(context)
with CORSComponents {
// Configs
lazy val mongoConfig = wire[MongoConfig]
lazy val neo4jConfig = wire[Neo4jConfig]
// DBs
lazy val mongoModule: MongoModule = wire[MongoModule]
lazy val neo4jModule: Neo4jModule = wire[Neo4jModule]
// GraphQL Schema
lazy val schema = wire[DapperSchemaBuilder]
// Controllers
lazy val dapperController = wire[DapperController]
// Routes
lazy val router: Router = {
val prefix = "/"
wire[Routes]
}
override def httpFilters: Seq[EssentialFilter] = Seq(corsFilter)
}
示例7: ServerModule
//设置package包名称以及导入依赖的类
package uk.co.telegraph.utils.server
import akka.actor.ActorSystem
import com.google.inject.{AbstractModule, Provides}
import com.google.inject.Key.get
import com.google.inject.multibindings.Multibinder.newSetBinder
import play.api.mvc.EssentialFilter
import play.filters.cors.CORSFilter
import uk.co.telegraph.utils.server.filters.{AuthFilter, EventIdFilter}
import java.util.{Set => JSet}
import scala.language.postfixOps
import scala.collection.convert.WrapAsScala._
import uk.co.telegraph.utils.client.GenericClient
import uk.co.telegraph.utils.client.monitor.Monitor
class ServerModule extends AbstractModule {
override def configure(): Unit = {
val filterBinder = newSetBinder(binder(), get(classOf[EssentialFilter]))
filterBinder.addBinding().to(classOf[CORSFilter])
filterBinder.addBinding().to(classOf[EventIdFilter])
filterBinder.addBinding().to(classOf[AuthFilter])
}
@Provides
def monitorProvider(clientsSet:JSet[GenericClient])(implicit system:ActorSystem): Monitor = {
Monitor(clientsSet.toSeq)
}
}
示例8: CORSFilter
//设置package包名称以及导入依赖的类
package utils.play
import javax.inject.Inject
import controllers.Default
import play.api.Logger
import play.api.http.HttpFilters
import play.api.mvc.{ EssentialFilter, Filter, RequestHeader, Result }
import play.filters.csrf.CSRFFilter
import play.filters.headers.SecurityHeadersFilter
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
case class CORSFilter() extends Filter {
lazy val allowedDomain = play.api.Play.current.configuration.getString("cors.allowed.domain")
def apply(f: (RequestHeader) => Future[Result])(request: RequestHeader): Future[Result] = {
Logger.trace("[cors] filtering request to add cors")
if (isPreFlight(request)) {
Logger.trace("[cors] request is preflight")
Logger.trace(s"[cors] default allowed domain is $allowedDomain")
Future.successful(Default.Ok.withHeaders(
"Access-Control-Allow-Origin" -> allowedDomain.orElse(request.headers.get("Origin")).getOrElse(""),
"Access-Control-Allow-Methods" -> request.headers.get("Access-Control-Request-Method").getOrElse("*"),
"Access-Control-Allow-Headers" -> request.headers.get("Access-Control-Request-Headers").getOrElse(""),
"Access-Control-Expose-Headers" -> "WWW-Authenticate, Server-Authorization, X-Auth-Token",
"Access-Control-Allow-Credentials" -> "true"))
} else {
Logger.trace("[cors] request is normal")
Logger.trace(s"[cors] default allowed domain is $allowedDomain")
f(request).map {
_.withHeaders(
"Access-Control-Allow-Origin" -> allowedDomain.orElse(request.headers.get("Origin")).getOrElse(""),
"Access-Control-Allow-Methods" -> request.headers.get("Access-Control-Request-Method").getOrElse("*"),
"Access-Control-Allow-Headers" -> request.headers.get("Access-Control-Request-Headers").getOrElse(""),
"Access-Control-Expose-Headers" -> "WWW-Authenticate, Server-Authorization, X-Auth-Token, Authorization, Proxy-Authorization, Content-Type",
"Access-Control-Allow-Credentials" -> "true")
}
}
}
def isPreFlight(r: RequestHeader) = (
r.method.toLowerCase.equals("options")
&&
r.headers.get("Access-Control-Request-Method").nonEmpty)
}
class Filters @Inject() (csrfFilter: CSRFFilter, securityHeadersFilter: SecurityHeadersFilter) extends HttpFilters {
override def filters: Seq[EssentialFilter] = Seq(csrfFilter, securityHeadersFilter, CORSFilter())
}