在Finagle服务后应用过滤器

时间:2015-12-02 12:23:30

标签: scala finagle

我可以通过使用int yaya = 5; int x = 10; do { System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); System.out.println("Vote Ballot"); System.out.println("Below are the 2 Canditates you can choose to vote from"); System.out.println("Mar Roxas --- Code: 11"); System.out.println("Duterte ---- Code: 12"); System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); System.out.println("Who do you vote? Enter numbers only!"); int choice = input.nextInt(); System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); if (choice == 11) { System.out.println("You have voted Mar Roxas and not Duterte"); } else if ( choice == 12 ) { System.out.println("You have voted Duterte and not Mar Roxas"); } else { System.out.println("You have entered an invalid number"); } String confirm = "confirm"; String deny = "deny"; int conf = 1; int den = 2; System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); System.out.println("Do you want to let another voter vote? Or would you like to end the program at hand?"); int ans = input.nextInt(); System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); if ( ans==1 ) { System.out.println("The program would now repeat"); } else if ( ans==2 ) { if ( choice ==11 ) { int RoxasC = 0; int DuterteC = 0; RoxasC+=1; System.out.println("Mar roxas recieved " +RoxasC+ " number of vote/s and Duterte Recieved " +DuterteC+ " number of votes"); } else if ( choice ==12) { int RoxasC = 0; int DuterteC = 0; DuterteC+=1; System.out.println("Duterte recieved " +DuterteC+ " number of vote/s Roxas received " +RoxasC+ " number of votes"); } System.out.println("Program will end as per request"); break; } else { System.out.println("You entered an invalid keyword program would still repeat"); } System.out.println("\n"); } while( yaya==5 ); //Program Runs Infinitely 在HTTP服务之前添加过滤器来应用所有类型的传入HTTP请求预处理 即。

filter1 andThen httpService

问题是,我想向Response添加标头。有售后服务过滤器吗? 你不能真正val addRequestHeaderFilter = new SimpleFilter[Request, Response] { def apply(req: Request, service: Service[Request, Response]) = { req.headerMap.add("header1","header_value") service(req) } } Http.serve(":5000", addRequestHeaderFilter andThen theActualHTTPService)

1 个答案:

答案 0 :(得分:4)

我的错:我推测过滤器就像是溪流一样。所以这有效:

val addRequestHeaderFilter = new SimpleFilter[Request, Response] {
    def apply(req: Request, service: Service[Request, Response]) = {
      val respFuture = service(req)
      respFuture.map(_.headerMap.add("header1","header_value"))
      respFuture
    }      
 }
 Http.serve(":5000", addRequestHeaderFilter andThen theActualHTTPService) 

在fitler中,我正在应用该服务并在返回之前操纵其响应。这就像一个预服务过滤器,也就是说,不需要反转andThen功能组合。

Finagle非常酷< 3