scala - How to use a Filtered Service with RoutingService in Finagle -
fair warning, i'm new scala, missing simple here.
i have code, works great
import java.net.inetsocketaddress import com.twitter.finagle.builder.server import com.twitter.finagle.builder.serverbuilder import com.twitter.finagle.http.http import com.twitter.finagle.http.request import com.twitter.finagle.http.richhttp import java.util.logging.logger import com.typesafe.config.configfactory import org.jboss.netty.buffer.channelbuffers.copiedbuffer import org.jboss.netty.util.charsetutil.utf_8 import com.twitter.finagle.service import com.twitter.finagle.http.response import com.twitter.util.future import com.twitter.finagle.http.service.routingservice import org.jboss.netty.handler.codec.http.httpresponse import org.jboss.netty.handler.codec.http.httprequest import com.twitter.finagle.simplefilter object testserver extends app { val myservice = new service[request, response] { def apply(request: request) = { // if throw exception here, filter catch it. // throw new runtimeexception("s") future { val response = response() response.setcontent(copiedbuffer("hello world", utf_8)) response } } } val routingservice = routingservice.bypath { case "/" => myservice } val server: server = serverbuilder() .codec(richhttp[request](http())) .bindto(new inetsocketaddress(8080)) .name("httpserver") .build(routingservice) println("server up.") class authorize extends simplefilter[httprequest, httpresponse] { def apply(request: httprequest, continue: service[httprequest, httpresponse]) = { println("filtered") continue(request) } } }
but want add authorize filter service. if add this:
val authorize = new authorize val aservice: service[httprequest, httpresponse] = authorize andthen myservice
i compilation error:
[error] found : com.twitter.finagle.service[com.twitter.finagle.http.request,com.twitter.finagle.http.response] [error] required: com.twitter.finagle.service[org.jboss.netty.handler.codec.http.httprequest,org.jboss.netty.handler.codec.http.httpresponse]
if change service type service[httprequest, httpresponse], can't use routingservice.
[error] found : com.twitter.finagle.service[org.jboss.netty.handler.codec.http.httprequest,org.jboss.netty.handler.codec.http.httpresponse] [error] required: com.twitter.finagle.service[?,com.twitter.finagle.http.response]
due these type errors, having hard time getting routing service work filters.
thanks!
edit:
i wanted add example of code working using @septem's answer below, use filter:
class authorize extends filter[request, response, request, response] { def apply(request: request, continue: service[request, response]) = { println("filtered") continue(request) } }
i'm stilla little hazy on finagle , scala- guess happening here using simplefilter assumed service of type,
why use different request , response types in service , filter?
your service using request , response finagle:
val myservice = new service[request, response]
but filter using request , response netty:
class authorize extends simplefilter[httprequest, httpresponse]
i think reason compliation error, have choose 1 satisfies need, , stick it.
--edit--
then think should implement filter base on filter instead of simplefilter
abstract class filter[-reqin, +repout, +reqout, -repin] extends ((reqin, service[reqout, repin]) => future[repout])
the abstract class filter allow transform request/response types
check out doc details
Comments
Post a Comment