Example 16-3: Action handler to return HTTP header information

import oracle.xml.xsql.*;
import java.util.*;
import javax.servlet.http.*;
import org.w3c.dom.*;

public class HTTPHeaders extends XSQLActionHandlerImpl {
  public void handleAction( Node root ) {
    if (getPageRequest().getRequestType().equals("Servlet")) {
      // If request type is "Servlet" then it's ok to cast
      XSQLServletPageRequest xspr = (XSQLServletPageRequest)getPageRequest();
      // Then we can get the HTTPServletRequest from the XSQLServletPageRequest
      HttpServletRequest req  = xspr.getHttpServletRequest();
      // Create an element using the action element's owning document
      Element e = getActionElement().getOwnerDocument().createElement("Headers");
      // Get the text content of the action element that contains the
      // list of http headers the user wants the values of
      String headerList = getActionElementContent();
      // Use a string-tokenizer to parse the text into
      // the list of whitespace-separated tokens it contains
      if (headerList != null && !headerList.equals("")) {
        Enumeration headerNames = null;
        // If user gave just the name "all", then show all headers
        if (headerList.trim().equals("all")) {
          headerNames = req.getHeaderNames();
        }
        else {
          headerNames = new StringTokenizer(headerList);
        }
        while (headerNames.hasMoreElements()) {
          String headerName  = (String)headerNames.nextElement();
          String headerValue = req.getHeader(headerName);
          // Append the <Name>Value</Name> to the <Headers> element
          addResultElement(e,headerName,headerValue);
        }
      }
      // Append the <Headers> element to the XML result root for the action
      root.appendChild(e);
    }
  }
}