use of co.rsk.jsonrpc.JsonRpcResultOrError in project rskj by rsksmart.
the class RskWebSocketJsonRpcHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) {
ByteBuf content = msg.copy().content();
try (ByteBufInputStream source = new ByteBufInputStream(content)) {
final JsonNode jsonNodeRequest = mapper.readTree(source);
RskWebSocketJsonParameterValidator.Result validationResult = parameterValidator.validate(jsonNodeRequest);
RskJsonRpcRequest request = mapper.treeToValue(jsonNodeRequest, RskJsonRpcRequest.class);
JsonRpcResultOrError resultOrError = null;
if (validationResult.isValid()) {
// TODO(mc) we should support the ModuleDescription method filters
resultOrError = request.accept(this, ctx);
} else {
resultOrError = new JsonRpcError(JsonRpcError.INVALID_PARAMS, validationResult.getMessage());
}
JsonRpcIdentifiableMessage response = resultOrError.responseFor(request.getId());
ctx.writeAndFlush(new TextWebSocketFrame(getJsonWithTypedId(jsonNodeRequest, response)));
return;
} catch (IOException e) {
LOGGER.trace("Not a known or valid JsonRpcRequest", e);
// We need to release this resource, netty only takes care about 'ByteBufHolder msg'
content.release(content.refCnt());
}
// delegate to the next handler if the message can't be matched to a known JSON-RPC request
ctx.fireChannelRead(msg);
}
Aggregations