Search in sources :

Example 1 with MessageAcceptMimeTypesMetadata

use of org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata in project kin-rsocket-broker by huangjianqin.

the class RSocketResponderHandlerSupport method localRequestChannel.

/**
 * 本地调用服务接口方法并针对RequestChannel Frame Type场景定制额外逻辑
 */
@SuppressWarnings("ReactiveStreamsNullableInLambdaInTransform")
protected Flux<Payload> localRequestChannel(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload signal, Flux<Payload> payloads) {
    String service = routing.getService();
    String handler = routing.getHandler();
    try {
        ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
        if (methodInvoker != null) {
            Object result;
            if (methodInvoker.getParamCount() == 1) {
                Flux<Object> paramFlux = payloads.map(payload -> Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), payload.data(), methodInvoker.getInferredClassForParameter(0)));
                result = methodInvoker.invoke(paramFlux);
            } else {
                Object paramFirst = Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), signal.data(), methodInvoker.getParameterTypes()[0]);
                Flux<Object> paramFlux = payloads.map(payload -> Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), payload.data(), methodInvoker.getInferredClassForParameter(1)));
                result = methodInvoker.invoke(paramFirst, paramFlux);
            }
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
            // result return
            return ReactiveObjAdapter.INSTANCE.toFlux(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
        } else {
            return Flux.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
        }
    } catch (Exception e) {
        error(failCallLog(service, handler), e);
        // release
        ReferenceCountUtil.safeRelease(signal);
        payloads.subscribe(ReferenceCountUtil::safeRelease);
        return Flux.error(new InvalidException(failCallTips(service, handler, e)));
    }
}
Also used : MessageAcceptMimeTypesMetadata(org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata) Flux(reactor.core.publisher.Flux) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) MessageMimeTypeMetadata(org.kin.rsocket.core.metadata.MessageMimeTypeMetadata) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LoggerOprs(org.kin.framework.log.LoggerOprs) Codecs(org.kin.rsocket.core.codec.Codecs) Mono(reactor.core.publisher.Mono) ExceptionUtils(org.kin.framework.utils.ExceptionUtils) InvalidException(io.rsocket.exceptions.InvalidException) ByteBufPayload(io.rsocket.util.ByteBufPayload) InvalidException(io.rsocket.exceptions.InvalidException) InvalidException(io.rsocket.exceptions.InvalidException)

Example 2 with MessageAcceptMimeTypesMetadata

use of org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata in project kin-rsocket-broker by huangjianqin.

the class RSocketResponderHandlerSupport method localRequestResponse.

/**
 * 本地调用服务接口方法并针对RequestResponse Frame Type场景定制额外逻辑
 */
protected Mono<Payload> localRequestResponse(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload payload) {
    String service = routing.getService();
    String handler = routing.getHandler();
    try {
        ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
        if (methodInvoker != null) {
            Object result;
            if (methodInvoker.isAsyncReturn()) {
                result = invokeServiceMethod(methodInvoker, dataEncodingMetadata, payload);
            } else {
                result = Mono.create((sink) -> {
                    try {
                        Object resultObj = invokeServiceMethod(methodInvoker, dataEncodingMetadata, payload);
                        if (resultObj == null) {
                            sink.success();
                        } else if (resultObj instanceof Mono) {
                            Mono<Object> monoObj = (Mono<Object>) resultObj;
                            monoObj.doOnError(sink::error).doOnNext(sink::success).thenEmpty(Mono.fromRunnable(sink::success)).subscribe();
                        } else {
                            sink.success(resultObj);
                        }
                    } catch (Exception e) {
                        error(failCallLog(service, handler), e);
                        sink.error(e);
                    }
                });
            }
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
            return ReactiveObjAdapter.INSTANCE.toMono(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
        } else {
            ReferenceCountUtil.safeRelease(payload);
            return Mono.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
        }
    } catch (Exception e) {
        error(failCallLog(service, handler), e);
        ReferenceCountUtil.safeRelease(payload);
        return Mono.error(new InvalidException(failCallTips(service, handler, e)));
    }
}
Also used : MessageAcceptMimeTypesMetadata(org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata) Flux(reactor.core.publisher.Flux) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) MessageMimeTypeMetadata(org.kin.rsocket.core.metadata.MessageMimeTypeMetadata) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LoggerOprs(org.kin.framework.log.LoggerOprs) Codecs(org.kin.rsocket.core.codec.Codecs) Mono(reactor.core.publisher.Mono) ExceptionUtils(org.kin.framework.utils.ExceptionUtils) InvalidException(io.rsocket.exceptions.InvalidException) ByteBufPayload(io.rsocket.util.ByteBufPayload) Mono(reactor.core.publisher.Mono) InvalidException(io.rsocket.exceptions.InvalidException) InvalidException(io.rsocket.exceptions.InvalidException)

Example 3 with MessageAcceptMimeTypesMetadata

use of org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata in project kin-rsocket-broker by huangjianqin.

the class RSocketResponderHandlerSupport method localRequestStream.

/**
 * 本地调用服务接口方法并针对RequestStream Frame Type场景定制额外逻辑
 */
protected Flux<Payload> localRequestStream(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload payload) {
    String service = routing.getService();
    String handler = routing.getHandler();
    try {
        ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
        if (methodInvoker != null) {
            Object result = invokeServiceMethod(methodInvoker, dataEncodingMetadata, payload);
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
            return ReactiveObjAdapter.INSTANCE.toFlux(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
        } else {
            ReferenceCountUtil.safeRelease(payload);
            return Flux.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
        }
    } catch (Exception e) {
        error(failCallLog(service, handler), e);
        ReferenceCountUtil.safeRelease(payload);
        return Flux.error(new InvalidException(failCallTips(service, handler, e)));
    }
}
Also used : MessageAcceptMimeTypesMetadata(org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata) Flux(reactor.core.publisher.Flux) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) MessageMimeTypeMetadata(org.kin.rsocket.core.metadata.MessageMimeTypeMetadata) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LoggerOprs(org.kin.framework.log.LoggerOprs) Codecs(org.kin.rsocket.core.codec.Codecs) Mono(reactor.core.publisher.Mono) ExceptionUtils(org.kin.framework.utils.ExceptionUtils) InvalidException(io.rsocket.exceptions.InvalidException) ByteBufPayload(io.rsocket.util.ByteBufPayload) InvalidException(io.rsocket.exceptions.InvalidException) InvalidException(io.rsocket.exceptions.InvalidException)

Aggregations

ReferenceCountUtil (io.netty.util.ReferenceCountUtil)3 Payload (io.rsocket.Payload)3 InvalidException (io.rsocket.exceptions.InvalidException)3 ByteBufPayload (io.rsocket.util.ByteBufPayload)3 LoggerOprs (org.kin.framework.log.LoggerOprs)3 ExceptionUtils (org.kin.framework.utils.ExceptionUtils)3 Codecs (org.kin.rsocket.core.codec.Codecs)3 GSVRoutingMetadata (org.kin.rsocket.core.metadata.GSVRoutingMetadata)3 MessageAcceptMimeTypesMetadata (org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata)3 MessageMimeTypeMetadata (org.kin.rsocket.core.metadata.MessageMimeTypeMetadata)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3