1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package xyz.driver.pdsuicommon.http
import akka.http.scaladsl.marshalling._
import akka.http.scaladsl.server.Directive1
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{PathMatcher1, PathMatchers}
import akka.http.scaladsl.model._
import xyz.driver.core.rest.AuthorizedServiceRequestContext
import xyz.driver.core.rest.ContextHeaders
import xyz.driver.entities.users.UserInfo
import xyz.driver.pdsuicommon.auth._
import xyz.driver.pdsuicommon.error._
import xyz.driver.pdsuicommon.error.DomainError._
import xyz.driver.pdsuicommon.error.ErrorsResponse.ResponseError
import xyz.driver.pdsuicommon.parsers._
import xyz.driver.pdsuicommon.db.{Pagination, Sorting, SearchFilterExpr}
import xyz.driver.pdsuicommon.domain._
import scala.util._
import scala.concurrent._
trait Directives {
val paginated: Directive1[Pagination] = parameters(('pageSize.as[Int], 'pageNumber.as[Int])).tmap {
case (size, number) => Pagination(size, number)
}
def sorted(validDimensions: Set[String]): Directive1[Sorting] = parameterSeq.flatMap { params =>
SortingParser.parse(validDimensions, params) match {
case Success(sorting) => provide(sorting)
case Failure(ex) => failWith(ex)
}
}
val searchFiltered: Directive1[SearchFilterExpr] = parameterSeq.flatMap { params =>
SearchFilterParser.parse(params) match {
case Success(sorting) => provide(sorting)
case Failure(ex) => failWith(ex)
}
}
def StringIdInPath[T]: PathMatcher1[StringId[T]] =
PathMatchers.Segment.map((id) => StringId(id.toString.toLowerCase))
def LongIdInPath[T]: PathMatcher1[LongId[T]] =
PathMatchers.LongNumber.map((id) => LongId(id))
def UuidIdInPath[T]: PathMatcher1[UuidId[T]] =
PathMatchers.JavaUUID.map((id) => UuidId(id))
@annotation.implicitNotFound("An ApiExtractor of ${Reply} to ${Api} is required to complete service replies.")
trait ApiExtractor[Reply, Api] extends PartialFunction[Reply, Api]
object ApiExtractor {
// Note: make sure the Reply here is the most common response
// type. The specific entity type should be handled in the partial
// function. E.g. `apply[GetByIdReply, Api]{case
// GetByIdReply.Entity => Api}`
def apply[Reply, Api](pf: PartialFunction[Reply, Api]): ApiExtractor[Reply, Api] = new ApiExtractor[Reply, Api] {
override def isDefinedAt(x: Reply) = pf.isDefinedAt(x)
override def apply(x: Reply) = pf.apply(x)
}
}
implicit def replyMarshaller[Reply, Api](
implicit ctx: AuthenticatedRequestContext,
apiExtractor: ApiExtractor[Reply, Api],
apiMarshaller: ToEntityMarshaller[Api],
errorMarshaller: ToEntityMarshaller[ErrorsResponse]
): ToResponseMarshaller[Reply] = {
def errorResponse(err: DomainError) =
ErrorsResponse(Seq(ResponseError(None, err.getMessage, ErrorCode.Unspecified)), ctx.requestId)
Marshaller[Reply, HttpResponse] { (executionContext: ExecutionContext) => (reply: Reply) =>
implicit val ec = executionContext
reply match {
case apiReply if apiExtractor.isDefinedAt(apiReply) =>
Marshaller.fromToEntityMarshaller[Api](StatusCodes.OK).apply(apiExtractor(apiReply))
case err: NotFoundError =>
Marshaller.fromToEntityMarshaller[ErrorsResponse](StatusCodes.Unauthorized).apply(errorResponse(err))
case err: AuthorizationError =>
Marshaller.fromToEntityMarshaller[ErrorsResponse](StatusCodes.Forbidden).apply(errorResponse(err))
case err: DomainError =>
Marshaller.fromToEntityMarshaller[ErrorsResponse](StatusCodes.BadRequest).apply(errorResponse(err))
case other =>
val msg = s"Got unexpected response type in completion directive: ${other.getClass.getSimpleName}"
val res = ErrorsResponse(Seq(ResponseError(None, msg, ErrorCode.Unspecified)), ctx.requestId)
Marshaller.fromToEntityMarshaller[ErrorsResponse](StatusCodes.InternalServerError).apply(res)
}
}
}
implicit class PdsContext(core: AuthorizedServiceRequestContext[UserInfo]) {
def authenticated = new AuthenticatedRequestContext(
core.authenticatedUser,
RequestId(),
core.contextHeaders(ContextHeaders.AuthenticationTokenHeader)
)
}
}
object Directives extends Directives
|