Logging Proxy


Logging Proxy

Logging Proxy is a simple Proxy Servlet that forwards its request to one or more servers configured in the servlet's init parameters and optionally logs the request, response and headers as files. It has been tested on Tomcat 8.

It is not an HTTP proxy which can be configured in a browser. The caller has to use the servlet's URL explicitly.

It is intended for dev environments. Typical uses cases are to find out HTTP calls made in a SOAP invocation, fetch static resource from another domain for a local web application or proxy ajax calls. The latter 2 use cases can also be accomplished by setting up ProxyPass in Web Servers like Apache and nginx.


Features

Simple Proxying

Request Headers except Host, Request Body if any and Query String if present are passed to the destination server.

Received Response Headers except Transfer-Encoding, and response body is sent back.

The source is a single java file with no dependencies except servlet-api. You can easily modify the servlet to suit your needs.

Optional Logging

There is a sub directory created for each request based on the current time and a sequence if multiple requests arrive in the same second. Under the directory request.txt contains the POST data, response.txt contains the response body and info.txt contains the URL and headers.

Logging can be turned off by setting the servlet's init parameter logging-enabled to false


Usage

Download loggingproxy and extract the zip file containing the source and examples.

Example 1 without Logging

Copy the lp.war in the example1WithoutLogging directory to the webapp folder of your Tomcat Server and start Tomcat. This proxies to http://jambula.sourceforge.net/.

Open http://localhost:8080/lp/ in a browser. It should open the Jambula homepage.

Check web.xml under the expanded loggingproxy directory for the configuration used.

Stop Tomcat and delete the loggingproxy directory for the next example.

Example 2 with Logging and multiple destinations

Copy the lp.war in the example2WithLoggingAndMultipleDestinations directory to the webapp folder of your Tomcat Server and start Tomcat. This proxies to http://jambula.sourceforge.net/ for URLs starting with /jm and to http://aredis.sourceforge.net/ for URLs starting with /ar

Open http://localhost:8080/lp/jm/ in a browser. It should open the Jambula homepage.

Open http://localhost:8080/lp/ar/ in a browser. It should open the AREDIS homepage.

Check web.xml under the expanded loggingproxy directory for the configuration used.

You can see the logs under work/Catalina/localhost/lp/reqres under Tomcat installation directory. You won't find request.txt files because the requests are all GET requests without a POST body.

Deploying as ROOT webapp

You can also deploy loggingproxy Servlet as a ROOT webapplication so that you can access the proxy URL without the /lp prefix. To do this copy webapps/lp/WEB-INF to webapps/ROOT.

Startup dependency on proxy

If there is a startup dependency on a URL proxied by loggingproxy by another web application deployed on the same server, Tomcat will not start because of the cyclic dependency caused due to the Proxy Servlet itself waiting for Tomcat to come up.

To overcome this you can define a second host entry as shown in serverWithMultipleHosts.xml. Edit server.xml and similarly copy paste the Host section and add the attribute deployOnStartup="false" to the current host section only.

In the pasted host entry change the host name as org.loggingproxy. You can add the alias tags as shown for org.lp for additional host names. Also change the appBase to webappsLoggingProxy.

Next create webappsLoggingProxy directory defined in the new Host under Tomcat root and copy paste the host-manager and ROOT directories from webapps.

Deploy the example2 lp.war under webappsLoggingProxy after deleting loggingproxy directory and war from webapps directory.

Add host entries for org.loggingproxy and org.lp to point to 12.0.0.1.

Open http://org.loggingproxy:8080/lp/ar/ or http://org.lp:8080/lp/ar/ in a browser. It should open the AREDIS homepage.

The above URLs can be used by apps deployed under webapps directory even during their startup because they get loaded after Tomcat is up with loggingproxy Servlet.

The request logs are created under <TOMCAT_HOME>/work/Catalina/org.loggingproxy/lp/reqres.

Older Java Versions

The LoggingProxyServlet.class is compiled with Java 8. If you have an older Java version please re-compile the java file. You will need to change the source to use SimpleDateFormat instead of DateTimeFormatter to do the date formatting. Use a new instance inside the service method since it is not Threadsafe.


Contact

My email is msuresh@cheerful.com


My Other Projects

Jambula: A Lossless Date Stamper for Digital Pictures.

AREDIS: An Asynchronous, pipelined redis client based on the Java 7 NIO Channel API

MediaUtil (LLJTran) A Java Library for Lossless JPEG Transformations and modifying Exif header.



Copyright © 2018, Suresh Mahalingam