View Javadoc
1   package info.ejava.examples.secureping.ejb;
2   
3   import info.ejava.examples.secureping.ejb.SecurePingRemote;
4   
5   import javax.annotation.PostConstruct;
6   import javax.annotation.Resource;
7   import javax.annotation.security.PermitAll;
8   import javax.annotation.security.RunAs;
9   import javax.ejb.EJB;
10  import javax.ejb.SessionContext;
11  import javax.ejb.Stateless;
12  
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  /**
17   * This session bean allows all methods to be invoked and then performs 
18   * the matching operation on SecurePingEJB using a run-as with an admin role.
19   */
20  @Stateless
21  @PermitAll
22  @RunAs("admin")
23  public class SecurePingClientEJB 
24      implements SecurePingClientRemote, SecurePingClientLocal {
25      private static final Logger logger = LoggerFactory.getLogger(SecurePingClientEJB.class);
26      
27      @Resource
28      SessionContext ctx;
29     
30      @EJB(lookup="ejb:securePingEAR/securePingEJB/SecurePingEJB!info.ejava.examples.secureping.ejb.SecurePingRemote")
31      SecurePingRemote securePingServer;
32      
33      @PostConstruct
34      public void init() {
35          logger.debug("*** SecurePingClientEJB initializing ***");
36          logger.debug("securePingServer=" + securePingServer);
37      }
38      
39      /**
40       * Return what this EJB's container thinks about the caller.
41       */
42      private String getInfo(String prefix) {
43          StringBuilder text = new StringBuilder();
44          text.append("securePingClient called " + prefix);
45          try {
46              text.append(", principal=" + ctx.getCallerPrincipal().getName());
47              text.append(", isUser=" + ctx.isCallerInRole("user"));
48              text.append(", isAdmin=" + ctx.isCallerInRole("admin"));
49              text.append(", isInternalRole=" + 
50                      ctx.isCallerInRole("internalRole"));
51          }
52          catch (Throwable ex) {
53              logger.debug("error calling session context:", ex);
54              text.append(", error calling Session Context:" + ex);
55          }
56          String result = text.toString();
57          logger.debug(result);
58          return result;        
59      }
60  
61      /**
62       * Return info from this bean and the securePingServer after performing
63       * a run-as.
64       */
65      public String pingAll() {
66          return getInfo("pingAll") + ":\nsecurePing=" + securePingServer.pingAll();
67      }
68  
69      /**
70       * Return info from this bean and the securePingServer after performing
71       * a run-as.
72       */
73      public String pingUser() {
74          return getInfo("pingUser") + ":\nsecurePing=" + securePingServer.pingUser();
75      }
76  
77      /**
78       * Return info from this bean and the securePingServer after performing
79       * a run-as.
80       */
81      public String pingAdmin() {        
82          return getInfo("pingAdmin") + ":\nsecurePing=" + securePingServer.pingAdmin();
83      }
84  
85      /**
86       * Return info from this bean and the securePingServer after performing
87       * a run-as.
88       */
89      public String pingExcluded() {
90          return getInfo("pingExcluded") + ":\nsecurePing=" + securePingServer.pingExcluded();
91      }
92      
93      /**
94       * Return the identify of this caller within the context of this EJB invocation.
95       */
96      @Override
97      public String whoAmI() {
98          logger.debug("whoAmI=", ctx.getCallerPrincipal().getName());
99          return ctx.getCallerPrincipal().getName();
100     }
101     
102     /**
103      * Return info from this bean and the securePingServer after performing
104      * a run-as. Most of the details are written to the log since the return
105      * type here is a simple boolean.
106      */
107     public boolean isCallerInRole(String role) {
108         boolean result = ctx.isCallerInRole(role);
109         logger.debug("securePingClient.user=" + ctx.getCallerPrincipal().getName() + 
110                 ", isCallerInRole(" + role + ")=" + result + 
111                 "\n:securePing=" + securePingServer.isCallerInRole(role));  
112         return result;
113     }
114 }