spray.can.Http Scala Examples

The following examples show how to use spray.can.Http. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: WorkflowDownloadClient.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.Try

import akka.actor.ActorSystem
import akka.io.IO
import akka.pattern.ask
import spray.can.Http
import spray.client.pipelining._
import spray.http._
import spray.util._

import ai.deepsense.commons.utils.Logging
import ai.deepsense.sparkutils
import ai.deepsense.workflowexecutor.exception.UnexpectedHttpResponseException

class WorkflowDownloadClient(
    val address: String,
    val path: String,
    val timeout: Int)
  extends Logging {

  val downloadUrl = (workflowId: String) =>
    s"$address/$path/$workflowId/download"

  def downloadWorkflow(workflowId: String): Future[String] = {

    logger.info(s"Downloading workflow $workflowId...")

    implicit val system = ActorSystem()
    import system.dispatcher
    implicit val timeoutSeconds = timeout.seconds

    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
    val futureResponse = pipeline(Get(downloadUrl(workflowId)))

    futureResponse.onComplete { _ =>
      Try(IO(Http).ask(Http.CloseAll)(1.second).await)
      sparkutils.AkkaUtils.terminate(system)
    }
    futureResponse.map(handleResponse)
  }

  private def handleResponse(response: HttpResponse): String = {
    response.status match {
      case StatusCodes.OK =>
        response.entity.data.asString
      case _ => throw UnexpectedHttpResponseException(
        "Workflow download failed", response.status, response.entity.data.asString)
    }
  }
} 
Example 2
Source File: RestInterface.scala    From akka-sharding-example   with MIT License 5 votes vote down vote up
package com.michalplachta.shoesorter.api

import akka.actor.{Actor, ActorLogging, ActorRef}
import akka.io.IO
import akka.pattern.ask
import com.michalplachta.shoesorter.Domain.{Container, Junction}
import com.michalplachta.shoesorter.Messages._
import spray.can.Http
import spray.httpx.SprayJsonSupport._
import spray.routing._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

class RestInterface(decider: ActorRef, exposedPort: Int) extends Actor with HttpServiceBase with ActorLogging {
  val route: Route = {
    path("junctions" / IntNumber / "decisionForContainer" / IntNumber) { (junctionId, containerId) =>
      get {
        complete {
          log.info(s"Request for junction $junctionId and container $containerId")
          val junction = Junction(junctionId)
          val container = Container(containerId)
          decider.ask(WhereShouldIGo(junction, container))(5 seconds).mapTo[Go]
        }
      }
    }
  }

  def receive = runRoute(route)

  implicit val system = context.system
  IO(Http) ! Http.Bind(self, interface = "0.0.0.0", port = exposedPort)
} 
Example 3
Source File: WorkflowDownloadClient.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.Try

import akka.actor.ActorSystem
import akka.io.IO
import akka.pattern.ask
import spray.can.Http
import spray.client.pipelining._
import spray.http._
import spray.util._

import io.deepsense.commons.utils.Logging
import io.deepsense.sparkutils
import io.deepsense.workflowexecutor.exception.UnexpectedHttpResponseException

class WorkflowDownloadClient(
    val address: String,
    val path: String,
    val timeout: Int)
  extends Logging {

  val downloadUrl = (workflowId: String) =>
    s"$address/$path/$workflowId/download"

  def downloadWorkflow(workflowId: String): Future[String] = {

    logger.info(s"Downloading workflow $workflowId...")

    implicit val system = ActorSystem()
    import system.dispatcher
    implicit val timeoutSeconds = timeout.seconds

    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
    val futureResponse = pipeline(Get(downloadUrl(workflowId)))

    futureResponse.onComplete { _ =>
      Try(IO(Http).ask(Http.CloseAll)(1.second).await)
      sparkutils.AkkaUtils.terminate(system)
    }
    futureResponse.map(handleResponse)
  }

  private def handleResponse(response: HttpResponse): String = {
    response.status match {
      case StatusCodes.OK =>
        response.entity.data.asString
      case _ => throw UnexpectedHttpResponseException(
        "Workflow download failed", response.status, response.entity.data.asString)
    }
  }
} 
Example 4
Source File: RestClient.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.commons.rest.client

import java.net.URL
import java.util.UUID

import scala.concurrent.{ExecutionContext, Future}

import akka.io.IO
import akka.pattern.ask
import spray.can.Http
import spray.can.Http.HostConnectorInfo
import spray.client.pipelining._
import spray.http.{HttpCredentials, HttpRequest, HttpResponse}
import spray.httpx.unmarshalling.FromResponseUnmarshaller

trait RestClient extends RestClientImplicits {
  import RestClient._

  def apiUrl: URL
  def userId: Option[UUID]
  def userName: Option[String]
  def credentials: Option[HttpCredentials]
  implicit override val ctx: ExecutionContext = as.dispatcher

  private def hostConnectorFut(): Future[HostConnectorInfo] = {
    (IO(Http) ? Http.HostConnectorSetup(apiUrl.getHost, port = apiUrl.getPort)).mapTo[HostConnectorInfo]
  }

  private def sendReceivePipeline() : Future[HttpRequest => Future[HttpResponse]] = {
    for {
      HostConnectorInfo(hostConnector, _) <- hostConnectorFut()
    } yield {
      credentials.map(addCredentials).getOrElse[RequestTransformer](identity) ~>
        userId.map(id => addHeader(UserIdHeader, id.toString)).getOrElse[RequestTransformer](identity) ~>
        userName.map(addHeader(UserNameHeader, _)).getOrElse[RequestTransformer](identity) ~>
        sendReceive(hostConnector)
    }
  }

  private def unmarshalPipeline[U: FromResponseUnmarshaller](): Future[HttpRequest => Future[U]] = {
    for {
      sr <- sendReceivePipeline()
    } yield {
      sr ~> unmarshal[U]
    }
  }

  def fetchResponse[U : FromResponseUnmarshaller](
      req: HttpRequest
  ): Future[U] = {
    unmarshalPipeline().flatMap(_(req))
  }

  def fetchHttpResponse(req: HttpRequest) : Future[HttpResponse] = {
    sendReceivePipeline().flatMap(_(req))
  }

  def endpointPath(endpoint: String): String = {
    new URL(apiUrl, endpoint).getFile
  }
}


object RestClient {
  val UserIdHeader = "X-Seahorse-UserId"
  val UserNameHeader = "X-Seahorse-UserName"
} 
Example 5
Source File: Main.scala    From geotrellis-osm-elevation   with Apache License 2.0 5 votes vote down vote up
package geotrellis.osme.server

import geotrellis.raster._
import geotrellis.raster.io.geotiff._
import geotrellis.raster.render._
import geotrellis.raster.resample._

import geotrellis.spark._
import geotrellis.spark.io._
import geotrellis.spark.io.file._
import geotrellis.spark.io.avro._
import geotrellis.spark.io.avro.codecs._

import geotrellis.spark.io.index._

import org.apache.spark._
import org.apache.avro.Schema

import com.github.nscala_time.time.Imports._
import akka.actor._
import akka.io.IO
import spray.can.Http
import spray.routing.{HttpService, RequestContext}
import spray.routing.directives.CachingDirectives
import spray.http.MediaTypes
import spray.json._
import spray.json.DefaultJsonProtocol._

import com.typesafe.config.ConfigFactory

import scala.concurrent._
import scala.collection.JavaConverters._
import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    val conf =
      new SparkConf()
        .setIfMissing("spark.master", "local[*]")
        .setAppName("Osme Server")
        .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
        .set("spark.kryo.registrator", "geotrellis.spark.io.hadoop.KryoRegistrator")

    implicit val sc = new SparkContext(conf)

    implicit val system = akka.actor.ActorSystem("demo-system")

    // create and start our service actor
    val service =
      system.actorOf(Props(classOf[OsmeServiceActor], sc), "osme")

    // start a new HTTP server on port 8088 with our service actor as the handler
    IO(Http) ! Http.Bind(service, "0.0.0.0", 8088)
  }
} 
Example 6
Source File: BootstrapTestApp.scala    From scalajs-bootstrap   with MIT License 5 votes vote down vote up
package com.karasiq.bootstrap.test.backend

import java.util.concurrent.TimeUnit

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import spray.can.Http
import spray.http._
import spray.routing.HttpService

import com.karasiq.bootstrap.test.frontend.TestHtmlPage

object BootstrapTestApp extends App {
  final class AppHandler extends Actor with HttpService {
    override def receive: Actor.Receive = runRoute {
      get {
         {
          // Server-rendered page
          path("serverside.html") {
            complete(HttpResponse(entity = HttpEntity(ContentType(MediaTypes.`text/html`), TestHtmlPage())))
          } ~
          // Index page
          (pathSingleSlash & respondWithMediaType(MediaTypes.`text/html`)) {
            getFromResource("webapp/index.html")
          } ~
          // Other resources
          getFromResourceDirectory("webapp")
        }
      }
    }

    override def actorRefFactory: ActorRefFactory = context
  }

  def startup(): Unit = {
    implicit val timeout = Timeout(20 seconds)

    implicit val actorSystem = ActorSystem("bootstrap-test")

    Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
      override def run(): Unit = {
        Await.result(actorSystem.terminate(), FiniteDuration(5, TimeUnit.MINUTES))
      }
    }))

    val service = actorSystem.actorOf(Props[AppHandler], "webService")
    IO(Http) ? Http.Bind(service, interface = "localhost", port = 9000)
  }

  startup()
} 
Example 7
Source File: Boot.scala    From unicorn   with Apache License 2.0 5 votes vote down vote up
package unicorn.rhino

import scala.concurrent.duration._
import akka.actor.{ActorSystem, Props}
import akka.routing.FromConfig
import akka.io.IO
import spray.can.Http
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory


object Boot extends App {

  // we need an ActorSystem to host our application in
  implicit val actorSystem = ActorSystem("unicorn-rhino")

  // create a pool of RhinoActors
  val service = actorSystem.actorOf(FromConfig.props(Props[RhinoActor]), "rhino-router")

  val conf = ConfigFactory.load()
  val serverPort = conf.getInt("spray.can.server.port")

  val ip = if (System.getProperty("loopback.only") != null) "127.0.0.1" else "0.0.0.0"
  IO(Http) ! Http.Bind(service, interface = ip, port = serverPort)
} 
Example 8
Source File: EchoServer.scala    From Neutrino   with Apache License 2.0 5 votes vote down vote up
package com.ebay.neutrino

import java.util.concurrent.TimeUnit

import akka.actor._
import akka.io.IO
import com.ebay.neutrino.util.Random
import com.typesafe.config.{Config, ConfigFactory}
import spray.can.Http
import spray.http._

import scala.concurrent.duration._


object EchoServer extends App {

  // Extract port from args, if provided
  val port = if (args.size > 0) args(0).toInt else 8081

  // Load our configuration from file and merge in the port parameter
  val config = ConfigFactory.parseString(s"echo-server.port = $port") withFallback ConfigFactory.load("echo.conf")
  val system = ActorSystem("echo-server", config)
  system.actorOf(Props[EchoServer], "echo-server")
}


class EchoServer extends Actor with ActorLogging {
  import scala.language.implicitConversions

  implicit val system = context.system
  val startup  = System.currentTimeMillis
  val settings = EchoServerSettings(system)

  //Use the system's dispatcher as ExecutionContext
  import system.dispatcher

  // Register connection service
  IO(Http) ! Http.Bind(self, interface = settings.host, port = settings.port)

  
case class EchoServerSettings(host: String, port: Int, random: Boolean, duration: FiniteDuration)
  extends Extension
{
  def latency = random match {
    case false => duration
    case true  => Random.nextMillis(duration)
  }
}

object EchoServerSettings {

  def apply(c: Config): EchoServerSettings = EchoServerSettings(
    c getString "host",
    c getInt "port",
    c getBoolean "random",
    c getDuration("duration", TimeUnit.MILLISECONDS) milliseconds
  )

  def apply(system: ActorSystem): EchoServerSettings =
    EchoServerSettings(system.settings.config getConfig "echo-server")

} 
Example 9
Source File: KeepAliveTest.scala    From Neutrino   with Apache License 2.0 5 votes vote down vote up
package com.ebay.neutrino.channel

import akka.actor.ActorSystem
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.ebay.neutrino.{EchoIntegrationTest, ProxyIntegrationTest}
import org.scalatest._
import spray.can.Http
import spray.http._

import scala.concurrent.Await
import scala.concurrent.duration._


class KeepAliveTest extends FlatSpec with ProxyIntegrationTest with EchoIntegrationTest with Matchers {
  behavior of "KeepAlive integration support"

  // Default requests
  override val configurationFile = "proxy-echo-test.conf"

  implicit val system = ActorSystem()
  implicit val timeout: Timeout = 15 seconds
  val Get = HttpRequest(HttpMethods.GET, Uri("http://localhost:8080/someurl"))


  def send(request: HttpRequest) = (IO(Http) ? request).mapTo[HttpResponse]


  it should "successfully send a single HTTP 1.1 GET" in {
    val request  = Get
    val response = Await.result(send(request), 2 seconds)

    //response.status should be (StatusCodes.OK)
    //response.protocol should be (HttpProtocols.`HTTP/1.1`)
    //response.header[HttpHeaders.Connection] shouldBe a [Some[HttpHeader]]
    //response.headers find (_ == HttpHeaders.Connection) .hasKeepAlive should be (false)
  }

  it should "successfully send a single HTTP 1.1 GET with connection-close" in {
    val start    = System.currentTimeMillis
    val request  = Get.copy(headers = List(HttpHeaders.Connection("close")))
    val response = Await.result(send(request), 2 seconds)

    response.status should be (StatusCodes.OK)
    response.protocol should be (HttpProtocols.`HTTP/1.1`)
    response.header[HttpHeaders.Connection] should be (Some(HttpHeaders.Connection("close")))
  }

  it should "successfully send a single HTTP 1.0 GET" in {
    val request  = Get.copy(protocol=HttpProtocols.`HTTP/1.0`)
    val response = Await.result(send(request), 2 seconds)

    // Regardless of Client:1.0, server should support 1.1
    response.status should be (StatusCodes.OK)
    response.protocol should be (HttpProtocols.`HTTP/1.1`)
    // response.headers should not contain keep-alive
  }

  it should "successfully send a single HTTP 1.0 GET w. keepalive" in {
    val request  = Get.copy(protocol=HttpProtocols.`HTTP/1.0`, headers = List(HttpHeaders.Connection("Keep-Alive")))
    val response = Await.result(send(request), 2 seconds)

    // Regardless of Client:1.0, server should support 1.1
    response.status should be (StatusCodes.OK)
    response.protocol should be (HttpProtocols.`HTTP/1.1`)
    response.header[HttpHeaders.Connection] shouldBe a [Some[_]]
    response.header[HttpHeaders.Connection].get.hasKeepAlive should be (true)
  }
} 
Example 10
Source File: HttpTransport.scala    From shield   with MIT License 5 votes vote down vote up
package shield.transports

import akka.actor.{ActorRef, ActorRefFactory}
import akka.pattern.ask
import akka.io.IO
import akka.util.Timeout
import spray.can.Http
import spray.can.Http.HostConnectorSetup
import spray.can.client.HostConnectorSettings
import spray.http.Uri.Authority
import spray.http.{Uri, HttpResponsePart, HttpResponse, HttpRequest}
import spray.httpx.{ResponseTransformation, RequestBuilding}
import spray.util._
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._

object HttpTransport extends RequestBuilding with ResponseTransformation {
  // Imitating client\pipelining.scala from spray, but taking advantage of the fact that
  // Spray's "Request-level API" lets you send a tuple of (Request, ConnectorSetup) to specify destination
  // without an absolute uri or a Host header
  type SendReceive = spray.client.pipelining.SendReceive

  def httpTransport(host: Uri, futureTimeout: FiniteDuration)(implicit refFactory: ActorRefFactory, executionContext: ExecutionContext) : SendReceive =
    httpTransport(IO(Http)(actorSystem), HostConnectorSetup(host.authority.host.address, host.effectivePort, sslEncryption = host.scheme == "https"))(executionContext, Timeout(futureTimeout))

  def httpTransport(host: Uri, settings: HostConnectorSettings)(implicit refFactory: ActorRefFactory, executionContext: ExecutionContext,
                                                                futureTimeout: Timeout) : SendReceive =
    httpTransport(IO(Http)(actorSystem), HostConnectorSetup(host.authority.host.address, host.effectivePort, sslEncryption = host.scheme == "https", settings=Some(settings)))


  def httpTransport(transport: ActorRef, config: HostConnectorSetup)(implicit ec: ExecutionContext, futureTimeout: Timeout): SendReceive =
    request => transport ? (request, config) map {
      case x: HttpResponse          => x
      case x: HttpResponsePart      => sys.error("sendReceive doesn't support chunked responses, try sendTo instead")
      case x: Http.ConnectionClosed => sys.error("Connection closed before reception of response: " + x)
      case x                        => sys.error("Unexpected response from HTTP transport: " + x)
    }
} 
Example 11
Source File: ShieldActor.scala    From shield   with MIT License 5 votes vote down vote up
package shield.actors

import akka.actor._
import akka.io.{IO, Tcp}
import shield.config.{DomainSettings, Settings}
import shield.metrics.Instrumented
import shield.routing._
import spray.can.Http
import spray.http._

// todo: verify how Spray handles HEAD requests
// todo: 412 Precondition Failed support
// todo: 417 Expectation Failed support
// todo: does spray automatically handle `505 HTTP Version Not Supported`?
// todo: periodically inspect a response to validate it's compliance with the documented swagger schema
object ShieldActor {
  def props() : Props = Props(new ShieldActor())
}

object ShieldActorMsgs {
  case class DomainsUpdated(domains: Map[String, DomainSettings])
  case class RouterUpdated(domain: String, router: RequestRouter)
  case class ListenersUpdated(domain: String, listeners: collection.Map[String, ActorRef])
}
class ShieldActor() extends Actor with ActorLogging with RestartLogging with Instrumented {
  // todo: integrate spray's service statistics into this
  import context.system
  val settings = Settings(context.system)

  val domainWatcher = context.actorOf(Props(settings.DomainWatcher), "domain-watcher")
  var domains = Map[String, DomainSettings]()

  var defaultRouter = buildDefaultRouter()
  var routers = Map[String, RequestRouter]()
  var listeners = Map[String,  collection.Map[String, ActorRef]]()

  // start a new HTTP server on port 8080 with our service actor as the handler
  IO(Http) ! Http.Bind(self, interface = "0.0.0.0", port = settings.Port)

  val connectionCounter = metrics.counter("open-connections")
  val requestMeter = metrics.meter("requests")
  val connectionOpenMeter = metrics.meter("connection-open")
  val connectionCloseMeter = metrics.meter("connection-close")


  def receive: Receive = {
    // todo: graceful shutdown (save bound's sender for later)
    case Http.Bound(addr) =>
      log.info("Server successfully bound to {}", addr)

    case Http.CommandFailed(cmd) =>
      log.error("Failed to bind the server: cmd={}", cmd)
      system.shutdown()

    case _ : Tcp.Connected =>
      connectionCounter += 1
      connectionOpenMeter.mark()
      sender ! Tcp.Register(self)
    case _ : Tcp.ConnectionClosed =>
      connectionCounter -= 1
      connectionCloseMeter.mark()

    case request: HttpRequest =>
      requestMeter.mark()
      val domain = request.header[HttpHeaders.Host].map(_.host).getOrElse(request.uri.authority.host.address)
      val destination = for {
        location <- Some(settings.DefaultServiceLocation)
        domainSettings <- domains.get(domain)
        router <- routers.get(domain)
        listeners <- listeners.get(domain)
      } yield (location, domainSettings.IgnoreExtensions, router, listeners)
      val (location, ignoredExtensions, router, listenerList) = destination.getOrElse(settings.DefaultServiceLocation, Set[String](), defaultRouter, collection.Map[String, ActorRef]())

      // todo: profiling optimization: destroying these actors is expensive.  Make a resizable pool
      context.actorOf(RequestProcessor.props(router, listenerList, sender(), location, ignoredExtensions)) ! request

    case du: ShieldActorMsgs.DomainsUpdated =>
      val newDomains = du.domains.keySet.diff(domains.keySet)
      for (d <- newDomains) {
        routers += d -> BootstrapRouter.router(settings.DefaultServiceLocation, settings.LocalServiceName, settings.HealthcheckTemplate)
        listeners += d -> collection.Map[String, ActorRef]()
      }
      val removedDomains = domains.keySet.diff(du.domains.keySet)
      for (d <- removedDomains) {
        routers -= d
        listeners -= d
      }
      domains = du.domains
      defaultRouter = buildDefaultRouter()

    case ru: ShieldActorMsgs.RouterUpdated => routers += ru.domain -> ru.router
    case lu: ShieldActorMsgs.ListenersUpdated => listeners += lu.domain -> lu.listeners
  }

  private def buildDefaultRouter() : RequestRouter = {
    BootstrapRouter.defaultRouter(settings.DefaultServiceLocation, settings.LocalServiceName, settings.HealthcheckTemplate, domains.keySet)
  }
} 
Example 12
Source File: TotalTweetsScheduler.scala    From redrock   with Apache License 2.0 5 votes vote down vote up
package com.restapi

import java.io.{File, FileInputStream}

import akka.actor.{ActorRef, Actor, ActorSystem, Props}
import akka.io.IO
import org.slf4j.LoggerFactory
import play.api.libs.json.Json
import spray.can.Http
import akka.pattern.ask
import spray.http.DateTime
import scala.concurrent.duration._
import akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
import org.apache.commons.codec.digest.DigestUtils
import scala.io.Source

case object GetTotalTweetsScheduler

object CurrentTotalTweets {
  @volatile
  var totalTweets: Long = 0
}

class ExecuterTotalTweetsES(delay: FiniteDuration, interval: FiniteDuration) extends Actor {
  context.system.scheduler.schedule(delay, interval) {
    getTotalTweetsES
  }

  val logger = LoggerFactory.getLogger(this.getClass)

  override def receive: Actor.Receive = {
    case GetTotalTweetsScheduler => {
      logger.info(s"Getting Total of Tweets. Begin: ${CurrentTotalTweets.totalTweets}")
    }
    case _ => // just ignore any messages
  }

  def getTotalTweetsES: Unit = {
    val elasticsearchRequests = new GetElasticsearchResponse(0, Array[String](), Array[String](),
      LoadConf.restConf.getString("searchParam.defaulStartDatetime"),
      LoadConf.restConf.getString("searchParam.defaultEndDatetime"),
      LoadConf.esConf.getString("decahoseIndexName"))
    val totalTweetsResponse = Json.parse(elasticsearchRequests.getTotalTweetsESResponse())
    logger.info(s"Getting Total of Tweets. Current: ${CurrentTotalTweets.totalTweets}")
    CurrentTotalTweets.totalTweets = (totalTweetsResponse \ "hits" \ "total").as[Long]
    logger.info(s"Total users updated. New: ${CurrentTotalTweets.totalTweets}")
  }
} 
Example 13
Source File: RestClient.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.commons.rest.client

import java.net.URL
import java.util.UUID

import scala.concurrent.{ExecutionContext, Future}

import akka.io.IO
import akka.pattern.ask
import spray.can.Http
import spray.can.Http.HostConnectorInfo
import spray.client.pipelining._
import spray.http.{HttpCredentials, HttpRequest, HttpResponse}
import spray.httpx.unmarshalling.FromResponseUnmarshaller

trait RestClient extends RestClientImplicits {
  import RestClient._

  def apiUrl: URL
  def userId: Option[UUID]
  def userName: Option[String]
  def credentials: Option[HttpCredentials]
  implicit override val ctx: ExecutionContext = as.dispatcher

  private def hostConnectorFut(): Future[HostConnectorInfo] = {
    (IO(Http) ? Http.HostConnectorSetup(apiUrl.getHost, port = apiUrl.getPort)).mapTo[HostConnectorInfo]
  }

  private def sendReceivePipeline() : Future[HttpRequest => Future[HttpResponse]] = {
    for {
      HostConnectorInfo(hostConnector, _) <- hostConnectorFut()
    } yield {
      credentials.map(addCredentials).getOrElse[RequestTransformer](identity) ~>
        userId.map(id => addHeader(UserIdHeader, id.toString)).getOrElse[RequestTransformer](identity) ~>
        userName.map(addHeader(UserNameHeader, _)).getOrElse[RequestTransformer](identity) ~>
        sendReceive(hostConnector)
    }
  }

  private def unmarshalPipeline[U: FromResponseUnmarshaller](): Future[HttpRequest => Future[U]] = {
    for {
      sr <- sendReceivePipeline()
    } yield {
      sr ~> unmarshal[U]
    }
  }

  def fetchResponse[U : FromResponseUnmarshaller](
      req: HttpRequest
  ): Future[U] = {
    unmarshalPipeline().flatMap(_(req))
  }

  def fetchHttpResponse(req: HttpRequest) : Future[HttpResponse] = {
    sendReceivePipeline().flatMap(_(req))
  }

  def endpointPath(endpoint: String): String = {
    new URL(apiUrl, endpoint).getFile
  }
}


object RestClient {
  val UserIdHeader = "X-Seahorse-UserId"
  val UserNameHeader = "X-Seahorse-UserName"
} 
Example 14
Source File: HighchartsTestApp.scala    From scalajs-highcharts   with MIT License 5 votes vote down vote up
package com.karasiq.highcharts.test.backend

import java.util.concurrent.TimeUnit

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import spray.can.Http
import spray.http.MediaTypes
import spray.routing.HttpService

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps

object HighchartsTestApp extends App {
  final class AppHandler extends Actor with HttpService {
    override def receive: Actor.Receive = runRoute {
      get {
        compressResponse() {
          // Index page
          (pathSingleSlash & respondWithMediaType(MediaTypes.`text/html`)) {
            getFromResource("webapp/index.html")
          } ~
          // Other resources
          getFromResourceDirectory("webapp")
        }
      }
    }

    override def actorRefFactory: ActorRefFactory = context
  }

  def startup(): Unit = {
    implicit val timeout = Timeout(20 seconds)

    implicit val actorSystem = ActorSystem("highcharts-test")

    val service = actorSystem.actorOf(Props[AppHandler], "webService")

    IO(Http) ? Http.Bind(service, interface = "localhost", port = 9000)

    Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
      override def run(): Unit = {
        Await.result(actorSystem.terminate(), FiniteDuration(5, TimeUnit.MINUTES))
      }
    }))
  }

  startup()
} 
Example 15
Source File: Main.scala    From quiz-management-service   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.quiz.management

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import spray.can.Http

import scala.concurrent.duration._

object Main extends App {
  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")

  implicit val system = ActorSystem("quiz-management-service")

  val api = system.actorOf(Props(new RestInterface()), "httpInterface")

  implicit val executionContext = system.dispatcher
  implicit val timeout = Timeout(10 seconds)

  IO(Http).ask(Http.Bind(listener = api, interface = host, port = port))
    .mapTo[Http.Event]
    .map {
      case Http.Bound(address) =>
        println(s"REST interface bound to $address")
      case Http.CommandFailed(cmd) =>
        println("REST interface could not bind to " +
          s"$host:$port, ${cmd.failureMessage}")
        system.shutdown()
    }
} 
Example 16
Source File: Main.scala    From quiz-management-service   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.quiz.management

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import spray.can.Http

import scala.concurrent.duration._

object Main extends App {
  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")

  implicit val system = ActorSystem("quiz-management-service")
  implicit val executionContext = system.dispatcher
  implicit val timeout = Timeout(10 seconds)

  val api = system.actorOf(Props(new RestInterface))

  IO(Http).ask(Http.Bind(listener = api, interface = host, port = port))
    .mapTo[Http.Event]
    .map {
      case Http.Bound(address) =>
        println(s"REST interface bound to $address")
      case Http.CommandFailed(cmd) =>
        println("REST interface could not bind to " +
          s"$host:$port, ${cmd.failureMessage}")
        system.shutdown()
    }
} 
Example 17
Source File: Main.scala    From quiz-management-service   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.quiz.management

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import spray.can.Http

import scala.concurrent.duration._

object Main extends App {
  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")

  implicit val system = ActorSystem("quiz-management-service")

  val api = system.actorOf(Props(new RestInterface()), "httpInterface")

  implicit val executionContext = system.dispatcher
  implicit val timeout = Timeout(10 seconds)

  IO(Http).ask(Http.Bind(listener = api, interface = host, port = port))
    .mapTo[Http.Event]
    .map {
      case Http.Bound(address) =>
        println(s"REST interface bound to $address")
      case Http.CommandFailed(cmd) =>
        println("REST interface could not bind to " +
          s"$host:$port, ${cmd.failureMessage}")
        system.shutdown()
    }
} 
Example 18
Source File: Main.scala    From quiz-management-service   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.quiz.management

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import spray.can.Http

import scala.concurrent.duration._

object Main extends App {
  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")

  implicit val system = ActorSystem("quiz-management-service")

  val api = system.actorOf(Props(new RestInterface()), "httpInterface")

  implicit val executionContext = system.dispatcher
  implicit val timeout = Timeout(10 seconds)

  IO(Http).ask(Http.Bind(listener = api, interface = host, port = port))
    .mapTo[Http.Event]
    .map {
      case Http.Bound(address) =>
        println(s"REST interface bound to $address")
      case Http.CommandFailed(cmd) =>
        println("REST interface could not bind to " +
          s"$host:$port, ${cmd.failureMessage}")
        system.shutdown()
    }
} 
Example 19
Source File: SpartaHelper.scala    From sparta   with Apache License 2.0 5 votes vote down vote up
package com.stratio.sparta.serving.api.helpers

import akka.actor.{ActorSystem, Props}
import akka.event.slf4j.SLF4JLogging
import akka.io.IO
import com.stratio.sparkta.serving.api.ssl.SSLSupport
import com.stratio.sparta.driver.service.StreamingContextService
import com.stratio.sparta.serving.api.actor._
import com.stratio.sparta.serving.core.actor.StatusActor.AddClusterListeners
import com.stratio.sparta.serving.core.actor.{FragmentActor, RequestActor, StatusActor}
import com.stratio.sparta.serving.core.config.SpartaConfig
import com.stratio.sparta.serving.core.constants.AkkaConstant._
import com.stratio.sparta.serving.core.curator.CuratorFactoryHolder
import spray.can.Http


  def initSpartaAPI(appName: String): Unit = {
    if (SpartaConfig.mainConfig.isDefined && SpartaConfig.apiConfig.isDefined) {
      val curatorFramework = CuratorFactoryHolder.getInstance()

      log.info("Initializing Sparta Actors System ...")
      implicit val system = ActorSystem(appName, SpartaConfig.mainConfig)

      val statusActor = system.actorOf(Props(new StatusActor(curatorFramework)), StatusActorName)
      val fragmentActor = system.actorOf(Props(new FragmentActor(curatorFramework)), FragmentActorName)
      val policyActor = system.actorOf(Props(new PolicyActor(curatorFramework, statusActor)), PolicyActorName)
      val executionActor = system.actorOf(Props(new RequestActor(curatorFramework)), ExecutionActorName)
      val scService = StreamingContextService(curatorFramework, SpartaConfig.mainConfig)
      val launcherActor = system.actorOf(Props(new LauncherActor(scService, curatorFramework)), LauncherActorName)
      val pluginActor = system.actorOf(Props(new PluginActor()), PluginActorName)
      val driverActor = system.actorOf(Props(new DriverActor()), DriverActorName)
      val configActor = system.actorOf(Props(new ConfigActor()), ConfigActorName)
      val metadataActor = system.actorOf(Props(new MetadataActor()), MetadataActorName)
      val actors = Map(
        StatusActorName -> statusActor,
        FragmentActorName -> fragmentActor,
        PolicyActorName -> policyActor,
        LauncherActorName -> launcherActor,
        PluginActorName -> pluginActor,
        DriverActorName -> driverActor,
        ExecutionActorName -> executionActor,
        ConfigActorName -> configActor,
        MetadataActorName -> metadataActor
      )
      val controllerActor = system.actorOf(Props(new ControllerActor(actors, curatorFramework)), ControllerActorName)

      IO(Http) ! Http.Bind(controllerActor,
        interface = SpartaConfig.apiConfig.get.getString("host"),
        port = SpartaConfig.apiConfig.get.getInt("port")
      )
      log.info("Sparta Actors System initiated correctly")

      statusActor ! AddClusterListeners
    } else log.info("Sparta Configuration is not defined")
  }

} 
Example 20
Source File: ScoringTask.scala    From gearpump-examples   with Apache License 2.0 5 votes vote down vote up
package io.gearpump.examples.kafka_hdfs_pipeline

import akka.io.IO
import akka.pattern.ask
import io.gearpump.Message
import io.gearpump.cluster.UserConfig
import io.gearpump.streaming.task.{Task, TaskContext}
import io.gearpump.util.Constants
import spray.can.Http
import spray.http.HttpMethods._
import spray.http.{HttpRequest, HttpResponse, Uri}
import upickle.default._

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.util.{Failure, Success, Try}


class ScoringTask(taskContext : TaskContext, config: UserConfig) extends Task(taskContext, config) {
  implicit val timeout = Constants.FUTURE_TIMEOUT
  implicit val ec: ExecutionContext = system.dispatcher

  import taskContext.output

  override def onNext(msg: Message): Unit = {
    Try( {
      val jsonData = msg.msg.asInstanceOf[Array[Byte]]
      val jsonValue = new String(jsonData)
      val spaceShuttleMessage = read[SpaceShuttleMessage](jsonValue)
      val vector = read[Array[Float]](spaceShuttleMessage.body)
      val featureVector = vector.drop(1)
      val featureVectorString = featureVector.mkString(",")
      val url = s"http://atk-scoringengine.demo-gotapaas.com/v1/models/DemoModel/score?data=$featureVectorString"

      val result = Await.result((IO(Http) ? HttpRequest(GET, Uri(url))).asInstanceOf[Future[HttpResponse]], 5 seconds)
      val entity = result.entity.data.asString.toFloat
      entity match {
        case 1.0F =>
        case anomaly:Float =>
          output(Message(SpaceShuttleRecord(System.currentTimeMillis, anomaly), System.currentTimeMillis))
      }
    }) match {
      case Success(ok) =>
      case Failure(throwable) =>
        LOG.error(s"failed ${throwable.getMessage}")
    }

  }
} 
Example 21
Source File: Boot.scala    From reactive-consul   with MIT License 5 votes vote down vote up
package stormlantern.consul.example

import java.net.URL

import akka.actor.ActorSystem
import akka.io.IO
import akka.pattern._
import akka.util.Timeout
import spray.can.Http
import spray.json.{ JsString, JsObject }
import stormlantern.consul.client.discovery.{ ConnectionStrategy, ServiceDefinition, ConnectionProvider }
import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer
import stormlantern.consul.client.ServiceBroker
import stormlantern.consul.client.DNS

import scala.concurrent.Future
import scala.concurrent.duration._

object Boot extends App {
  implicit val system = ActorSystem("reactive-consul")
  implicit val executionContext = system.dispatcher

  val service = system.actorOf(ReactiveConsulHttpServiceActor.props(), "webservice")

  implicit val timeout = Timeout(5.seconds)

  IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = 8080)

  def connectionProviderFactory = (host: String, port: Int) ⇒ new ConnectionProvider {
    val client = new SprayExampleServiceClient(new URL(s"http://$host:$port"))
    override def getConnection: Future[Any] = Future.successful(client)
  }
  val connectionStrategy1 = ConnectionStrategy("example-service-1", connectionProviderFactory)
  val connectionStrategy2 = ConnectionStrategy("example-service-2", connectionProviderFactory)

  val services = Set(connectionStrategy1, connectionStrategy2)
  val serviceBroker = ServiceBroker(DNS.lookup("consul-8500.service.consul"), services)

  system.scheduler.schedule(5.seconds, 5.seconds) {
    serviceBroker.withService("example-service-1") { client: SprayExampleServiceClient ⇒
      client.identify
    }.foreach(println)
    serviceBroker.withService("example-service-2") { client: SprayExampleServiceClient ⇒
      client.identify
    }.foreach(println)
  }
} 
Example 22
Source File: Application.scala    From redrock   with Apache License 2.0 5 votes vote down vote up
package com.restapi

import akka.actor.{ActorSystem, Props}
import akka.io.IO
import spray.can.Http
import akka.pattern.ask
import scala.concurrent.duration._
import akka.util.Timeout
import org.slf4j.LoggerFactory;


object Application extends App {
  val logger = LoggerFactory.getLogger(this.getClass)
  
  // we need an ActorSystem to host our application in
  implicit val system = ActorSystem(LoadConf.restConf.getString("actor"))
  // create and start our service actor
  val service = system.actorOf(Props[MyServiceActor], LoadConf.restConf.getString("name"))
  val sessionTimeout = system.actorOf(Props[SessionTimeoutActor])

  val sessionTable = system.actorOf(Props(classOf[SimpleSession], sessionTimeout,
    LoadConf.accessConf.getInt("delay") seconds,
    LoadConf.accessConf.getInt("timeout-interval") seconds))
  sessionTable ! InitSessionTable

  val sessionLoader = system.actorOf(Props(classOf[LoadSessionActor], sessionTable,
    LoadConf.accessConf.getInt("delay") seconds,
    LoadConf.accessConf.getInt("check-interval") seconds))
  sessionLoader ! InitFileMd5Sum

  val schedTotalTweets = system.actorOf(Props(classOf[ExecuterTotalTweetsES],
    LoadConf.restConf.getInt("totalTweetsScheduler.delay") seconds,
    LoadConf.restConf.getInt("totalTweetsScheduler.reapeatEvery") seconds))
  schedTotalTweets ! GetTotalTweetsScheduler

  implicit val timeout = Timeout(800.seconds)
  IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = LoadConf.restConf.getInt("port"))

  logger.info( s"""Application: ${LoadConf.globalConf.getString("appName")} running version: ${LoadConf.globalConf.getString("appVersion")}""".stripMargin)
}