Created
November 5, 2014 23:36
-
-
Save thesamet/20e1711b406ffab6495d to your computer and use it in GitHub Desktop.
Reverse-proxy in Play!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reverseProxy = Action.async(parse.raw) { | |
request: Request[RawBuffer] => | |
// Create the request to the upstream server: | |
val proxyRequest = | |
WS.url("http://localhost:8887" + request.path) | |
.withFollowRedirects(false) | |
.withMethod(request.method) | |
.withVirtualHost("localhost:9000") | |
.withHeaders(flattenMultiMap(request.headers.toMap): _*) | |
.withQueryString(request.queryString.mapValues(_.head).toSeq: _*) | |
.withBody(request.body.asBytes().get) | |
// Stream the response to the client: | |
proxyRequest.stream.map { | |
case (headers: WSResponseHeaders, enum) => Result( | |
ResponseHeader(headers.status, headers.headers.mapValues(_.head)), | |
enum) | |
} | |
} |
Something like that? Also see the Play Framework Documentation.
proxyRequest.stream().map {
case (response, body) =>
// Check that the response was successful.
if (response.status == 200) {
// Get the content type.
val contentType = response.headers.get("Content-Type").flatMap(_.headOption)
.getOrElse("application/octet-stream")
val headers = response.headers.filterKeys(_ != "Transfer-Encoding")
// If there's a content length, send that, otherwise return the body chunked.
response.headers.get("Content-Length") match {
case Some(Seq(length)) =>
Ok.feed(body)
.withHeaders(headers.mapValues(_.mkString(",")).toSeq: _*)
.withHeaders("Content-Length" -> length)
.as(contentType)
case _ =>
Ok.chunked(body)
.withHeaders(headers.mapValues(_.mkString(",")).toSeq: _*)
.as(contentType)
}
} else {
BadGateway
}
}
How to do that in Java? I get problem with the body part:
import akka.stream.javadsl.Source;
.setBody(Source.single(request.body().asBytes()))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code runs well unless the backend content-encoding is chuncked. Do you know how do it?