Skip to content

Instantly share code, notes, and snippets.

@bracki
Last active September 6, 2017 10:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bracki/4551987 to your computer and use it in GitHub Desktop.
Save bracki/4551987 to your computer and use it in GitHub Desktop.
Thrift HTTP transport implemented as a Filter for Twitter's Finagle.
class ThriftOverHttp extends Filter[ThriftClientRequest, Array[Byte], HttpRequest, HttpResponse] {
def apply(request: ThriftClientRequest, service: Service[HttpRequest, HttpResponse]) = {
val httpRequest = convertThriftRequestToHttpRequest(request)
val response = service(httpRequest)
response flatMap { res =>
Future.value(res.getContent().array())
}
}
private def convertThriftRequestToHttpRequest(request: ThriftClientRequest) = {
val content = ChannelBuffers.copiedBuffer(request.message)
val httpRequest = new DefaultHttpRequest(
HttpVersion.HTTP_1_1,
HttpMethod.POST,
"/")
httpRequest.setContent(content)
httpRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes.toString)
httpRequest
}
}
@trevex
Copy link

trevex commented Feb 16, 2013

Hi, I am trying to use finagle along with your code to communicate with a flash client for a university project. Unfortunately I am struggling to get it to work could you include a usage example (I was trying to use it chained with and Then, but no success complaining that the arguments don't fit)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment