简体   繁体   中英

Play framework filter that modifies json request and response

I'll appreciate if someone can throw pointers on how to modify the following play framework logging filter (ref. play filters ) to achieve the following:

  • Print and modify the incoming json request body and http headers (eg, for POST, PUT, & PATCH)
  • Print and modify the outgoing json response body and http headers
  • A modification example can be injecting/replacing some token strings in the request and response body, eg,
  • REQUEST Json: {'a': 'REPLACE_ME', 'b': 'REPLACE_ME_TOO', 'c':'something'}
  • RESPONSE Json: {'A': 'REPLACE_ME', 'Bb': 'REPLACE_ME_TOO', 'C':'anything'}

import play.api.Logger
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits.defaultContext

object LoggingFilter extends EssentialFilter {
  def apply(nextFilter: EssentialAction) = new EssentialAction {
    def apply(requestHeader: RequestHeader) = {
      val startTime = System.currentTimeMillis
      nextFilter(requestHeader).map { result =>
        val endTime = System.currentTimeMillis
        val requestTime = endTime - startTime
        Logger.info(s"${requestHeader.method} ${requestHeader.uri}" +
          s" took ${requestTime}ms and returned ${result.header.status}")
        result.withHeaders("Request-Time" -> requestTime.toString)
      }
    }
  }
}

So far I have tried the following solution which is clearly ugly and brutal as it contains blocking calls and cryptic operators. I am still not sure how to re-inject the modified request body. (The presented solution incorporates code from 2 and 3 .)

import play.api.libs.iteratee._
import play.api.mvc._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration

class ReqResFilter extends EssentialFilter {

  def apply(next: EssentialAction) = new EssentialAction {
    def apply(requestHeader: RequestHeader): Iteratee[Array[Byte], Result] = {
      modifyRequest(next, requestHeader).map { result => modifyResponse(result)}
    }
  }

  def bytesToString: Enumeratee[Array[Byte], String] = Enumeratee.map[Array[Byte]] { bytes => new String(bytes)}

  def modifyRequest(nextA: EssentialAction, request: RequestHeader): Iteratee[Array[Byte], Result] = {

    def step(body: Array[Byte], nextI: Iteratee[Array[Byte], Result])(i: Input[Array[Byte]]):
    Iteratee[Array[Byte], Result] = i match {
      case Input.EOF =>
        val requestBody = new String(body, "utf-8")
        val modRequestBody = requestBody.replaceAll("REPLACE_ME", "1224")
        println(s"modifyRequest:: Here is the request body ${modRequestBody}")
        Iteratee.flatten(nextI.feed(Input.EOF))
      case Input.Empty =>
        Cont[Array[Byte], Result](step(body, nextI) _)
      case Input.El(e) =>
        val curBody = Array.concat(body, e)
        Cont[Array[Byte], Result](step(curBody, Iteratee.flatten(nextI.feed(Input.El(e)))) _)
    }

    val nextIteratee: Iteratee[Array[Byte], Result] = nextA(request)

    Cont[Array[Byte], Result](i => step(Array(), nextIteratee)(i))
  }

  def modifyResponse(result: Result): Result = {
    val responseBodyFuture: Future[String] = result.body |>>> bytesToString &>> Iteratee.consume[String]()
    val responseBody = Await.result(responseBodyFuture, Duration.Inf)
    val modResponseBody = responseBody.replaceAll("REPLACE_ME", "1224")
    println(s"modifyResponse:: Here is the response body ${modResponseBody}")
    new Result(result.header, Enumerator(modResponseBody.getBytes)).withHeaders("New-Header" -> "1234")
  }
}

Well since there are no solutions posted here let me add one solution. To make it work, I rewrote step() in modifyRequest() as follows:

def step(body: Array[Byte], nextI: Iteratee[Array[Byte], Result])(i: Input[Array[Byte]]):
Iteratee[Array[Byte], Result] = i match {
  case Input.EOF =>
    val requestBody = new String(body, "utf-8")
    val modRequestBody = requestBody.replaceAll("REPLACE_ME", "1224")
    println(s"modifyRequest:: Here is the request body ${modRequestBody}")
    Iteratee.flatten(nextI.feed(Input.El(modRequestBody.getBytes)))
  case Input.Empty =>
    Cont[Array[Byte], Result](step(body, nextI) _)
  case Input.El(e) =>
    val curBody = Array.concat(body, e)
    Cont[Array[Byte], Result](step(curBody, nextI) _)
}

The change is still blocking in nature as it buffers the incoming request. If someone has better solution please do post. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM