1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.web.tags;
20
21 import java.beans.BeanInfo;
22 import java.beans.Introspector;
23 import java.beans.PropertyDescriptor;
24 import java.io.IOException;
25 import javax.servlet.jsp.JspException;
26 import javax.servlet.jsp.JspTagException;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class PrincipalTag extends SecureTag {
46
47
48
49
50
51
52
53
54
55
56 private static final Logger log = LoggerFactory.getLogger(PrincipalTag.class);
57
58
59
60
61 private String type;
62
63
64
65
66 private String property;
67
68
69
70
71 private String defaultValue;
72
73
74
75
76
77
78
79
80
81
82 public String getType() {
83 return type;
84 }
85
86
87 public void setType(String type) {
88 this.type = type;
89 }
90
91
92 public String getProperty() {
93 return property;
94 }
95
96
97 public void setProperty(String property) {
98 this.property = property;
99 }
100
101
102 public String getDefaultValue() {
103 return defaultValue;
104 }
105
106
107 public void setDefaultValue(String defaultValue) {
108 this.defaultValue = defaultValue;
109 }
110
111
112
113
114
115
116 @SuppressWarnings({"unchecked"})
117 public int onDoStartTag() throws JspException {
118 String strValue = null;
119
120 if (getSubject() != null) {
121
122
123 Object principal;
124
125 if (type == null) {
126 principal = getSubject().getPrincipal();
127 } else {
128 principal = getPrincipalFromClassName();
129 }
130
131
132 if (principal != null) {
133 if (property == null) {
134 strValue = principal.toString();
135 } else {
136 strValue = getPrincipalProperty(principal, property);
137 }
138 }
139
140 }
141
142
143 if (strValue != null) {
144 try {
145 pageContext.getOut().write(strValue);
146 } catch (IOException e) {
147 throw new JspTagException("Error writing [" + strValue + "] to JSP.", e);
148 }
149 }
150
151 return SKIP_BODY;
152 }
153
154 @SuppressWarnings({"unchecked"})
155 private Object getPrincipalFromClassName() {
156 Object principal = null;
157
158 try {
159 Class cls = Class.forName(type);
160 principal = getSubject().getPrincipals().oneByType(cls);
161 } catch (ClassNotFoundException e) {
162 if (log.isErrorEnabled()) {
163 log.error("Unable to find class for name [" + type + "]");
164 }
165 }
166 return principal;
167 }
168
169
170 private String getPrincipalProperty(Object principal, String property) throws JspTagException {
171 String strValue = null;
172
173 try {
174 BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
175
176
177 boolean foundProperty = false;
178 for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
179 if (pd.getName().equals(property)) {
180 Object value = pd.getReadMethod().invoke(principal, (Object[]) null);
181 strValue = String.valueOf(value);
182 foundProperty = true;
183 break;
184 }
185 }
186
187 if (!foundProperty) {
188 final String message = "Property [" + property + "] not found in principal of type [" + principal.getClass().getName() + "]";
189 if (log.isErrorEnabled()) {
190 log.error(message);
191 }
192 throw new JspTagException(message);
193 }
194
195 } catch (Exception e) {
196 final String message = "Error reading property [" + property + "] from principal of type [" + principal.getClass().getName() + "]";
197 if (log.isErrorEnabled()) {
198 log.error(message, e);
199 }
200 throw new JspTagException(message, e);
201 }
202
203 return strValue;
204 }
205 }