Coverage Summary for Class: JsonRpcWeb3ServerHandler (co.rsk.rpc.netty)

Class Class, % Method, % Line, %
JsonRpcWeb3ServerHandler 0% (0/1) 0% (0/5) 0% (0/26)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2018 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.rpc.netty; 20  21 import co.rsk.rpc.JsonRpcMethodFilter; 22 import co.rsk.rpc.ModuleDescription; 23 import com.fasterxml.jackson.core.JsonProcessingException; 24 import com.fasterxml.jackson.databind.JsonNode; 25 import com.fasterxml.jackson.databind.ObjectMapper; 26 import com.fasterxml.jackson.databind.node.JsonNodeFactory; 27 import com.googlecode.jsonrpc4j.*; 28 import io.netty.buffer.*; 29 import io.netty.channel.ChannelHandler; 30 import io.netty.channel.ChannelHandlerContext; 31 import io.netty.channel.SimpleChannelInboundHandler; 32 import org.ethereum.rpc.Web3; 33 import org.ethereum.rpc.exception.RskErrorResolver; 34 import org.slf4j.Logger; 35 import org.slf4j.LoggerFactory; 36  37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.Map; 40  41 @ChannelHandler.Sharable 42 public class JsonRpcWeb3ServerHandler extends SimpleChannelInboundHandler<ByteBufHolder> { 43  44  private static final Logger LOGGER = LoggerFactory.getLogger("jsonrpc"); 45  46  private final ObjectMapper mapper = new ObjectMapper(); 47  private final JsonNodeFactory jsonNodeFactory = JsonNodeFactory.instance; 48  private final JsonRpcBasicServer jsonRpcServer; 49  50  public JsonRpcWeb3ServerHandler(Web3 service, List<ModuleDescription> filteredModules) { 51  this.jsonRpcServer = new JsonRpcBasicServer(service, service.getClass()); 52  jsonRpcServer.setRequestInterceptor(new JsonRpcMethodFilter(filteredModules)); 53  jsonRpcServer.setErrorResolver(new MultipleErrorResolver(new RskErrorResolver(), AnnotationsErrorResolver.INSTANCE, DefaultErrorResolver.INSTANCE)); 54  } 55  56  @Override 57  protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder request) throws Exception { 58  ByteBuf responseContent = Unpooled.buffer(); 59  int responseCode; 60  try (ByteBufOutputStream os = new ByteBufOutputStream(responseContent); 61  ByteBufInputStream is = new ByteBufInputStream(request.content().retain())){ 62  63  responseCode = jsonRpcServer.handleRequest(is, os); 64  } catch (Exception e) { 65  String unexpectedErrorMsg = "Unexpected error"; 66  LOGGER.error(unexpectedErrorMsg, e); 67  int errorCode = ErrorResolver.JsonError.CUSTOM_SERVER_ERROR_LOWER; 68  responseContent = buildErrorContent(errorCode, unexpectedErrorMsg); 69  responseCode = errorCode; 70  } 71  72  ctx.fireChannelRead(new Web3Result( 73  responseContent, 74  responseCode 75  )); 76  } 77  78  @Override 79  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 80  LOGGER.error("Unexpected exception", cause); 81  ctx.close(); 82  } 83  84  private ByteBuf buildErrorContent(int errorCode, String errorMessage) throws JsonProcessingException { 85  Map<String, JsonNode> errorProperties = new HashMap<>(); 86  errorProperties.put("code", jsonNodeFactory.numberNode(errorCode)); 87  errorProperties.put("message", jsonNodeFactory.textNode(errorMessage)); 88  JsonNode error = jsonNodeFactory.objectNode().set("error", jsonNodeFactory.objectNode().setAll(errorProperties)); 89  return Unpooled.wrappedBuffer(mapper.writeValueAsBytes(mapper.treeToValue(error, Object.class))); 90  } 91 }