1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.logic;
22
23 import org.apache.struts.taglib.TagUtils;
24
25 import javax.servlet.http.Cookie;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.jsp.JspException;
28
29 import java.security.Principal;
30
31 import java.util.StringTokenizer;
32
33
34
35
36
37
38
39
40 public class PresentTag extends ConditionalTagBase {
41 public static final String ROLE_DELIMITER = ",";
42
43
44
45
46
47
48
49
50
51
52
53 protected boolean condition()
54 throws JspException {
55 return (condition(true));
56 }
57
58
59
60
61
62
63
64
65
66
67 protected boolean condition(boolean desired)
68 throws JspException {
69
70 boolean present = false;
71 HttpServletRequest request =
72 (HttpServletRequest) pageContext.getRequest();
73
74 if (cookie != null) {
75 present = this.isCookiePresent(request);
76 } else if (header != null) {
77 String value = request.getHeader(header);
78
79 present = (value != null);
80 } else if (name != null) {
81 present = this.isBeanPresent();
82 } else if (parameter != null) {
83 String value = request.getParameter(parameter);
84
85 present = (value != null);
86 } else if (role != null) {
87 StringTokenizer st =
88 new StringTokenizer(role, ROLE_DELIMITER, false);
89
90 while (!present && st.hasMoreTokens()) {
91 present = request.isUserInRole(st.nextToken());
92 }
93 } else if (user != null) {
94 Principal principal = request.getUserPrincipal();
95
96 present = (principal != null) && user.equals(principal.getName());
97 } else {
98 JspException e =
99 new JspException(messages.getMessage("logic.selector"));
100
101 TagUtils.getInstance().saveException(pageContext, e);
102 throw e;
103 }
104
105 return (present == desired);
106 }
107
108
109
110
111
112
113
114 protected boolean isBeanPresent() {
115 Object value = null;
116
117 try {
118 if (this.property != null) {
119 value =
120 TagUtils.getInstance().lookup(pageContext, name,
121 this.property, scope);
122 } else {
123 value = TagUtils.getInstance().lookup(pageContext, name, scope);
124 }
125 } catch (JspException e) {
126 value = null;
127 }
128
129 return (value != null);
130 }
131
132
133
134
135
136
137 protected boolean isCookiePresent(HttpServletRequest request) {
138 Cookie[] cookies = request.getCookies();
139
140 if (cookies == null) {
141 return false;
142 }
143
144 for (int i = 0; i < cookies.length; i++) {
145 if (this.cookie.equals(cookies[i].getName())) {
146 return true;
147 }
148 }
149
150 return false;
151 }
152 }