Search in sources :

Example 11 with GSVRoutingMetadata

use of com.alibaba.rsocket.metadata.GSVRoutingMetadata 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 12 with GSVRoutingMetadata

use of com.alibaba.rsocket.metadata.GSVRoutingMetadata 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)

Example 13 with GSVRoutingMetadata

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

the class CanaryFilter method run.

@Override
public Mono<Void> run(RSocketExchange exchange) {
    if (roundRobinIndex % 100 < trafficRating) {
        GSVRoutingMetadata routingMetadata = exchange.getRoutingMetadata();
        routingMetadata.setVersion(canaryVersion);
    }
    roundRobinIndex = (roundRobinIndex + 1) % 100;
    return Mono.empty();
}
Also used : GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata)

Example 14 with GSVRoutingMetadata

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

the class MainController method handle.

@RequestMapping(value = "/api/{serviceName}/{method}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Mono<ResponseEntity<ByteBuf>> 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) ByteBuf body, @RequestHeader(name = "Authorization", required = false, defaultValue = "") String authorizationValue, @RequestHeader(name = "Content-Type", required = false, defaultValue = "") String contentType) {
    boolean authenticated;
    if (!authRequired) {
        authenticated = true;
    } else {
        authenticated = authAuthorizationValue(authorizationValue);
    }
    if (!authenticated) {
        return Mono.error(new Exception(RsocketErrorCode.message("RST-500403")));
    }
    try {
        GSVRoutingMetadata routingMetadata = new GSVRoutingMetadata(group, serviceName, method, version);
        RSocketCompositeMetadata compositeMetadata;
        if (contentType.startsWith("application/cloudevents+json")) {
            compositeMetadata = RSocketCompositeMetadata.from(routingMetadata, cloudEventsEncoding);
        } else {
            compositeMetadata = RSocketCompositeMetadata.from(routingMetadata, jsonMetaEncoding);
        }
        ByteBuf bodyBuf = body == null ? EMPTY_BUFFER : body;
        return rsocket.requestResponse(ByteBufPayload.create(bodyBuf, compositeMetadata.getContent())).map(payload -> {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setCacheControl(CacheControl.noCache().getHeaderValue());
            return new ResponseEntity<>(payload.data(), headers, HttpStatus.OK);
        });
    } catch (Exception e) {
        return Mono.error(e);
    }
}
Also used : RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

GSVRoutingMetadata (com.alibaba.rsocket.metadata.GSVRoutingMetadata)14 RSocketCompositeMetadata (com.alibaba.rsocket.metadata.RSocketCompositeMetadata)13 MessageMimeTypeMetadata (com.alibaba.rsocket.metadata.MessageMimeTypeMetadata)6 ByteBuf (io.netty.buffer.ByteBuf)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 RSocketExchange (com.alibaba.rsocket.RSocketExchange)3 NotNull (org.jetbrains.annotations.NotNull)3 ServiceLocator (com.alibaba.rsocket.ServiceLocator)2 RSocketMimeType (com.alibaba.rsocket.metadata.RSocketMimeType)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 Unpooled (io.netty.buffer.Unpooled)2 Payload (io.rsocket.Payload)2 ByteBufPayload (io.rsocket.util.ByteBufPayload)2 Nullable (org.jetbrains.annotations.Nullable)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2