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());
}
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()));
}
}
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();
}
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);
}
}
Aggregations