Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rest echo can handle non json request
- Loading branch information
1 parent
295223b
commit d87a191
Showing
2 changed files
with
189 additions
and
36 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,28 +1,82 @@ | ||
package io.apim.samples.rest | ||
|
||
import io.vertx.core.http.HttpHeaders | ||
import io.vertx.core.json.DecodeException | ||
import io.vertx.core.json.JsonObject | ||
import io.vertx.ext.web.impl.ParsableMIMEValue | ||
import io.vertx.kotlin.core.json.json | ||
import io.vertx.kotlin.core.json.obj | ||
import io.vertx.rxjava3.core.MultiMap | ||
import io.vertx.rxjava3.ext.web.ParsedHeaderValues | ||
import io.vertx.rxjava3.ext.web.RequestBody | ||
import io.vertx.rxjava3.ext.web.RoutingContext | ||
|
||
fun echoHandler(ctx: RoutingContext) { | ||
ctx.response() | ||
.setStatusCode(200) | ||
.putHeader("Content-Type", "application/json") | ||
.end( | ||
json { | ||
var body: JsonObject | ||
val response = ctx.response() | ||
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json") | ||
|
||
try { | ||
body = json { | ||
obj( | ||
"method" to ctx.request().method().name(), | ||
"headers" to obj(ctx.request().headers().toSimpleMap()), | ||
"query_params" to obj(ctx.request().params().toSimpleMap()), | ||
"body" to handleBody(ctx.body(), ctx.parsedHeaders()) | ||
) | ||
} | ||
response.statusCode = 200 | ||
} catch (e: DecodeException) { | ||
response.statusCode = 400 | ||
body = json { | ||
obj( | ||
"title" to "The request body fail to be parsed", | ||
"detail" to e.cause?.message | ||
) | ||
} | ||
} | ||
|
||
response.end(body.toString()).subscribe() | ||
} | ||
|
||
fun handleBody(body: RequestBody, headers: ParsedHeaderValues): JsonObject { | ||
val contentType = (headers.delegate.contentType() as ParsableMIMEValue).forceParse() | ||
|
||
if (contentType.isText()) { | ||
return json { | ||
obj( | ||
"method" to ctx.request().method().name(), | ||
"headers" to obj(ctx.request().headers().toSimpleMap()), | ||
"query_params" to obj(ctx.request().params().toSimpleMap()), | ||
"body" to ctx.body().asJsonObject() | ||
"type" to "text", | ||
"content" to body.asString() | ||
) | ||
}.toString() | ||
} | ||
} | ||
|
||
if (contentType.isJson()) { | ||
return json { | ||
obj( | ||
"type" to "json", | ||
"content" to body.asJsonObject() | ||
) | ||
} | ||
} | ||
|
||
return json { | ||
obj( | ||
"type" to "unknown", | ||
"content" to body.asString() | ||
) | ||
.subscribe() | ||
} | ||
} | ||
|
||
/** Transform a multimap into a simple map. Multiple values are joined in a string separated with ; */ | ||
/** Transform a MultiMap into a simple map. Multiple values are joined in a string separated with ; */ | ||
fun MultiMap.toSimpleMap() = this.entries() | ||
.groupBy { it.key } | ||
.mapValues { it.value.joinToString(";") { h -> h.value } } | ||
|
||
fun ParsableMIMEValue.isText(): Boolean { | ||
return this.component() == "text" | ||
} | ||
|
||
fun ParsableMIMEValue.isJson(): Boolean { | ||
return this.component() == "application" && this.subComponent().contains("json") | ||
} |
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