-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
278d8e0
commit c1a6d30
Showing
9 changed files
with
279 additions
and
41 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.apim.samples.rest | ||
|
||
import io.vertx.ext.web.impl.ParsableMIMEValue | ||
import io.vertx.rxjava3.core.MultiMap | ||
|
||
/** 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") | ||
} | ||
|
||
fun ParsableMIMEValue.isAvro(): Boolean { | ||
return this.component() == "avro" || this.subComponent().contains("avro") | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/kotlin/io/apim/samples/rest/ResponseHelper.kt
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,15 @@ | ||
package io.apim.samples.rest | ||
|
||
import io.vertx.kotlin.core.json.obj | ||
import io.vertx.rxjava3.ext.web.RoutingContext | ||
|
||
fun RoutingContext.sendError(statusCode: Int, title: String, detail: String? = null) { | ||
this.response().statusCode = statusCode | ||
this.end(io.vertx.kotlin.core.json.json { | ||
obj( | ||
"title" to title, | ||
"detail" to detail | ||
) | ||
} | ||
.toString()).subscribe() | ||
} |
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
43 changes: 43 additions & 0 deletions
43
app/src/main/kotlin/io/apim/samples/rest/avro/AvroRequestHelper.kt
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,43 @@ | ||
package io.apim.samples.rest.avro | ||
|
||
import io.apim.samples.avro.SerializationFormat | ||
import io.apim.samples.rest.sendError | ||
import io.vertx.rxjava3.ext.web.RoutingContext | ||
import org.apache.avro.Schema | ||
import org.apache.avro.SchemaParseException | ||
import java.util.* | ||
|
||
fun RoutingContext.getOutputFormatFromQueryParam(param: String = "output"): OutputFormat { | ||
val output = queryParam(param).elementAtOrNull(0) ?: "avro" | ||
try { | ||
return OutputFormat.valueOf(output.uppercase(Locale.getDefault())) | ||
} catch (e: IllegalArgumentException) { | ||
sendError(400, "Invalid $param format", "Valid values are: ${OutputFormat.values().joinToString(", ") { it.name.lowercase() }}") | ||
throw e | ||
} | ||
} | ||
|
||
fun RoutingContext.getSerializationFormatFromQueryParam(param: String = "format"): SerializationFormat { | ||
val format = queryParam(param).elementAtOrNull(0) ?: SerializationFormat.CONFLUENT.name | ||
try { | ||
return SerializationFormat.valueOf(format.uppercase(Locale.getDefault())) | ||
} catch (e: IllegalArgumentException) { | ||
sendError(400, "Invalid $param", "Valid values are: ${SerializationFormat.values().joinToString(", ") { it.name.lowercase() }}") | ||
throw e | ||
} | ||
} | ||
|
||
fun RoutingContext.getSchemaFromHeader(header: String = "X-Avro-Schema"): Schema { | ||
val schemaString = request().getHeader(header) | ||
if (schemaString == null) { | ||
sendError(400, "Avro schema required in $header header") | ||
throw IllegalArgumentException("Avro schema required in $header header") | ||
} | ||
|
||
return try { | ||
Schema.Parser().parse(schemaString) | ||
} catch (e: SchemaParseException) { | ||
sendError(400, "Invalid avro schema", e.message) | ||
throw e | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/kotlin/io/apim/samples/rest/avro/AvroSerDeHandler.kt
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,37 @@ | ||
package io.apim.samples.rest.avro | ||
|
||
import io.apim.samples.avro.JsonSerDe | ||
import io.apim.samples.rest.isAvro | ||
import io.apim.samples.rest.isJson | ||
import io.apim.samples.rest.sendError | ||
import io.vertx.core.http.HttpHeaders | ||
import io.vertx.ext.web.impl.ParsableMIMEValue | ||
import io.vertx.rxjava3.core.buffer.Buffer | ||
import io.vertx.rxjava3.ext.web.RoutingContext | ||
|
||
fun avroSerDeHandler(ctx: RoutingContext) { | ||
val contentType = (ctx.parsedHeaders().delegate.contentType() as ParsableMIMEValue).forceParse() | ||
|
||
val schema = ctx.getSchemaFromHeader("X-Avro-Schema") | ||
val jsonSerDe = JsonSerDe(schema) | ||
val avroSerDe = serdeFactory.new(schema, ctx.getSerializationFormatFromQueryParam()) | ||
|
||
if(contentType.isJson()) { | ||
val data = jsonSerDe.deserialize(ctx.body().asString()) | ||
ctx.response().statusCode = 200 | ||
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "avro/binary") | ||
ctx.end(Buffer.buffer(avroSerDe.serialize(data))).subscribe() | ||
return | ||
} | ||
|
||
if(contentType.isAvro()) { | ||
val data = avroSerDe.deserialize(ctx.body().buffer().bytes) | ||
ctx.response().statusCode = 200 | ||
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json") | ||
ctx.end(Buffer.buffer(jsonSerDe.serialize(data))).subscribe() | ||
return | ||
} | ||
|
||
ctx.sendError(400, "Unsupported content type") | ||
return | ||
} |
Oops, something went wrong.