Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
airframe-http: Add HttpFilter support to Router (#540)
* Reorganize Router structure * Adding before/after filter * Pass the surface of a request filter to use DI * Use immutable Route implementation * Use tree structure for Router * Add workaround for slf4j flush delay * Add FinagleFilter, FinagleContext
- Loading branch information
Showing
10 changed files
with
510 additions
and
101 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...rame-http-finagle/src/main/scala/wvlet/airframe/http/finagle/FinagleResponseHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package wvlet.airframe.http.finagle | ||
|
||
import com.twitter.finagle.http.{Request, Response, Status} | ||
import com.twitter.io.Buf.ByteArray | ||
import wvlet.airframe.codec.{JSONCodec, MessageCodec, MessageCodecFactory} | ||
import wvlet.airframe.http.{ResponseHandler, SimpleHttpResponse} | ||
import wvlet.airframe.surface.Surface | ||
|
||
/** | ||
* Converting controller results into finagle http responses. | ||
*/ | ||
trait FinagleResponseHandler extends ResponseHandler[Request, Response] { | ||
|
||
// Use Map codecs to create natural JSON responses | ||
private[this] val mapCodecFactory = | ||
MessageCodecFactory.defaultFactory.withObjectMapCodec | ||
|
||
// TODO: Extract this logic into airframe-http | ||
def toHttpResponse[A](request: Request, responseSurface: Surface, a: A): Response = { | ||
a match { | ||
case r: Response => | ||
// Return the response as is | ||
r | ||
case r: SimpleHttpResponse => | ||
val resp = Response(request) | ||
resp.statusCode = r.statusCode | ||
resp.contentString = r.contentString | ||
r.contentType.map { c => | ||
resp.contentType = c | ||
} | ||
resp | ||
case s: String => | ||
val r = Response() | ||
r.contentString = s | ||
r | ||
case _ => | ||
// TODO Return large responses with streams | ||
|
||
// Convert the response object into JSON | ||
val rs = mapCodecFactory.of(responseSurface) | ||
val msgpack: Array[Byte] = rs match { | ||
case m: MessageCodec[_] => | ||
m.asInstanceOf[MessageCodec[A]].toMsgPack(a) | ||
case _ => | ||
throw new IllegalArgumentException(s"Unknown codec: ${rs}") | ||
} | ||
|
||
// Return application/msgpack content type | ||
if (request.accept.contains("application/x-msgpack")) { | ||
val res = Response(Status.Ok) | ||
res.contentType = "application/x-msgpack" | ||
res.content = ByteArray.Owned(msgpack) | ||
res | ||
} else { | ||
val json = JSONCodec.unpackMsgPack(msgpack) | ||
json match { | ||
case Some(j) => | ||
val res = Response(Status.Ok) | ||
res.setContentTypeJson() | ||
res.setContentString(json.get) | ||
res | ||
case None => | ||
Response(Status.InternalServerError) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.