Search in sources :

Example 16 with RSocketCompositeMetadata

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

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