I'm trying to log the contents of the HttpServletRequest attributes collection. I need to do this when the servlet first starts, and again right before the servlet is finished. I'm doing this in an attempt to understand a crufty and ill-maintained servlet. Because I need to have as little impact as possible, servlet filters are not an option.
So here's the problem. When the servlet starts, I'll iterate through the enumeration returned by HttpServletRequest.getAttributeNames(). However, when I want to iterate through it again, getAttributeNames().hasMoreElements() returns "false"! I can't find any way to "reset" the enumeration. What's worse is that, even if I add attributes to the collection using HttpServletRequest.setAttribute(), I still get a result of "false" when I call getAttributeNames().hasMoreElements().
Is this really possible? Is there really no way to iterate through the attribute names more than once?
By request, here's my code. It's pretty straightforward -- don't think I'm doing any funny stuff.
/**
*
* Returns the contents of the Attributes collection, formatted for the InterfaceTracker loglines
*
*/
@SuppressWarnings("unchecked")
public static String getAttributes(HttpServletRequest request) {
try {
StringBuilder toLog = new StringBuilder();
Enumeration attributeNames = request.getAttributeNames();
while(attributeNames.hasMoreElements()) {
String current = (String) attributeNames.nextElement();
toLog.append(current + "=" + request.getAttribute(current));
if(attributeNames.hasMoreElements()) {
toLog.append(", ");
}
}
return "TRACKER_ATTRIBUTES={"+ toLog.toString() + "}";
}
catch (Exception ex) {
return "TRACKER_ATTRIBUTES={" + InterfaceTrackerValues.DATA_UNKNOWN_EXCEPTION_THROWN + "}";
}
}