dubeejw commented on a change in pull request #2218: Port Controller from Spray
to Akka
URL:
https://github.com/apache/incubator-openwhisk/pull/2218#discussion_r127706529
##########
File path:
core/controller/src/main/scala/whisk/core/controller/WebActions.scala
##########
@@ -251,35 +260,33 @@ protected[core] object WhiskWebActionsApi extends
Directives {
}
}
- private def interpretHttpResponse(code: StatusCode, headers:
List[RawHeader], str: String, transid: TransactionId): RequestContext => Unit =
{
+ private def interpretHttpResponse(code: StatusCode, headers:
List[RawHeader], str: String, transid: TransactionId) = {
val parsedHeader: Try[MediaType] = headers.find(_.lowercaseName ==
`Content-Type`.lowercaseName) match {
case Some(header) =>
- HttpParser.parseHeader(header) match {
- case Right(header: `Content-Type`) =>
- val mediaType = header.contentType.mediaType
+ MediaType.parse(header.value) match {
+ case Right(mediaType: MediaType) =>
// lookup the media type specified in the content
header to see if it is a recognized type
MediaTypes.getForKey(mediaType.mainType ->
mediaType.subType).map(Success(_)).getOrElse {
// this is a content-type that is not recognized,
reject it
Failure(RejectRequest(BadRequest,
Messages.httpUnknownContentType)(transid))
}
-
- case _ =>
- Failure(RejectRequest(BadRequest,
Messages.httpUnknownContentType)(transid))
- }
+ case _ => Failure(RejectRequest(BadRequest,
Messages.httpUnknownContentType)(transid))
+ }
case None => Success(`text/html`)
}
parsedHeader.flatMap { mediaType =>
- if (mediaType.binary) {
- Try(HttpData(Base64.getDecoder().decode(str))).map((mediaType,
_))
+ if (mediaType.binary || mediaType == `application/json`) {
+ Try(new String(Base64.getDecoder().decode(str),
"UTF-8")).map((mediaType, _))
} else {
- Success(mediaType, HttpData(str))
+ Success(mediaType, str)
}
} match {
- case Success((mediaType, data)) =>
+ case Success((mediaType, data: String)) =>
respondWithHeaders(headers) {
- respondWithMediaType(mediaType) {
- complete(code, data)
+ mediaType.toString.split("/") match {
+ case Array(mainType, subType) =>
+ complete(code,
HttpEntity(ContentType(MediaType.customWithFixedCharset(mainType, subType,
HttpCharsets.`UTF-8`)), data))
Review comment:
@rabbah, @markusthoemmes, any suggestions on this one? The change is a
result of `respondWithMediaType()` being removed from Akka as it was deemed an
anti-pattern.
I've tried using `ContentType.parse` to get the content-type from the
headers and passing it into an `HttpEntity`. Ie, `complete(code,
HttpEntity(contentType, ByteString.fromString(data)))`. However, test failures
result.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services