From 4e4684ae6f16a6999fc43c7ef0d6d21a45e934d2 Mon Sep 17 00:00:00 2001 From: Shiranuit Date: Wed, 15 Jun 2022 15:28:40 +0200 Subject: [PATCH 1/3] with HTTP protocol use headers from the HTTP Request when headers are not provided in the body --- src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt | 4 +++ .../kuzzle/sdk/events/MessageReceivedEvent.kt | 2 +- .../kotlin/io/kuzzle/sdk/protocol/Http.kt | 32 +++++++++---------- .../io/kuzzle/sdk/protocol/WebSocket.kt | 4 +-- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt index ecd15d75..fb795709 100644 --- a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt +++ b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt @@ -80,6 +80,10 @@ open class Kuzzle { return } + if (! jsonObject.containsKey("headers") && event.headers != null) { + jsonObject = jsonObject.plus("headers" to event.headers) + } + // If the message is empty, we take the requestId of the event, // to avoid error in fromMap function. if (! jsonObject.containsKey("requestId") && event.requestId != null) { diff --git a/src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt b/src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt index 2aa23c42..3cdb1731 100644 --- a/src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt +++ b/src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt @@ -1,3 +1,3 @@ package io.kuzzle.sdk.events -data class MessageReceivedEvent(var message: String?, var requestId: String?) +data class MessageReceivedEvent(var message: String?, var requestId: String? = null, var headers: Map>? = null) diff --git a/src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt b/src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt index e4156b7f..2bc4ffb7 100644 --- a/src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt +++ b/src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt @@ -5,6 +5,7 @@ import io.ktor.client.call.* import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* +import io.ktor.util.* import io.kuzzle.sdk.coreClasses.exceptions.* import io.kuzzle.sdk.coreClasses.http.Route import io.kuzzle.sdk.coreClasses.json.JsonSerializer @@ -222,7 +223,7 @@ open class Http : AbstractProtocol { this.body = JsonSerializer.serialize(payload) } // trigger messageReceived - super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?)) + super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?, response.headers.toMap())) } catch (e: Exception) { super.trigger(RequestErrorEvent(e, payload["requestId"] as String?)) } finally { @@ -253,7 +254,7 @@ open class Http : AbstractProtocol { this.body = if (requestInfo.body != null) JsonSerializer.serialize(requestInfo.body) else "" } // trigger messageReceived - super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?)) + super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?, response.headers.toMap())) } catch (e: Exception) { super.trigger(RequestErrorEvent(e, payload["requestId"] as String?)) } finally { @@ -303,20 +304,19 @@ open class Http : AbstractProtocol { val route = this.routes["$controller:$action"] - if (route == null) { - super.trigger(RequestErrorEvent(URLNotFoundException(controller, action), payload["requestId"] as String?)) - return - } - - try { - val requestInfo = route.buildRequest(KuzzleMap.from(payload)) - queryHTTPEndpoint(payload, requestInfo) - } catch (e: MissingURLParamException) { - /** - * Fallback if we could not find a matching route with the given parameters - * Try to make the request using directly using /_query endpoint - */ - query(payload) + if (route != null) { + try { + val requestInfo = route.buildRequest(KuzzleMap.from(payload)) + queryHTTPEndpoint(payload, requestInfo) + return + } catch (e: MissingURLParamException) { + // Fallback to query + } } + /** + * Fallback if we could not find a matching route with the given parameters + * Try to make the request using directly using /_query endpoint + */ + query(payload) } } diff --git a/src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt b/src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt index 3b543fc9..ca82baab 100644 --- a/src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt +++ b/src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt @@ -130,9 +130,9 @@ open class WebSocket : AbstractProtocol { for (frame in incoming) { when (frame) { // @TODO Create enums for events - is Frame.Text -> super.trigger(MessageReceivedEvent(frame.readText(), null)) + is Frame.Text -> super.trigger(MessageReceivedEvent(frame.readText())) // @TODO Create enums for events - is Frame.Binary -> super.trigger(MessageReceivedEvent(frame.readBytes().toString(), null)) + is Frame.Binary -> super.trigger(MessageReceivedEvent(frame.readBytes().toString())) is Frame.Close -> TODO() is Frame.Ping -> TODO() is Frame.Pong -> TODO() From b4fd468abeaaa1f790d24066741683d46677bd5d Mon Sep 17 00:00:00 2001 From: Shiranuit Date: Wed, 15 Jun 2022 16:20:55 +0200 Subject: [PATCH 2/3] Release 1.3.2 --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8cb8e220..84a2baa5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -35,7 +35,7 @@ val pomDeveloperId = "kuzzleio" val pomDeveloperName = "kuzzle" group = "io.kuzzle.sdk" -version = "1.3.1" +version = "1.3.2" val ktorVersion = "1.6.8" repositories { From 9b6dbcae610f04169a3e8cbee31daa75266a7d0e Mon Sep 17 00:00:00 2001 From: Shiranuit Date: Wed, 15 Jun 2022 16:21:55 +0200 Subject: [PATCH 3/3] bump version --- build.gradle.kts | 8 ++++---- doc/1/getting-started/java/index.md | 6 +++--- doc/1/getting-started/kotlin/index.md | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 84a2baa5..adb30932 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -16,7 +16,7 @@ plugins { val artifactName = "sdk-jvm" val artifactGroup = "io.kuzzle" -val artifactVersion = "1.3.1" +val artifactVersion = "1.3.2" val pomUrl = "https://github.com/kuzzleio/sdk-jvm" val pomScmUrl = "https://github.com/kuzzleio/sdk-jvm" @@ -61,11 +61,11 @@ dependencies { testImplementation("org.jetbrains.kotlin:kotlin-test-junit") testImplementation("io.mockk:mockk:1.8.13") - testImplementation("io.ktor:ktor-client-mock:1.3.1") + testImplementation("io.ktor:ktor-client-mock:1.3.2") testImplementation("io.ktor:ktor-client-mock-jvm:$ktorVersion") testImplementation("io.ktor:ktor-client-json-jvm:$ktorVersion") - testImplementation("io.ktor:ktor-client-mock-js:1.3.1") - testImplementation("io.ktor:ktor-client-mock-native:1.3.1") + testImplementation("io.ktor:ktor-client-mock-js:1.3.2") + testImplementation("io.ktor:ktor-client-mock-native:1.3.2") testImplementation("org.mock-server:mockserver-netty:5.3.0") testImplementation("io.cucumber:cucumber-java8:7.0.0") testImplementation("io.cucumber:cucumber-junit:7.0.0") diff --git a/doc/1/getting-started/java/index.md b/doc/1/getting-started/java/index.md index d09928e4..1a921f94 100644 --- a/doc/1/getting-started/java/index.md +++ b/doc/1/getting-started/java/index.md @@ -61,7 +61,7 @@ To build the project, add the following lines: io.kuzzle sdk-jvm - 1.3.1 + 1.3.2 pom ``` @@ -70,14 +70,14 @@ To build the project, add the following lines: ```groovy dependencies { - compile 'io.kuzzle:sdk-jvm:1.3.1' + compile 'io.kuzzle:sdk-jvm:1.3.2' } ``` ### Ivy ```html - + ``` diff --git a/doc/1/getting-started/kotlin/index.md b/doc/1/getting-started/kotlin/index.md index 7e3f9aa9..23da68e6 100644 --- a/doc/1/getting-started/kotlin/index.md +++ b/doc/1/getting-started/kotlin/index.md @@ -73,7 +73,7 @@ To build the project, add the following lines: io.kuzzle sdk-jvm - 1.3.1 + 1.3.2 pom ``` @@ -82,14 +82,14 @@ To build the project, add the following lines: ```groovy dependencies { - compile 'io.kuzzle:sdk-jvm:1.3.1' + compile 'io.kuzzle:sdk-jvm:1.3.2' } ``` ### Ivy ```html - + ```