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
18
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
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
63
64
65 public String pingAll() {
66 return getInfo("pingAll") + ":\nsecurePing=" + securePingServer.pingAll();
67 }
68
69
70
71
72
73 public String pingUser() {
74 return getInfo("pingUser") + ":\nsecurePing=" + securePingServer.pingUser();
75 }
76
77
78
79
80
81 public String pingAdmin() {
82 return getInfo("pingAdmin") + ":\nsecurePing=" + securePingServer.pingAdmin();
83 }
84
85
86
87
88
89 public String pingExcluded() {
90 return getInfo("pingExcluded") + ":\nsecurePing=" + securePingServer.pingExcluded();
91 }
92
93
94
95
96 @Override
97 public String whoAmI() {
98 logger.debug("whoAmI=", ctx.getCallerPrincipal().getName());
99 return ctx.getCallerPrincipal().getName();
100 }
101
102
103
104
105
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 }