Showing posts with label java servlets. Show all posts
Showing posts with label java servlets. Show all posts

Monday, July 18, 2011

Logging full HTTP Requests in Java Servlets (à la Django)

I find it very useful to be able to LOG full HTTP requests (parameters, cookies, HTTP headers) when doing web development. It's probably more convenient using a good debugger (Eclipse, Visual Studio and similar products have great debuggers embedded) but there are some scenarios where debuggers are hard to use:


  1. Debugging on remote servers
  2. Debugging API callbacks (Facebook API callbacks, Twitter API callbacks)
  3. Just plain logging information when hosting a RESTful/SOAPAPI
When using  Django (one of many Python's web development frameworks) these lines of code,

logger = logging.getLogger(__name__)
def twitter_callback(request):
    logger.info(request)
    # rest of HTTP processing follows...

yield a very useful print of the HTTP request that looks like:


<WSGIRequest
GET:<QueryDict: {}>,
POST:<QueryDict: {u'signed_request': [u'XXXXXXX',
 'csrftoken': 'XXXXXXX',
 'galletita_67': '6767'},
META:{'CONTENT_LENGTH': '378',
 'CONTENT_TYPE': 'application/x-www-form-urlencoded',
 'CSRF_COOKIE': 'XXXXXX',
 'DOCUMENT_ROOT': 'XXXXXXX',
 'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
 'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch',
 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8',
 'HTTP_CACHE_CONTROL': 'max-age=0',
 'HTTP_CONNECTION': 'keep-alive',
 'HTTP_COOKIE': '__utma=XXXXX; csrftoken=XXXXX; galletita_67=6767',
 'HTTP_HOST': 'example.com',
 'HTTP_ORIGIN': 'http://apps.facebook.com',
 'HTTP_REFERER': 'http://apps.facebook.com/example/',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30',
 'PATH': '/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin',
 'PATH_INFO': u'/example/',
 'PATH_TRANSLATED': '/usr/local/www/apps/example_django/django.wsgi/example/',
 'QUERY_STRING': '',
 'REMOTE_ADDR': '192.168.16.240',
 'REMOTE_PORT': '56330',
 'REQUEST_METHOD': 'POST',
 'REQUEST_URI': '/tigo_paraguay/',
 'SCRIPT_FILENAME': '/usr/local/www/apps/example_django/django.wsgi',
 'SCRIPT_NAME': u'',
 'SERVER_ADDR': 'X.Y.Z.Z0',
 'SERVER_ADMIN': 'rachel@example.com',
 'SERVER_NAME': 'example.com',
 'SERVER_PORT': '80',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SIGNATURE': '',
 'SERVER_SOFTWARE': 'Apache/2.2.17 (FreeBSD) mod_ssl/2.2.17 OpenSSL/0.9.8e DAV/2 mod_wsgi/2.8 Python/2.5.5 PHP/5.3.6 with Suhosin-Patch SVN/1.6.16',
 'UNIQUE_ID': 'XXXXX',
 'mod_wsgi.application_group': 'example.com|',
 'mod_wsgi.callable_object': 'application',
 'mod_wsgi.listener_host': '',
 'mod_wsgi.listener_port': '80',
 'mod_wsgi.process_group': '',
 'mod_wsgi.reload_mechanism': '0',
 'mod_wsgi.script_reloading': '1',
 'mod_wsgi.version': (2, 8),
 'wsgi.errors': <mod_wsgi.Log object at 0x2b6b6da0>,
 'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0x2b6a24e8>,
 'wsgi.input': <mod_wsgi.Input object at 0x2b6afb10>,
 'wsgi.multiprocess': True,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0)}>

Well, I wanted something similar in Java Servlets. So I did what most of us developers do these days. I posted a question on StackOverflow asking if there is something like this available somewhere. And even though I got a lot of comments (*), five days later I had no answers on the topic.

(*)

So I wrote a little Java helper class to print a HTTP Request in a similar manner, so we can internally use in our devteam. Here's the class' source code in case you want something similar. It works with no other dependencies than JavaEE libraries (for javax.servlet.HttpRequest interface access) and produces output quite similar to what you get on Django.

To use it, just add the class to your Java Web project and use it like this:

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // debugInfo should print something similar to what you get in DJANGO
    logger.debug(RequestPrinter.debugString(request));
    // ... rest of servlet code follows...
}


Spanish translated entry will come in a few days.