Search in sources :

Example 11 with RSocketCompositeMetadata

use of com.alibaba.rsocket.metadata.RSocketCompositeMetadata in project alibaba-rsocket-broker by alibaba.

the class ServiceTestingView method callRSocketService.

public void callRSocketService(String service, String method, @Nullable String jsonData, Pre response) {
    Integer handlerId = this.routingSelector.findHandler(ServiceLocator.serviceHashCode(service));
    if (handlerId != null) {
        RSocketBrokerResponderHandler handler = handlerRegistry.findById(handlerId);
        if (handler != null) {
            // composite metadata for health check
            RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(new GSVRoutingMetadata(null, service, method, null), new MessageMimeTypeMetadata(RSocketMimeType.Json));
            ByteBuf payLoadData;
            if (jsonData == null || jsonData.isEmpty()) {
                payLoadData = Unpooled.EMPTY_BUFFER;
            } else {
                payLoadData = Unpooled.wrappedBuffer(jsonData.getBytes(StandardCharsets.UTF_8));
            }
            Payload requestPayload = ByteBufPayload.create(payLoadData, compositeMetadata.getContent());
            handler.getPeerRsocket().requestResponse(requestPayload).doOnError(throwable -> getUI().ifPresent(ui -> ui.access(() -> {
                response.setText(throwable.getMessage());
            }))).subscribe(payload -> getUI().ifPresent(ui -> ui.access(() -> {
                response.setText(payload.getDataUtf8());
            })));
        } else {
            this.serviceNameFiled.setInvalid(true);
            this.serviceNameFiled.setErrorMessage("No Service Provider!");
        }
    } else {
        this.serviceNameFiled.setInvalid(true);
        this.serviceNameFiled.setErrorMessage("Service not found!");
    }
}
Also used : RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) TextArea(com.vaadin.flow.component.textfield.TextArea) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Autowired(org.springframework.beans.factory.annotation.Autowired) RSocketBrokerHandlerRegistry(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerHandlerRegistry) Route(com.vaadin.flow.router.Route) Unpooled(io.netty.buffer.Unpooled) MessageMimeTypeMetadata(com.alibaba.rsocket.metadata.MessageMimeTypeMetadata) ByteBuf(io.netty.buffer.ByteBuf) RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) TextField(com.vaadin.flow.component.textfield.TextField) ByteBufPayload(io.rsocket.util.ByteBufPayload) RSocketMimeType(com.alibaba.rsocket.metadata.RSocketMimeType) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) H3(com.vaadin.flow.component.html.H3) ServiceRoutingSelector(com.alibaba.spring.boot.rsocket.broker.route.ServiceRoutingSelector) H4(com.vaadin.flow.component.html.H4) RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) StandardCharsets(java.nio.charset.StandardCharsets) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) Nullable(org.jetbrains.annotations.Nullable) Button(com.vaadin.flow.component.button.Button) Payload(io.rsocket.Payload) NAV(com.alibaba.rsocket.broker.web.ui.ServiceTestingView.NAV) Pre(com.vaadin.flow.component.html.Pre) ServiceLocator(com.alibaba.rsocket.ServiceLocator) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) MessageMimeTypeMetadata(com.alibaba.rsocket.metadata.MessageMimeTypeMetadata) ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) ByteBuf(io.netty.buffer.ByteBuf)

Example 12 with RSocketCompositeMetadata

use of com.alibaba.rsocket.metadata.RSocketCompositeMetadata in project alibaba-rsocket-broker by alibaba.

the class ServiceQueryController method queryDefinition.

@GetMapping(value = "/definition/{serviceName}")
public Mono<String> queryDefinition(@PathVariable(name = "serviceName") String serviceName) {
    Integer handler = routingSelector.findHandler(new ServiceLocator("", serviceName, "").getId());
    if (handler != null) {
        RSocketBrokerResponderHandler brokerResponderHandler = brokerHandlerRegistry.findById(handler);
        if (brokerResponderHandler != null) {
            GSVRoutingMetadata routingMetadata = new GSVRoutingMetadata("", ReactiveServiceDiscovery.class.getCanonicalName() + ".findServiceByFullName", "");
            RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(routingMetadata, jsonMetaEncoding);
            ByteBuf bodyBuf = Unpooled.wrappedBuffer(("[\"" + serviceName + "\"]").getBytes(StandardCharsets.UTF_8));
            return brokerResponderHandler.getPeerRsocket().requestResponse(ByteBufPayload.create(bodyBuf, compositeMetadata.getContent())).map(Payload::getDataUtf8);
        }
    }
    return Mono.error(new Exception(RsocketErrorCode.message("RST-900404", serviceName)));
}
Also used : ServiceLocator(com.alibaba.rsocket.ServiceLocator) RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) ByteBufPayload(io.rsocket.util.ByteBufPayload) ByteBuf(io.netty.buffer.ByteBuf) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 13 with RSocketCompositeMetadata

use of com.alibaba.rsocket.metadata.RSocketCompositeMetadata in project alibaba-rsocket-broker by alibaba.

the class CloudEventRSocket method extractCloudEventsFromMetadataPush.

@Nullable
default CloudEventImpl<JsonNode> extractCloudEventsFromMetadataPush(@NotNull Payload payload) {
    String jsonText = null;
    byte firstByte = payload.metadata().getByte(0);
    // json text: well known type > 127, and normal mime type's length < 127
    if (firstByte == '{') {
        jsonText = payload.getMetadataUtf8();
    } else {
        // composite metadata
        RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
        if (compositeMetadata.contains(RSocketMimeType.CloudEventsJson)) {
            jsonText = compositeMetadata.getMetadata(RSocketMimeType.CloudEventsJson).toString(StandardCharsets.UTF_8);
        }
    }
    if (jsonText != null) {
        return Json.decodeValue(jsonText);
    }
    return null;
}
Also used : RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with RSocketCompositeMetadata

use of com.alibaba.rsocket.metadata.RSocketCompositeMetadata in project alibaba-rsocket-broker by alibaba.

the class CloudEventRSocket method constructEventReplyPayload.

default Payload constructEventReplyPayload(URI replyTo, EventReply eventReply) {
    String path = replyTo.getPath();
    String serviceName = path.substring(path.lastIndexOf("/") + 1);
    String method = replyTo.getFragment();
    RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(new GSVRoutingMetadata("", serviceName, method, ""), new MessageMimeTypeMetadata(WellKnownMimeType.APPLICATION_JSON));
    return ByteBufPayload.create(JsonUtils.toJsonByteBuf(eventReply), compositeMetadata.getContent());
}
Also used : RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) MessageMimeTypeMetadata(com.alibaba.rsocket.metadata.MessageMimeTypeMetadata)

Example 15 with RSocketCompositeMetadata

use of com.alibaba.rsocket.metadata.RSocketCompositeMetadata in project alibaba-rsocket-broker by alibaba.

the class RSocketRestApiController method handle.

@RequestMapping(value = "/{serviceName}/{method}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Mono<ResponseEntity<String>> handle(@PathVariable("serviceName") String serviceName, @PathVariable("method") String method, @RequestParam(name = "group", required = false, defaultValue = "") String group, @RequestParam(name = "version", required = false, defaultValue = "") String version, @RequestBody(required = false) byte[] body, @RequestHeader(name = "X-Endpoint", required = false, defaultValue = "") String endpoint, @RequestHeader(name = "Authorization", required = false, defaultValue = "") String authorizationValue) {
    try {
        GSVRoutingMetadata routingMetadata = new GSVRoutingMetadata(group, serviceName, method, version);
        Integer serviceHashCode = routingMetadata.id();
        Integer targetHandlerId = routingSelector.findHandler(serviceHashCode);
        if (!endpoint.isEmpty() && endpoint.startsWith("id:")) {
            targetHandlerId = Integer.valueOf(endpoint.substring(3).trim());
        }
        return Optional.ofNullable(targetHandlerId).flatMap(handlerId -> Optional.ofNullable(handlerRegistry.findById(handlerId))).map(targetHandler -> {
            if (authRequired) {
                RSocketAppPrincipal principal = authAuthorizationValue(authorizationValue);
                if (principal == null || !serviceMeshInspector.isRequestAllowed(principal, routingMetadata.gsv(), targetHandler.getPrincipal())) {
                    return Mono.just(error(RsocketErrorCode.message("RST-900401", routingMetadata.gsv())));
                }
            }
            RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(routingMetadata, jsonMetaEncoding);
            ByteBuf bodyBuf = body == null ? EMPTY_BUFFER : Unpooled.wrappedBuffer(body);
            return targetHandler.requestResponse(DefaultPayload.create(bodyBuf, compositeMetadata.getContent())).map(payload -> {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                headers.setCacheControl(CacheControl.noCache().getHeaderValue());
                return new ResponseEntity<>(payload.getDataUtf8(), headers, HttpStatus.OK);
            });
        }).orElseGet(() -> Mono.just(error(RsocketErrorCode.message("RST-900404", routingMetadata.gsv()))));
    } catch (Exception e) {
        return Mono.just(error(e.getMessage()));
    }
}
Also used : RSocketMimeType(com.alibaba.rsocket.metadata.RSocketMimeType) EMPTY_BUFFER(io.netty.buffer.Unpooled.EMPTY_BUFFER) org.springframework.http(org.springframework.http) RsocketErrorCode(com.alibaba.rsocket.observability.RsocketErrorCode) Autowired(org.springframework.beans.factory.annotation.Autowired) Mono(reactor.core.publisher.Mono) ServiceRoutingSelector(com.alibaba.spring.boot.rsocket.broker.route.ServiceRoutingSelector) RSocketAppPrincipal(com.alibaba.spring.boot.rsocket.broker.security.RSocketAppPrincipal) RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) RSocketBrokerHandlerRegistry(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerHandlerRegistry) Unpooled(io.netty.buffer.Unpooled) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) Value(org.springframework.beans.factory.annotation.Value) Nullable(org.jetbrains.annotations.Nullable) MessageMimeTypeMetadata(com.alibaba.rsocket.metadata.MessageMimeTypeMetadata) ByteBuf(io.netty.buffer.ByteBuf) AuthenticationService(com.alibaba.spring.boot.rsocket.broker.security.AuthenticationService) DefaultPayload(io.rsocket.util.DefaultPayload) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ServiceMeshInspector(com.alibaba.spring.boot.rsocket.broker.route.ServiceMeshInspector) RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) ByteBuf(io.netty.buffer.ByteBuf) RSocketAppPrincipal(com.alibaba.spring.boot.rsocket.broker.security.RSocketAppPrincipal)

Aggregations

RSocketCompositeMetadata (com.alibaba.rsocket.metadata.RSocketCompositeMetadata)16 GSVRoutingMetadata (com.alibaba.rsocket.metadata.GSVRoutingMetadata)13 MessageMimeTypeMetadata (com.alibaba.rsocket.metadata.MessageMimeTypeMetadata)7 ByteBuf (io.netty.buffer.ByteBuf)6 NotNull (org.jetbrains.annotations.NotNull)5 AbstractRSocket (com.alibaba.rsocket.AbstractRSocket)4 BinaryRoutingMetadata (com.alibaba.rsocket.metadata.BinaryRoutingMetadata)4 RSocket (io.rsocket.RSocket)4 InvalidException (io.rsocket.exceptions.InvalidException)4 Nullable (org.jetbrains.annotations.Nullable)4 RSocketExchange (com.alibaba.rsocket.RSocketExchange)3 ServiceLocator (com.alibaba.rsocket.ServiceLocator)3 RSocketMimeType (com.alibaba.rsocket.metadata.RSocketMimeType)3 Unpooled (io.netty.buffer.Unpooled)3 Payload (io.rsocket.Payload)3 ByteBufPayload (io.rsocket.util.ByteBufPayload)3 RsocketErrorCode (com.alibaba.rsocket.observability.RsocketErrorCode)2 RSocketBrokerHandlerRegistry (com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerHandlerRegistry)2 RSocketBrokerResponderHandler (com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler)2 ServiceRoutingSelector (com.alibaba.spring.boot.rsocket.broker.route.ServiceRoutingSelector)2