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

Class Method, % Line, %
Web3HttpServer 0% (0/5) 0% (0/27)
Web3HttpServer$1 0% (0/2) 0% (0/16)
Total 0% (0/7) 0% (0/43)


1 package co.rsk.rpc.netty; 2  3 import co.rsk.config.InternalService; 4 import co.rsk.rpc.CorsConfiguration; 5 import io.netty.bootstrap.ServerBootstrap; 6 import io.netty.channel.ChannelInitializer; 7 import io.netty.channel.ChannelOption; 8 import io.netty.channel.ChannelPipeline; 9 import io.netty.channel.EventLoopGroup; 10 import io.netty.channel.nio.NioEventLoopGroup; 11 import io.netty.channel.socket.SocketChannel; 12 import io.netty.channel.socket.nio.NioServerSocketChannel; 13 import io.netty.handler.codec.http.*; 14 import io.netty.handler.codec.http.cors.CorsConfig; 15 import io.netty.handler.codec.http.cors.CorsHandler; 16 import io.netty.handler.logging.LogLevel; 17 import io.netty.handler.logging.LoggingHandler; 18 import org.slf4j.Logger; 19 import org.slf4j.LoggerFactory; 20  21 import java.net.InetAddress; 22  23 public class Web3HttpServer implements InternalService { 24  private static final Logger logger = LoggerFactory.getLogger(Web3HttpServer.class); 25  26  private final InetAddress bindAddress; 27  private final int port; 28  private final EventLoopGroup bossGroup; 29  private final EventLoopGroup workerGroup; 30  private final int socketLinger; 31  private final boolean reuseAddress; 32  private final CorsConfiguration corsConfiguration; 33  private final JsonRpcWeb3FilterHandler jsonRpcWeb3FilterHandler; 34  private final JsonRpcWeb3ServerHandler jsonRpcWeb3ServerHandler; 35  36  public Web3HttpServer(InetAddress bindAddress, 37  int port, 38  int socketLinger, 39  boolean reuseAddress, 40  CorsConfiguration corsConfiguration, 41  JsonRpcWeb3FilterHandler jsonRpcWeb3FilterHandler, 42  JsonRpcWeb3ServerHandler jsonRpcWeb3ServerHandler) { 43  this.bindAddress = bindAddress; 44  this.port = port; 45  this.socketLinger = socketLinger; 46  this.reuseAddress = reuseAddress; 47  this.corsConfiguration = corsConfiguration; 48  this.jsonRpcWeb3FilterHandler = jsonRpcWeb3FilterHandler; 49  this.jsonRpcWeb3ServerHandler = jsonRpcWeb3ServerHandler; 50  this.bossGroup = new NioEventLoopGroup(); 51  this.workerGroup = new NioEventLoopGroup(); 52  } 53  54  @Override 55  public void start() { 56  logger.info("RPC HTTP enabled"); 57  58  ServerBootstrap b = new ServerBootstrap(); 59  b.option(ChannelOption.SO_LINGER, socketLinger); 60  b.option(ChannelOption.SO_REUSEADDR, reuseAddress); 61  b.group(bossGroup, workerGroup) 62  .channel(NioServerSocketChannel.class) 63  .handler(new LoggingHandler(LogLevel.INFO)) 64  .childHandler(new ChannelInitializer<SocketChannel>() { 65  @Override 66  protected void initChannel(SocketChannel ch) throws Exception { 67  ChannelPipeline p = ch.pipeline(); 68  p.addLast(new HttpRequestDecoder()); 69  p.addLast(new HttpResponseEncoder()); 70  p.addLast(new HttpObjectAggregator(1024 * 1024 * 5)); 71  p.addLast(new HttpContentCompressor()); 72  if (corsConfiguration.hasHeader()) { 73  p.addLast(new CorsHandler( 74  CorsConfig 75  .withOrigin(corsConfiguration.getHeader()) 76  .allowedRequestHeaders(HttpHeaders.Names.CONTENT_TYPE) 77  .allowedRequestMethods(HttpMethod.POST) 78  .build()) 79  ); 80  } 81  p.addLast(jsonRpcWeb3FilterHandler); 82  p.addLast(new Web3HttpMethodFilterHandler()); 83  p.addLast(jsonRpcWeb3ServerHandler); 84  p.addLast(new Web3ResultHttpResponseHandler()); 85  } 86  }); 87  try { 88  b.bind(bindAddress, port).sync(); 89  } catch (InterruptedException e) { 90  logger.error("The RPC HTTP server couldn't be started", e); 91  Thread.currentThread().interrupt(); 92  } 93  } 94  95  @Override 96  public void stop() { 97  bossGroup.shutdownGracefully(); 98  workerGroup.shutdownGracefully(); 99  } 100 }