spray

Why does authorize directive execute after the code it's supposed to protect?

穿精又带淫゛_ 提交于 2019-12-24 06:02:56
问题 I am using Scala 2.11.2, Akka 2.3.6 and Spray 1.3.2. I'm facing an issue with the authorize directive. Here is the interesting part of the code: val authenticatorActor = context.actorOf(Props[AuthenticatorActor]) implicit val timeout = Timeout(5 seconds) cookie("userName") { cookie => def optionUser = Await.result(authenticatorActor ? cookie.content, timeout.duration).asInstanceOf[Option[User]] authorize(isAuthorized(optionUser)) { // ????? val user = optionUser.get //do stuff } } def

Why does authorize directive execute after the code it's supposed to protect?

久未见 提交于 2019-12-24 06:02:23
问题 I am using Scala 2.11.2, Akka 2.3.6 and Spray 1.3.2. I'm facing an issue with the authorize directive. Here is the interesting part of the code: val authenticatorActor = context.actorOf(Props[AuthenticatorActor]) implicit val timeout = Timeout(5 seconds) cookie("userName") { cookie => def optionUser = Await.result(authenticatorActor ? cookie.content, timeout.duration).asInstanceOf[Option[User]] authorize(isAuthorized(optionUser)) { // ????? val user = optionUser.get //do stuff } } def

Spray.io: When (not) to use non-blocking route handling?

怎甘沉沦 提交于 2019-12-24 03:24:10
问题 If we are thinking of production grade REST API, should we use non-blocking as much as possible, e.g. def insertDbAsync(rows: RowList): Future[Unit] = ... ... val route = path("database" / "insertRowList") { post { entity(as[RowList]) { rows => log.info(s"${rows.length} rows received") val async = insertDbAsync(rows) onComplete(async) { case Success(response) => complete("success") case Failure(t) => complete("error") } } } } I'm thinking that the answer will most likely be a 'yes', but what

how to make scalatest work with spraytestkit and HttpServiceActor

我是研究僧i 提交于 2019-12-23 11:36:29
问题 I looked at spray 1.3.1 testkit documentation but could not find a proper example for what I need below: I have this sample spray 1.3.1 service trait MyService extends HttpServiceActor { def receive = runRoute(routes) val isAliveRoute = path("isalive") { get { complete("YES") } } val routes = isAliveRoute } I'm trying to test it with spray test-kit but failing to do so here is my TestCase @RunWith(classOf[JUnitRunner]) class MyServiceTest extends FlatSpec with ScalatestRouteTest with

(Un)marshall JSON with named root for Ember Data using Scala case class on Spray

早过忘川 提交于 2019-12-23 05:44:11
问题 I am writing a RESTful interface and I would like to marshall and unmarshall JSON ready for Ember Data. The wrinkle is that Ember Data wants the entity name and the two libraries I've tried, spray-json and json4s, don't appear to do this easily. Desired Ember Data format { "coursePhoto": { "photoId": 1 } } Current default format: {"photoId":15} This should come from a case class: case class CoursePhoto(photoId: Long) I did get it running with the following custom code: object

How to parse json with spray json that uses snake case (underscore notation) instead of camel case

牧云@^-^@ 提交于 2019-12-23 02:39:48
问题 How to parse json with spray json that uses snake case (underscore notation) instead of camel case? E.g. case class Test(subjectDescription: String) "{\"subject_description\":\"Medicine\"}".parseJson.convertTo[Test] should work and not throw exception. 回答1: Like this: case class Test(subjectDescription: String) implicit val testFormat = jsonFormat(Test.apply, "subject_description") "{\"subject_description\":\"Medicine\"}".parseJson.convertTo[Test] The trick here is jsonFormat function takes

Use Future in Spray Routing

房东的猫 提交于 2019-12-22 06:38:00
问题 I'm new to asynchronous programming. I read this tutorial http://danielwestheide.com/blog/2013/01/09/the-neophytes-guide-to-scala-part-8-welcome-to-the-future.html and was surprised by how effortless I can incorporate Future into the program. However, when I was using Future with Routing, the return type is kind of wrong. get { optionalCookie("commToken") { case Some(commCookie) => val response = (MTurkerProgressActor ? Register).mapTo[..].map({...}) val result = Await.result(response, 5

How can I simulate a POST request with a json body in SprayTest?

一笑奈何 提交于 2019-12-22 05:32:15
问题 If I have an endpoint that unmarshalls json like this: (path("signup")& post) { entity(as[Credentials]) { credentials => … How can I test that with a Spray test spec: "The Authentication service" should { "create a new account if none exists" in { Post("/api/authentication/signup", """{"email":"foo", "password":"foo:" }""") ~> authenticationRoute ~> check { handled === true } } } That obviously doesn't work for several reasons. What would be the correct way? 回答1: The trick is to set the

How does spray find resources - e.g. javascript

孤人 提交于 2019-12-22 05:23:21
问题 It was simple to build my first servlet with spray-io. But the recources referenced in the header are never found. < head> ... < script src="javascript/jquery/jquery-1.9.1.js"/> ... < / head> In which directory does one have to put those recsources, or how can spray be directed to look up there? Simple question, but I could not figure out. Many thankx Girgl 回答1: With Spray routing, I use these directives - pathPrefix("css") { get { getFromResourceDirectory("css") } } ~ pathPrefix("js") { get

Akka Actor Priorities

醉酒当歌 提交于 2019-12-21 07:56:17
问题 I have an actor-based system that performs periodic, cpu-intensive data ingests as well as serves RESTful endpoints. I'm using Akka actors to signal/control the various stages of the ingest process, and Spray (which is of course built on Akka) to serve my restful endpoints. My problem is this: When the ingest kicks off it consumes most of the CPU, starving the RESTful endpoints until its done. What's the best way to lower the priority of the ingest? Right now the ingest and the Spray module