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