Building an active(smart) proxy

235 Views Asked by At

After a deep search on the internet I found Smiley's HTTP Proxy Servlet decent to handle proxy request. It is small and composed of a single class. My question is, can I use this proxy as an active proxy, ie. When before fetching the target host, I need to run a piece of code to compute few values. If it is not possible with this servlet, is their any other solution to the problem?

1

There are 1 best solutions below

0
On

Smiley's HTTP Proxy Servlet is described as being easily extendible, because you simply need to extend the class and override the methods you want to modify.

It seems that this proxy can work for your situation, but it depends a little more on your use case. You want to run some code before retrieving the target host, which seems to mean that #1 your target host would be modified based on your custom code, or #2 you simply want a hook to run your custom code before the target host is retrieved. Below are my answers for each:

1) If you want to modify the target host (e.g. targetUriObj as referenced in the ProxyServlet.java, line 204 of the service() method), you would really need to download the source and modify it directly for your needs. You can either edit the original source, or override the service() method and change what you need.

2) If you simply want your code to run, I would recommend creating an extension of ProxyServlet and override the service() method, like so:

public class ProxyServletExtension extends ProxyServlet {
    @Override
    protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
        // Run your custom code here

        // Call the parent service() method to finish processing
        super.service(servletRequest, servletResponse);
    }
}