public
Created — forked from jlcheng/Tutorial3.scala

Finagle - Http request (with compression)

  • Download Gist
Tutorial3.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
package com.twitter.finagle.example.misc
 
import com.twitter.finagle.Service
import com.twitter.finagle.builder._
import com.twitter.finagle.http._
import com.twitter.util.Future
import java.net.InetSocketAddress
import org.jboss.netty.buffer.ChannelBuffers
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.util.CharsetUtil.UTF_8
import scala.collection.mutable.Buffer
import org.jboss.netty.util.CharsetUtil
import java.net.URL
 
object Tutorial3Client {
def main(args: Array[String]) {
val clientCodec = Http(_compressionLevel = 6, _decompressionEnabled = true)
val client = ClientBuilder()
.codec(clientCodec)
.name("EchoClient")
.hosts("localhost:8080")
.hostConnectionLimit(10)
.build()
 
val request = RequestBuilder()
.setHeader(HttpHeaders.Names.USER_AGENT, "MyBot")
.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE)
.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, "gzip,deflate,sdch")
.url(new URL("http://localhost:8080/?a=111111111111111111111"))
.buildGet()
 
client(request) onSuccess { result =>
println("success: " + result)
} onFailure { error =>
error.printStackTrace()
} ensure {
client.release()
}
 
}
}
 
object Tutorial3Server {
def main(args: Array[String]) {
 
class EchoService extends Service[HttpRequest, HttpResponse] {
def apply(request: HttpRequest) = {
val response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)
val params = new QueryStringDecoder(request.getUri()).getParameters()
val responseContent = params.toString()
println(request.getHeaders())
response.setContent(ChannelBuffers.copiedBuffer(responseContent, CharsetUtil.UTF_8))
Future.value(response)
}
}
 
val echoServer: Server = ServerBuilder()
.codec(Http(_compressionLevel = 6))
.bindTo(new InetSocketAddress(8080))
.name("EchoServer")
.build(new EchoService())
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.