01: /**
02: *
03: */package clime.messadmin.providers.user;
04:
05: import java.security.Principal;
06: import java.util.ArrayList;
07: import java.util.Enumeration;
08: import java.util.List;
09:
10: import javax.servlet.http.HttpSession;
11:
12: import clime.messadmin.providers.spi.UserNameProvider;
13:
14: /**
15: * Search all attributes for a single java.security.Principal or javax.security.auth.Subject
16: * @author Cédrik LIME
17: */
18: public class AttributesIterator implements UserNameProvider {
19:
20: /**
21: *
22: */
23: public AttributesIterator() {
24: super ();
25: }
26:
27: /**
28: * {@inheritDoc}
29: */
30: public int getPriority() {
31: return 100;
32: }
33:
34: /**
35: * {@inheritDoc}
36: */
37: public Object guessUserFromSession(HttpSession httpSession) {
38: Object user = null;
39:
40: final List principalArray = new ArrayList();
41: for (Enumeration enumeration = httpSession.getAttributeNames(); enumeration
42: .hasMoreElements();) {
43: String name = (String) enumeration.nextElement();
44: Object obj = httpSession.getAttribute(name);
45: if (null != obj && (obj instanceof Principal)) {//|| obj instanceof Subject)) {
46: principalArray.add(obj);
47: }
48: // This workaround for JDK 1.3 compatibility. For JDK 1.4+, use previous (commented) instanceof.
49: try {
50: Class subjectClass = Class
51: .forName(
52: "javax.security.auth.Subject", true, Thread.currentThread().getContextClassLoader());//$NON-NLS-1$
53: if (subjectClass.isInstance(obj)) {
54: principalArray.add(obj);
55: }
56: } catch (ClassNotFoundException cnfe) {
57: // This is JDK 1.3: javax.security.auth.Subject does not exist; do nothing
58: }
59: }
60: if (principalArray.size() == 1) {
61: user = principalArray.get(0);
62: }
63:
64: return user;
65: }
66:
67: }
|