Skip to content

Instantly share code, notes, and snippets.

@wkimeria
Created December 29, 2014 19:19
Show Gist options
  • Save wkimeria/1df3b50219677687aa04 to your computer and use it in GitHub Desktop.
Save wkimeria/1df3b50219677687aa04 to your computer and use it in GitHub Desktop.
I was thinking the upstream services essentially become proxies that get configured with the endpoint url
To instantiate
val mtpService = new UpstreamServiceProxy(authService, URI.create("https://localhost:8081/mtp"))
Class definition of UpstreamServiceProxy
package com.lookout.borderpatrol
import java.net.URI
import com.lookout.borderpatrol.BorderPatrolApp.{NeedsAuthResponse, Response}
import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http.{Request => FinagleRequest, Response => FinagleResponse}
import com.twitter.util.{Await, Future}
import org.jboss.netty.handler.codec.http._
/**
* Created by wkimeria on 12/28/14.
*/
class UpstreamServiceProxy (authService: Service[HttpRequest, HttpResponse], uri: URI) extends Service[HttpRequest, FinagleResponse] {
val endPoint: URI = uri
def apply(request: HttpRequest) = {
println("------------------------------ UpstreamServiceProxy " + request.getUri + "----------------------------->")
val resp = Http.fetchUrl(endPoint.toURL) //TODO This needs to be the appropriate HTTP Verb
val response = new Response(Await.result(resp))
val modifiedResponse = response.status match {
case HttpResponseStatus.UNAUTHORIZED => {
println("returning a 401")
new NeedsAuthResponse(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED))
}
case _ => response
}
val r = Future.value(modifiedResponse)
println("<----------------------------- UpstreamService ------------------------------")
r
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment