-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from ctasada/ctasada/bytearray_as_inputstream
Add support for InputStream
- Loading branch information
Showing
12 changed files
with
250 additions
and
2 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
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
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,32 @@ | ||
openapi: 3.0.1 | ||
info: | ||
description: Testing binary body and binary response | ||
title: Test | ||
version: '0.0' | ||
paths: | ||
/binary-data: | ||
post: | ||
operationId: postBinaryData | ||
requestBody: | ||
required: true | ||
content: | ||
application/octet-stream: | ||
schema: | ||
type: string | ||
format: binary | ||
responses: | ||
200: | ||
description: Success | ||
content: | ||
application/octet-stream: | ||
schema: | ||
type: string | ||
format: binary | ||
|
||
components: | ||
schemas: | ||
BinaryData: | ||
properties: | ||
binaryValue: | ||
type: string | ||
format: binary |
100 changes: 100 additions & 0 deletions
100
src/test/resources/examples/byteArrayStream/controllers/ktor/Controllers.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,100 @@ | ||
package ie.zalando.controllers | ||
|
||
import io.ktor.http.Headers | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.Parameters | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.application.call | ||
import io.ktor.server.plugins.BadRequestException | ||
import io.ktor.server.plugins.ParameterConversionException | ||
import io.ktor.server.request.receive | ||
import io.ktor.server.response.respond | ||
import io.ktor.server.routing.Route | ||
import io.ktor.server.routing.post | ||
import io.ktor.util.converters.DefaultConversionService | ||
import io.ktor.util.reflect.typeInfo | ||
import java.io.InputStream | ||
import kotlin.Any | ||
import kotlin.String | ||
import kotlin.Suppress | ||
|
||
public interface BinaryDataController { | ||
/** | ||
* Route is expected to respond with [java.io.InputStream]. | ||
* Use [ie.zalando.controllers.TypedApplicationCall.respondTyped] to send the response. | ||
* | ||
* @param applicationOctetStream | ||
* @param call Decorated ApplicationCall with additional typed respond methods | ||
*/ | ||
public suspend fun postBinaryData( | ||
applicationOctetStream: InputStream, | ||
call: TypedApplicationCall<InputStream>, | ||
) | ||
|
||
public companion object { | ||
/** | ||
* Mounts all routes for the BinaryData resource | ||
* | ||
* - POST /binary-data | ||
*/ | ||
public fun Route.binaryDataRoutes(controller: BinaryDataController) { | ||
post("/binary-data") { | ||
val applicationOctetStream = call.receive<InputStream>() | ||
controller.postBinaryData(applicationOctetStream, TypedApplicationCall(call)) | ||
} | ||
} | ||
|
||
/** | ||
* Gets parameter value associated with this name or null if the name is not present. | ||
* Converting to type R using DefaultConversionService. | ||
* | ||
* Throws: | ||
* ParameterConversionException - when conversion from String to R fails | ||
*/ | ||
private inline fun <reified R : Any> Parameters.getTyped(name: String): R? { | ||
val values = getAll(name) ?: return null | ||
val typeInfo = typeInfo<R>() | ||
return try { | ||
@Suppress("UNCHECKED_CAST") | ||
DefaultConversionService.fromValues(values, typeInfo) as R | ||
} catch (cause: Exception) { | ||
throw ParameterConversionException( | ||
name, | ||
typeInfo.type.simpleName | ||
?: typeInfo.type.toString(), | ||
cause, | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Gets first value from the list of values associated with a name. | ||
* | ||
* Throws: | ||
* BadRequestException - when the name is not present | ||
*/ | ||
private fun Headers.getOrFail(name: String): String = this[name] ?: throw | ||
BadRequestException("Header " + name + " is required") | ||
} | ||
} | ||
|
||
/** | ||
* Decorator for Ktor's ApplicationCall that provides type safe variants of the [respond] functions. | ||
* | ||
* It can be used as a drop-in replacement for [io.ktor.server.application.ApplicationCall]. | ||
* | ||
* @param R The type of the response body | ||
*/ | ||
public class TypedApplicationCall<R : Any>( | ||
private val applicationCall: ApplicationCall, | ||
) : ApplicationCall by applicationCall { | ||
@Suppress("unused") | ||
public suspend inline fun <reified T : R> respondTyped(message: T) { | ||
respond(message) | ||
} | ||
|
||
@Suppress("unused") | ||
public suspend inline fun <reified T : R> respondTyped(status: HttpStatusCode, message: T) { | ||
respond(status, message) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/resources/examples/byteArrayStream/controllers/micronaut/Controllers.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,23 @@ | ||
package ie.zalando.controllers | ||
|
||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.`annotation`.Body | ||
import io.micronaut.http.`annotation`.Consumes | ||
import io.micronaut.http.`annotation`.Controller | ||
import io.micronaut.http.`annotation`.Post | ||
import io.micronaut.http.`annotation`.Produces | ||
import java.io.InputStream | ||
import javax.validation.Valid | ||
|
||
@Controller | ||
public interface BinaryDataController { | ||
/** | ||
* | ||
* | ||
* @param applicationOctetStream | ||
*/ | ||
@Post(uri = "/binary-data") | ||
@Consumes(value = ["application/octet-stream"]) | ||
@Produces(value = ["application/octet-stream"]) | ||
public fun postBinaryData(@Body @Valid applicationOctetStream: InputStream): HttpResponse<InputStream> | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/resources/examples/byteArrayStream/controllers/spring/Controllers.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,28 @@ | ||
package ie.zalando.controllers | ||
|
||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Controller | ||
import org.springframework.validation.`annotation`.Validated | ||
import org.springframework.web.bind.`annotation`.RequestBody | ||
import org.springframework.web.bind.`annotation`.RequestMapping | ||
import org.springframework.web.bind.`annotation`.RequestMethod | ||
import java.io.InputStream | ||
import javax.validation.Valid | ||
|
||
@Controller | ||
@Validated | ||
@RequestMapping("") | ||
public interface BinaryDataController { | ||
/** | ||
* | ||
* | ||
* @param applicationOctetStream | ||
*/ | ||
@RequestMapping( | ||
value = ["/binary-data"], | ||
produces = ["application/octet-stream"], | ||
method = [RequestMethod.POST], | ||
consumes = ["application/octet-stream"], | ||
) | ||
public fun postBinaryData(@RequestBody @Valid applicationOctetStream: InputStream): ResponseEntity<InputStream> | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/resources/examples/byteArrayStream/models/BinaryData.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,10 @@ | ||
package examples.byteArrayStream.models | ||
|
||
import com.fasterxml.jackson.`annotation`.JsonProperty | ||
import java.io.InputStream | ||
|
||
public data class BinaryData( | ||
@param:JsonProperty("binaryValue") | ||
@get:JsonProperty("binaryValue") | ||
public val binaryValue: InputStream? = null, | ||
) |