Implementing Reverse proxy using Netty
Basically forward proxy server uses to gather resources from different servers. Client is being connected to the server via forward proxy. But reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client showing that they are being originated from the proxy server itself. While a forward proxy acts as an intermediary for its associated clients to contact any server, a reverse proxy acts as an intermediary for its associated servers to be contacted by any client.
Implementation was done just using a new framework called Netty. Java IO was using streams and when it comes to NIO it was totally about channels and buffers. But it was really helpful as they provides notifications to the developer about the status of the request.
Message flow
Established two channels between client to proxy and proxy to server. It registered as a channel pipeline and added two handlers called Source Handler and Target Handler respectively. Following shows the class diagram of implementation.
Class diagram of ReverseProxy implementation
Step 1: Initialize proxy and create inChannel
Step 2: Send a HTTP request. (Eg. SoapUI)
Step 3: Create source handler initializer and activate channel active of source Handler. Create host client with host, port and target Handler instance.
Step 4: Create client Bootstrap inside target Handler and initialize it. Invoke channelActive method. Then invoke channel read method of Source Handler. Then request starts to flow as both connections are active.
Step 5: channel read method of source handler hand over the request to TargetHandler. Then target Handler gather data from particular server and reads it back to the outbound channel. Once the channel inactive is called response is read back to the client using closeOnFlush (Channel channel) method.
Originally published at chamilelladeniya.wordpress.com on February 22, 2016.