score:4
After spending some time looking into this, the way the Filter system currently works it doesn't lend itself well to being applied to WebSocket requests.
The issue is that Filters work with an apply method taking a
nextFilter: RequestHeader => Future[Result]
where as WebSocket functions take a
f: RequestHeader => Future[Either[Result, Flow[In, Out, _]]]
So passing the WebSocket result through the Filter system doesn't really work.
However for my case, I was primarily interested in the logging filter. So as a workaround I created a custom version of acceptOrResult
as follows:
def acceptOrResult[TIn, TOut](f: RequestHeader => Future[Either[Result, Flow[TIn, TOut, _]]])(implicit transformer: MessageFlowTransformer[TIn, TOut], materializer: Materializer, ec: ExecutionContext): WebSocket = {
WebSocket { request =>
f(request).map { fResult =>
PostRequestLoggingFilter.apply(request,fResult.left.getOrElse(Ok))
fResult.right.map(transformer.transform)
}
}
}
I needed to break the logic out of the PostRequestLoggingFilter
into it's companion object's apply method so the above could function.
If the WebSocket
request is Left
, we have a Result
; if it's Right
then I just pass an Ok
result. This could be applied to other filters as long as you don't mind the fact that they'll run after the WebSocket request.
Source: stackoverflow.com
Related Query
- How do I add filters for WebSocket requests in Play Framework
- Liquibase - how add changelog for second database in play framework with play-slick
- How to add a prefix to all my routes in Play Framework 2?
- How to include file in production mode for Play framework
- How to not watch a file for changes in Play Framework
- How to parse json list or array in scala for play framework 2.2
- Play Framework : how to not watch a folder for changes in Play Framework
- How do I wait for an actor to stop during Play Framework shutdown?
- how can I remove codes creating session in unit test for Play framework and slick
- How to filter requests for access code in Play 2.1
- How to send json string body for FakeRequest without using withJsonBody in Play framework v2.x?
- Play Framework How to Compile Views for Distribution
- How to write tests for a Play Framework Filter?
- Using Play Framework Anorm, how do I get the auto-generated id back for an insert?
- An actor for websocket on play framework with multiple message type - scala
- How to use play framework FakeApplication() for multiple tests?
- How to add dependency to Play framework project in Eclipse from second project?
- How async Action api works in play framework 2.2.x for scala?
- Play 2.0 - Scala - How Add dependencies for any JDBC Drivers
- How to write unit test for Play Framework JSON readers
- How to add style to a radio button in play 2 for Scala?
- Scala Play Framework how to set language for current request?
- How to add dependency on library that is published locally for Play Project?
- play framework - how can i call this function for the authenticated user in this code play2 scala zentasks
- Play Framework 2.6.x how to add a specific cookie to every request if the cookie does not exists
- How to serve same static .html file for all dynamic urls using Play framework and Java?
- How to create multiple users in play framework 2.5.x with WebSocket
- How to specify custom database-connection parameters for testing purposes in Play Framework v2?
- How can I set script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' ; for specific page or path in play framework 2.6.x?
- How to expose a Restful Endpoint in Play Framework for uploading a File and Posting a JSON at the same time?
More Query from same tag
- Scala list find minimum difference of elements
- Why scala can't pass a function name directly to a val?
- Scala companion object override method error
- Why is this type not inferred by compiler?
- Scala shell. Declare same varibale name multiple times
- java.lang.NoClassDefFoundError: scala/collection/convert/DecorateAsScala but scala-library in pom.xml to use swagger with JBoss servlet
- How does the initialization of classes in Scala work?
- How does containTheSameElementsAs work in specs2
- Pattern matching custom implementation of List ADT
- Specifying Legal and/or Illegal Implicits at Compile-time?
- Is it possible to optimise a Free Monad program before execution?
- Scala: how can I sort an array of tuples by their second element?
- How can I use nscala_time inside spark-shell?
- Serialiaze a map[A,B] with lift-json knowing A<->String
- Tail recursion with multiple accumulator stages
- Immutable DataStructures In Scala
- Akka ZeroMQ - Any way to get the socket address?
- µPickle Couldn't derive type error on write
- Akka HTTP 2.0 to use SSL (HTTPS)
- Spark SQL: Sqlite - Unparseable date
- Using futures in Spark-Streaming & Cassandra (Scala)
- Handling NULL values in Spark StringIndexer
- Utils.Scala Databricks ~ Twitter Stream in Scala
- Building a Map[String,String]
- Scala import multiple packages
- How to call a method after a spark structured streaming query (Kafka)?
- Scala permutations using two lists
- Scala name mangling of private fields and JavaFX FXML injection
- Azure SDK in scala
- Composing an immutable list by 'foreach' over a List[JsObject]