Shailesh
Shailesh

Reputation: 388

Creating filters in play 2.5.X

I am upgrading my service to use Play(Scala) 2.5.X. I am stuck at applying filters to all the responses.
Following are the entries that I added into application.conf

play.http.requestHandler = "utils.SimpleHttpRequestHandler"
play.http.filters="utils.CustomFilter"

I am authenticating each request which is done in "utils.SimpleHttpRequestHandler" and works as expected. However, I am not able to add headers to the response using Filters.
Following is my utils.CustomFilter:

@Singleton
class CustomFilter @Inject() (implicit val mat: Materializer) extends DefaultHttpFilters {
  def apply(nextFilter: RequestHeader => Future[Result])(requestHeader: RequestHeader): Future[Result] = {
    nextFilter(requestHeader).map { result =>
      result.withHeaders(
        PRAGMA -> "no-cache",
        CACHE_CONTROL -> "no-cache, no-store, must-revalidate, max-age=0",
        EXPIRES -> serverTime
      )
    }
  }
  private def serverTime = {
    val calendar = Calendar.getInstance()
    val dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z")
    dateFormat.setTimeZone(calendar.getTimeZone)
    dateFormat.format(calendar.getTime())
  }
}

Following is my utils.SimpleHttpRequestHandler:

@Singleton
class SimpleHttpRequestHandler @Inject() (router: Router) extends HttpRequestHandler {
  def handlerForRequest(request: RequestHeader) = {
    router.routes.lift(request) match {
      case Some(handler) =>
        val response = authenticate(request)
        response match {
          case Ok => (request, handler)
          case Forbidden => (request, controllers.Application.forbidden)
          case Unauthorized => (request, controllers.Application.unauthorized)
          case _ => (request, controllers.Application.badRequest)
        }
      case None => (request, Action(Results.NotFound))
    }
  }
  /* OTHER HELPER METHOD CODE*/
}

When I run play-app, I am not getting any warnings or errors, only problem is that filters are not working. What I am missing or what mistake I am doing?

Upvotes: 2

Views: 650

Answers (2)

wherby
wherby

Reputation: 784

See the source code from https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/http/HttpRequestHandler.scala#L180 and comment:

/** * Update the given handler so that when the handler is run any filters will also be run. The * default behavior is to wrap all [[play.api.mvc.EssentialAction]]s by calling filterAction, but to leave * other kinds of handlers unchanged. */

  protected def filterHandler(request: RequestHeader, handler: Handler): Handler = {
handler match {
  case action: EssentialAction if inContext(request.path) => filterAction(action)
  case handler => handler
}

}

The reason your filter is not applied is because you action is not EssentialAction.

Upvotes: 0

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

CustomFilter must extend Filter but not DefaultHttpFilters

Looks like you are not applying the filter and also your application.conf entry is wrong.

package foo

class Filters @Inject() (customFilter: CustomFilter) extends DefaultHttpFilters(customFilter)

Your application.conf entry should be play.http.filters=foo.Filters

Upvotes: 0

Related Questions