-
Notifications
You must be signed in to change notification settings - Fork 594
/
PredefinedToResponseMarshallers.scala
executable file
·165 lines (139 loc) · 8.82 KB
/
PredefinedToResponseMarshallers.scala
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.http.scaladsl.marshalling
import akka.annotation.InternalApi
import akka.http.scaladsl.common.EntityStreamingSupport
import akka.http.scaladsl.model._
import akka.http.scaladsl.util.FastFuture
import akka.http.scaladsl.util.FastFuture._
import akka.util.ConstantFun
import akka.stream.scaladsl.Source
import akka.util.ByteString
import scala.collection.immutable
import scala.reflect.ClassTag
trait PredefinedToResponseMarshallers extends LowPriorityToResponseMarshallerImplicits {
import PredefinedToResponseMarshallers._
private type TRM[T] = ToResponseMarshaller[T] // brevity alias
def fromToEntityMarshaller[T](
status: StatusCode = StatusCodes.OK,
headers: immutable.Seq[HttpHeader] = Nil)(
implicit
m: ToEntityMarshaller[T]): ToResponseMarshaller[T] =
fromStatusCodeAndHeadersAndValue compose (t ⇒ (status, headers, t))
implicit val fromResponse: TRM[HttpResponse] = Marshaller.opaque(ConstantFun.scalaIdentityFunction)
/**
* Creates a response for a status code. Does not support content-type negotiation but will return
* a response either with a `text-plain` entity containing the `status.defaultMessage` or an empty entity
* for status codes that don't allow a response.
*/
implicit val fromStatusCode: TRM[StatusCode] =
Marshaller.opaque { status ⇒ statusCodeResponse(status) }
/**
* Creates a response from status code and headers. Does not support content-type negotiation but will return
* a response either with a `text-plain` entity containing the `status.defaultMessage` or an empty entity
* for status codes that don't allow a response.
*/
implicit val fromStatusCodeAndHeaders: TRM[(StatusCode, immutable.Seq[HttpHeader])] =
Marshaller.opaque { case (status, headers) ⇒ statusCodeResponse(status, headers) }
implicit def fromStatusCodeAndValue[S, T](implicit sConv: S ⇒ StatusCode, mt: ToEntityMarshaller[T]): TRM[(S, T)] =
fromStatusCodeAndHeadersAndValue[T] compose { case (status, value) ⇒ (sConv(status), Nil, value) }
implicit def fromStatusCodeConvertibleAndHeadersAndT[S, T](implicit sConv: S ⇒ StatusCode, mt: ToEntityMarshaller[T]): TRM[(S, immutable.Seq[HttpHeader], T)] =
fromStatusCodeAndHeadersAndValue[T] compose { case (status, headers, value) ⇒ (sConv(status), headers, value) }
implicit def fromStatusCodeAndHeadersAndValue[T](implicit mt: ToEntityMarshaller[T]): TRM[(StatusCode, immutable.Seq[HttpHeader], T)] =
Marshaller(implicit ec ⇒ {
case (status, headers, value) ⇒
mt(value).fast map { marshallings ⇒
val mappedMarshallings = marshallings map (_ map (statusCodeAndEntityResponse(status, headers, _)))
if (status.isSuccess)
// for 2xx status codes delegate content-type negotiation to the value marshaller
mappedMarshallings
else
// For non-2xx status, add an opaque fallback marshalling using the first returned marshalling
// that will be used if the result wouldn't otherwise be accepted.
//
// The reasoning is that users often use routes like `complete(400 -> "Illegal request in this context")`
// to eagerly complete a route with an error (instead of using rejection handling). If the client uses
// narrow Accept headers like `Accept: application/json` the user supplied error message would not be
// rendered because it will only accept `text/plain` but not `application/json` responses and fail the
// whole request with a "406 Not Acceptable" response.
//
// Adding the opaque fallback rendering will give an escape hatch for those situations.
// See akka/akka#19397, akka/akka#19842, and #1072.
mappedMarshallings match {
case Nil ⇒ Nil
case firstMarshalling :: _ ⇒ mappedMarshallings :+ firstMarshalling.toOpaque(HttpCharsets.`UTF-8`)
}
}
})
// The null ClassTag being passed to fromEntityStreamingSupportAndByteStringMarshaller is safe,
// as it is handled gracefully by NoStrictlyCompatibleElementMarshallingAvailableException.
@deprecated("This method exists only for the purpose of binary compatibility, it used to be implicit.", "10.1.0")
private[akka] def fromEntityStreamingSupportAndByteStringMarshaller[T, M](s: EntityStreamingSupport, m: ToByteStringMarshaller[T]): ToResponseMarshaller[Source[T, M]] =
fromEntityStreamingSupportAndByteStringMarshaller(null, s, m)
implicit def fromEntityStreamingSupportAndByteStringMarshaller[T: ClassTag, M](implicit s: EntityStreamingSupport, m: ToByteStringMarshaller[T]): ToResponseMarshaller[Source[T, M]] =
fromEntityStreamingSupportAndByteStringSourceMarshaller(s, m.map(Source.single))
}
trait LowPriorityToResponseMarshallerImplicits {
implicit def liftMarshallerConversion[T](m: ToEntityMarshaller[T]): ToResponseMarshaller[T] =
liftMarshaller(m)
implicit def liftMarshaller[T](implicit m: ToEntityMarshaller[T]): ToResponseMarshaller[T] =
PredefinedToResponseMarshallers.fromToEntityMarshaller()
implicit def fromEntityStreamingSupportAndEntityMarshaller[T, M](implicit s: EntityStreamingSupport, m: ToEntityMarshaller[T], tag: ClassTag[T]): ToResponseMarshaller[Source[T, M]] =
fromEntityStreamingSupportAndByteStringSourceMarshaller[T, M](s, m.map(_.dataBytes))
private[marshalling] def fromEntityStreamingSupportAndByteStringSourceMarshaller[T: ClassTag, M](s: EntityStreamingSupport, m: Marshaller[T, Source[ByteString, _]]): ToResponseMarshaller[Source[T, M]] = {
Marshaller[Source[T, M], HttpResponse] { implicit ec ⇒ source ⇒
FastFuture successful {
Marshalling.WithFixedContentType(s.contentType, () ⇒ {
val availableMarshallingsPerElement = source.mapAsync(1) { t ⇒ m(t)(ec) }
// TODO optimise such that we pick the optimal marshalling only once (headAndTail needed?)
// TODO, NOTE: this is somewhat duplicated from Marshal.scala it could be made DRYer
val bestMarshallingPerElement = availableMarshallingsPerElement map { marshallings ⇒
// pick the Marshalling that matches our EntityStreamingSupport
val selectedMarshalling = s.contentType match {
case best @ (_: ContentType.Binary | _: ContentType.WithFixedCharset | _: ContentType.WithMissingCharset) ⇒
marshallings collectFirst { case Marshalling.WithFixedContentType(`best`, marshal) ⇒ marshal }
case best @ ContentType.WithCharset(bestMT, bestCS) ⇒
marshallings collectFirst {
case Marshalling.WithFixedContentType(`best`, marshal) ⇒ marshal
case Marshalling.WithOpenCharset(`bestMT`, marshal) ⇒ () ⇒ marshal(bestCS)
}
}
// TODO we could either special case for certrain known types,
// or extend the entity support to be more advanced such that it would negotiate the element content type it
// is able to render.
selectedMarshalling.getOrElse(throw new NoStrictlyCompatibleElementMarshallingAvailableException[T](s.contentType, marshallings))
}
val marshalledElements: Source[ByteString, M] =
bestMarshallingPerElement
.flatMapConcat(_.apply()) // marshal!
.via(s.framingRenderer)
HttpResponse(entity = HttpEntity(s.contentType, marshalledElements))
}) :: Nil
}
}
}
}
object PredefinedToResponseMarshallers extends PredefinedToResponseMarshallers {
/** INTERNAL API */
@InternalApi
private def statusCodeResponse(statusCode: StatusCode, headers: immutable.Seq[HttpHeader] = Nil): HttpResponse = {
val entity =
if (statusCode.allowsEntity) HttpEntity(statusCode.defaultMessage)
else HttpEntity.Empty
HttpResponse(status = statusCode, headers = headers, entity = entity)
}
private def statusCodeAndEntityResponse(statusCode: StatusCode, headers: immutable.Seq[HttpHeader], entity: ResponseEntity): HttpResponse = {
if (statusCode.allowsEntity) HttpResponse(statusCode, headers, entity)
else HttpResponse(statusCode, headers, HttpEntity.Empty)
}
}
final class NoStrictlyCompatibleElementMarshallingAvailableException[T](
streamContentType: ContentType,
availableMarshallings: List[Marshalling[_]])(implicit tag: ClassTag[T])
extends RuntimeException(
s"None of the available marshallings ($availableMarshallings) directly " +
s"match the ContentType requested by the top-level streamed entity ($streamContentType). " +
s"Please provide an implicit `Marshaller[${if (tag == null) "T" else tag.runtimeClass.getName}, HttpEntity]` " +
s"that can render ${if (tag == null) "" else tag.runtimeClass.getName + " "}" +
s"as [$streamContentType]")