Patrick
Patrick

Reputation: 3981

Why does ServletRequest getAttribute() have different attributes than getAttributeNames()?

We run Apache infront of Tomcat and use mod_jk. Our single sign on Apache module sets information about the user which we can retrieve in Java with a getAttribute() call.

String mobileNumber = request.getAttribute("WEBAUTH_LDAP_MOBILE");

This works fine. Now I wanted to retrieve all attributes and look for ones named with a prefix "WEBAUTH_LDAP_". I used getAttributeNames() for this.

Enumeration<String> enumeration = request.getAttributeNames();

to get the attribute names. To my surprise there is no attribute named "WEBAUTH_LDAP_MOBILE". Is this expected? Is there a way to get all attributes? The JavaDoc makes it sound like something in getAttribute() should also be in getAttributeNames().

We are using Tomcat 6.0.28.

Upvotes: 1

Views: 1290

Answers (1)

Patrick
Patrick

Reputation: 3981

This is because any attributes set with mod_jk are available with getAttribute() but not through getAttributeNames(). According to the documentation

You can retrieve the variables on Tomcat as request attributes via request.getAttribute(attributeName). Note that the variables send via JkEnvVar will not be listed in request.getAttributeNames().

I debugged through all the RequestWrappers (per BalusC's suggestion) and the underlying request has an internal map of attributes which are used for getAttributeNames(). However, getAttribute() has a fall through to another object when the internal map's value is null. From the javadoc and documentation, this is working as designed.

This behavior has previously been reported as a bug, but the fix fails the TCK test:

I looked at just including all the Tomcat internal attributes in the return from getAttributeNames() but this causes problems with the Servlet 2.5 TCK tests which expect that getAttributeNames() return only those attributes that have been set via setAttribute().

So in short, getAttributeNames() will return attributes set with setAttribute() while getAttribute() can return attributes set through various other (internal) means.

Upvotes: 1

Related Questions