[ 
https://issues.apache.org/jira/browse/PIO-31?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16642816#comment-16642816
 ] 

ASF GitHub Bot commented on PIO-31:
-----------------------------------

shimamoto commented on a change in pull request #474: [PIO-31] Move from spray 
to akka-http
URL: https://github.com/apache/predictionio/pull/474#discussion_r223561690
 
 

 ##########
 File path: 
tools/src/main/scala/org/apache/predictionio/tools/dashboard/CorsSupport.scala
 ##########
 @@ -18,60 +18,33 @@
 
 package org.apache.predictionio.tools.dashboard
 
-// Reference from: https://gist.github.com/waymost/4b5598523c2c7361abea
-
-import spray.http.{HttpMethods, HttpMethod, HttpResponse, AllOrigins}
-import spray.http.HttpHeaders._
-import spray.http.HttpEntity
-import spray.routing._
-import spray.http.StatusCodes
-import spray.http.ContentTypes
-
-// see also 
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
-trait CORSSupport {
-  this: HttpService =>
-
-  private val allowOriginHeader = `Access-Control-Allow-Origin`(AllOrigins)
-  private val optionsCorsHeaders = List(
-    `Access-Control-Allow-Headers`("""Origin,
-                                      |X-Requested-With,
-                                      |Content-Type,
-                                      |Accept,
-                                      |Accept-Encoding,
-                                      |Accept-Language,
-                                      |Host,
-                                      |Referer,
-                                      |User-Agent""".stripMargin.replace("\n", 
" ")),
-    `Access-Control-Max-Age`(1728000)
-  )
+// Reference from: https://gist.github.com/jeroenr/5261fa041d592f37cd80
+
+import akka.http.scaladsl.model.HttpMethods._
+import akka.http.scaladsl.model.{StatusCodes, HttpResponse}
+import akka.http.scaladsl.model.headers._
+import akka.http.scaladsl.server.Directives._
+import akka.http.scaladsl.server.{Directive0, Route}
+import com.typesafe.config.ConfigFactory
+
+trait CorsSupport {
+
+  // this directive adds access control headers to normal responses
+  private def addAccessControlHeaders: Directive0 = {
+    respondWithHeaders(
+      `Access-Control-Allow-Origin`.forRange(HttpOriginRange.`*`),
+      `Access-Control-Allow-Credentials`(true),
+      `Access-Control-Allow-Headers`("Authorization", "Content-Type", 
"X-Requested-With")
+    )
+  }
 
-  def cors[T]: Directive0 = mapRequestContext { ctx =>
-    ctx.withRouteResponseHandling {
-      // OPTION request for a resource that responds to other methods
-      case Rejected(x) if (ctx.request.method.equals(HttpMethods.OPTIONS) &&
-          x.exists(_.isInstanceOf[MethodRejection])) => {
-        val allowedMethods: List[HttpMethod] = x.collect {
-          case rejection: MethodRejection => rejection.supported
-        }
-        ctx.complete {
-          HttpResponse().withHeaders(
-            `Access-Control-Allow-Methods`(HttpMethods.OPTIONS, allowedMethods 
:_*) ::
-            allowOriginHeader ::
-            optionsCorsHeaders
-          )
-        }
-      }
-    }.withHttpResponseHeadersMapped { headers =>
-      allowOriginHeader :: headers
-    }
+  // this handles preflight OPTIONS requests.
+  private def preflightRequestHandler: Route = options {
+    complete(HttpResponse(StatusCodes.OK)
+      .withHeaders(`Access-Control-Allow-Methods`(OPTIONS, POST, PUT, GET, 
DELETE)))
   }
 
-  override def timeoutRoute: StandardRoute = complete {
-    HttpResponse(
-      StatusCodes.InternalServerError,
-      HttpEntity(ContentTypes.`text/plain(UTF-8)`,
-          "The server was not able to produce a timely response to your 
request."),
-      List(allowOriginHeader)
-    )
+  def corsHandler(r: Route): Route = addAccessControlHeaders {
+    preflightRequestHandler ~ r
 
 Review comment:
   I think this trait doesn't meet CORS API specifications. Is this okay?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Move from spray to akka-http in servers
> ---------------------------------------
>
>                 Key: PIO-31
>                 URL: https://issues.apache.org/jira/browse/PIO-31
>             Project: PredictionIO
>          Issue Type: Improvement
>          Components: Core
>            Reporter: Marcin ZiemiƄski
>            Assignee: Naoki Takezoe
>            Priority: Major
>              Labels: gsoc2017, newbie
>
> On account of the death of spray for http and it being reborn as akka-http we 
> should update EventServer and Dashbord. It should be fairly simple, as 
> described in the following guide: 
> http://doc.akka.io/docs/akka/2.4/scala/http/migration-from-spray.html



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to