Corrected page numbering.
[photos] / jaccmagic.txt
1   // https://blogs.oracle.com/monzillo/entry/using_jacc_to_determine_a
2
3         Subject subject = (Subject) PolicyContext
4             .getContext("javax.security.auth.Subject.container");
5         CodeSource cs = new CodeSource(null,
6             (java.security.cert.Certificate[]) null);
7         Principal principals[] = (subject == null ? new Principal[0] : subject
8             .getPrincipals().toArray(new Principal[0]));
9         for (Principal principal : principals) {
10             System.out
11                 .println(principal + " " + principal.getClass().getName());
12         }
13
14         ProtectionDomain pd = new ProtectionDomain(cs, null, null, principals);
15         Policy policy = Policy.getPolicy();
16         PermissionCollection pc = policy.getPermissions(pd);
17         pc.implies(new WebRoleRefPermission(null, null));
18         Set<String> roleSet = new HashSet<String>();
19         Enumeration<Permission> e = pc.elements();
20         while (e.hasMoreElements()) {
21             Permission p = e.nextElement();
22             if (p instanceof WebRoleRefPermission) {
23                 String roleRef = p.getActions();
24                 // confirm roleRef via isUserInRole to ensure proper scoping to
25                 // Servlet Name
26                 // if (request.isUserInRole(roleRef)) {
27                 roleSet.add(p.getActions());
28                 System.out.println("Role actions: " + p.getActions());
29                 System.out.println("Role name:    " + p.getName());
30                 // }
31             }
32         }
33         System.out.println("Roles in the context of this page: " + roleSet);