score:0

Accepted answer

Alright, here is a solution that seems to work:

object RedirectFilter extends Filter {

  def apply(next: RequestHeader => Result)(request: RequestHeader): Result = {

    getRedirectParams(request).flatMap { case (newRequest, body) =>
      route(newRequest).map { iteratee =>
        AsyncResult(Enumerator(body).run(iteratee))
      }
    } getOrElse {
      next(request)
    }
  }

  private def getRedirectParams(request: RequestHeader): Option[(RequestHeader, Array[Byte])] = {
    if (someCondition) {
      Some(toPostRequest(request), "Some new body data".getBytes("UTF-8"))      
    } else {
      None
    }
  }

  private def toPostRequest(originalRequest: RequestHeader): RequestHeader = {
    originalRequest.copy(
      uri = "/some/new/uri",
      path = "/some/new/uri",
      method = "POST"
    )
  }

  private def route(request: RequestHeader): Option[Iteratee[Array[Byte], Result]] = {
    Play.global.onRouteRequest(request).collect {
      case action: EssentialAction => Play.global.doFilter(action)(request)
    }
  }

}

Check out this gist for some other useful comments.

score:0

Just call the corresponding controller method.

Java version:

public static Result foo() {
    return bar();
}

public static Result bar() {
    return ok();
}

Am I missing something?

score:0

So you need to transform your Request from a GET request into a POST. I'm not aware of any automatic way of doing that, but it should be fairly easy.

Could you explain a bit more why you need that ?

Julien.


Related Query

More Query from same tag