Print HTTP headers in ATG

ATG provides a class named InsertableServletImpl, which can do pretty much what it sounds like. You insert the servlet to be run in the ATG servlet pipeline where you dictate. This allows us to inject behavior into our application at points that make sense.

For example, you may want to switch the URL for a given page from http to https, based on it its relative path location, such as /secret, or /creditcard, or /login, etc.

While developing this example, I also found the API to access headers sent by the browser. I decided to combine both findings into one post.

In our example below, we simply print the headers sent by our browser to standard output (server.log in JBOSS) whenever a servlet is called. Of interest is the fact the JSP doesn’t even have to exist. The servlet pipeline will be called by the engine, run our code below, print our headers, and then throw the exception to the client that the page doesn’t. Not terribly useful, but interesting, nonetheless.

Java code…

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import atg.servlet.*;
import atg.servlet.pipeline.*;

public class URIPrinter extends InsertableServletImpl {
  public URIPrinter () {}
  public void service (DynamoHttpServletRequest request,DynamoHttpServletResponse response) throws IOException, ServletException {
    System.out.println ("Handling request for " +  request.getRequestURI () + " from " + request.getRemoteAddr());
    Enumeration cookies = request.getCookieParameterNames ();
    while (cookies.hasMoreElements()) {
      String cookie = (String)cookies.nextElement();
      System.out.println(cookie);
      String[] cookieValues = request.getCookieParameterValues(cookie);
      for (int i = 0; i < cookieValues.length; i++) {
        System.out.println(cookieValues[i]);
      }
    }
    Enumeration headers = request.getHeaderNames ();
    while (headers.hasMoreElements()) {
      String header = (String)headers.nextElement();
      Enumeration headerValues = request.getHeaders(header);
      while (headerValues.hasMoreElements()) {
        System.out.println(String.format("%1$-35s",header) + headerValues.nextElement());
      }
    }
    passRequest (request, response);
  }
}

Component properties file

$class=URIPrinter
$scope=global
insertAfterServlet=/atg/dynamo/servlet/dafpipeline/DynamoServlet

Override Initial.properties for /atg/dynamo/servlet/

initialServices+=/URIPrinter

Restart your application server, request any page (even one that doesn't exist, as noted earlier), and you should see the headers above printed in the server log.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.