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