io.axoniq.console.framework.client.RSocketHandlerRegistrar.kt Maven / Gradle / Ivy
/*
* Copyright (c) 2022-2024. AxonIQ B.V.
*
* 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 io.axoniq.console.framework.client
import io.axoniq.console.framework.client.strategy.RSocketPayloadEncodingStrategy
import io.rsocket.Payload
import io.rsocket.RSocket
import io.rsocket.metadata.CompositeMetadata
import io.rsocket.metadata.RoutingMetadata
import io.rsocket.metadata.WellKnownMimeType
import org.slf4j.LoggerFactory
import reactor.core.publisher.Mono
class RSocketHandlerRegistrar(
private val encodingStrategy: RSocketPayloadEncodingStrategy
) : RSocket {
private val logger = LoggerFactory.getLogger(this::class.java)
private val handlers: MutableList = mutableListOf()
fun registerHandlerWithoutPayload(route: String, handler: () -> Any) {
logger.debug("Registered AxonIQ Console handler for route {}", route)
handlers.add(PayloadlessRegisteredRsocketMessageHandler(route, handler))
}
fun registerHandlerWithPayload(route: String, payloadType: Class, handler: (T) -> Any) {
logger.debug("Registered AxonIQ Console handler for route {}", route)
handlers.add(PayloadRegisteredRsocketMessageHandler(route, payloadType, handler))
}
fun createRespondingRSocketFor(rSocket: RSocket) = object : RSocket {
override fun requestResponse(payload: Payload): Mono {
val route = routeFromPayload(payload)
if (route == "authentication_failed") {
logger.warn("Authentication to AxonIQ Console failed. Are your properties set correctly?")
rSocket.dispose()
return Mono.empty()
}
val matchingHandler = handlers.firstOrNull { it.route == route }
?: throw IllegalArgumentException("No handler registered for route $route")
val result = when (matchingHandler) {
is PayloadlessRegisteredRsocketMessageHandler ->
handleMessageWithoutPayload(matchingHandler, route)
is PayloadRegisteredRsocketMessageHandler<*> -> {
handleMessageWithPayload(matchingHandler, payload, route)
}
else -> throw IllegalArgumentException("Unknown handler type - should not happen!")
}
return Mono.just(result).map { encodingStrategy.encode(it) }
}
}
private fun handleMessageWithoutPayload(
matchingHandler: PayloadlessRegisteredRsocketMessageHandler,
route: String,
): Any {
logger.debug("Received AxonIQ Console message for route [$route]")
return matchingHandler.handler.invoke()
}
private fun handleMessageWithPayload(
matchingHandler: PayloadRegisteredRsocketMessageHandler,
payload: Payload,
route: String,
): Any {
val decodedPayload = encodingStrategy.decode(payload, matchingHandler.payloadType)
logger.debug("Received AxonIQ Console message for route [$route] with payload: [{}]", decodedPayload)
return matchingHandler.handler.invoke(decodedPayload)
}
private fun routeFromPayload(payload: Payload): String {
val compositeMetadata = CompositeMetadata(payload.metadata(), false)
val routeMetadata =
compositeMetadata.firstOrNull { it.mimeType == WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.string }
?: throw IllegalArgumentException("Request contained no route metadata!")
return RoutingMetadata(routeMetadata.content).iterator().next()
?: throw IllegalArgumentException("Request contained no route metadata!")
}
private interface RegisteredRsocketMessageHandler {
val route: String
}
private data class PayloadRegisteredRsocketMessageHandler(
override val route: String,
val payloadType: Class,
val handler: (T) -> Any
) : RegisteredRsocketMessageHandler
private data class PayloadlessRegisteredRsocketMessageHandler(
override val route: String,
val handler: () -> Any
) : RegisteredRsocketMessageHandler
}