{"id":2596,"date":"2012-11-21T09:25:26","date_gmt":"2012-11-21T14:25:26","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=2596"},"modified":"2012-11-21T09:25:34","modified_gmt":"2012-11-21T14:25:34","slug":"print-http-headers-in-atg","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2012\/11\/21\/print-http-headers-in-atg\/","title":{"rendered":"Print HTTP headers in ATG"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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&#8217;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&#8217;t.  Not terribly useful, but interesting, nonetheless.<\/p>\n<p>Java code&#8230;<\/p>\n<pre lang=\"java\">\r\nimport javax.servlet.*;\r\nimport javax.servlet.http.*;\r\nimport java.io.*;\r\nimport java.util.*;\r\nimport atg.servlet.*;\r\nimport atg.servlet.pipeline.*;\r\n\r\npublic class URIPrinter extends InsertableServletImpl {\r\n  public URIPrinter () {}\r\n  public void service (DynamoHttpServletRequest request,DynamoHttpServletResponse response) throws IOException, ServletException {\r\n    System.out.println (\"Handling request for \" +  request.getRequestURI () + \" from \" + request.getRemoteAddr());\r\n    Enumeration cookies = request.getCookieParameterNames ();\r\n    while (cookies.hasMoreElements()) {\r\n      String cookie = (String)cookies.nextElement();\r\n      System.out.println(cookie);\r\n      String[] cookieValues = request.getCookieParameterValues(cookie);\r\n      for (int i = 0; i < cookieValues.length; i++) {\r\n        System.out.println(cookieValues[i]);\r\n      }\r\n    }\r\n    Enumeration headers = request.getHeaderNames ();\r\n    while (headers.hasMoreElements()) {\r\n      String header = (String)headers.nextElement();\r\n      Enumeration headerValues = request.getHeaders(header);\r\n      while (headerValues.hasMoreElements()) {\r\n        System.out.println(String.format(\"%1$-35s\",header) + headerValues.nextElement());\r\n      }\r\n    }\r\n    passRequest (request, response);\r\n  }\r\n}\r\n<\/pre>\n<p>Component properties file<\/p>\n<pre lang=\"text\">\r\n$class=URIPrinter\r\n$scope=global\r\ninsertAfterServlet=\/atg\/dynamo\/servlet\/dafpipeline\/DynamoServlet\r\n<\/pre>\n<p>Override Initial.properties for \/atg\/dynamo\/servlet\/<\/p>\n<pre lang=\"text\">\r\ninitialServices+=\/URIPrinter\r\n<\/pre>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2012\/11\/21\/print-http-headers-in-atg\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[37,38,25],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2596"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2596"}],"version-history":[{"count":10,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2596\/revisions"}],"predecessor-version":[{"id":2606,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2596\/revisions\/2606"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2596"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}