KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > jee > taglib > PrivCredential


1 /*
2  jGuard is a security framework based on top of jaas (java authentication and authorization security).
3  it is written for web applications, to resolve simply, access control problems.
4  version $Name$
5  http://sourceforge.net/projects/jguard/
6
7  Copyright (C) 2004 Charles GAY
8
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26
27  */

28 package net.sf.jguard.jee.taglib;
29
30 import java.io.IOException JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javax.security.auth.Subject JavaDoc;
35 import javax.servlet.jsp.JspException JavaDoc;
36 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
37
38 import net.sf.jguard.core.authentication.credentials.JGuardCredential;
39 import net.sf.jguard.jee.authentication.http.HttpConstants;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
44
45 /**
46  * display the Private Credential of the Subject.
47  *
48  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
49  */

50 public class PrivCredential extends TagSupport JavaDoc implements HttpConstants {
51
52     /** Logger for this class */
53     private static final Log logger = LogFactory.getLog(PrivCredential.class);
54
55     /**
56      * serial version id.
57      */

58     private static final long serialVersionUID = 3546084670207767349L;
59     private String JavaDoc id = "";
60     private String JavaDoc def;
61
62     /**
63      * @return id
64      */

65     public String JavaDoc getId() {
66         return id;
67     }
68
69     /**
70      * @param id
71      */

72     public void setId(String JavaDoc id) {
73         this.id = id;
74     }
75
76     /**
77      *
78      */

79     public int doEndTag() throws JspException JavaDoc {
80         String JavaDoc value = null;
81         String JavaDoc defaut = (String JavaDoc) ExpressionEvaluatorManager.evaluate("default", def, String JavaDoc.class, this, pageContext);
82
83         String JavaDoc name = (String JavaDoc) ExpressionEvaluatorManager.evaluate("id", id, String JavaDoc.class, this, pageContext);
84         Subject JavaDoc subject = TagUtils.getSubject(pageContext);
85
86         try {
87             if (subject != null) {
88
89                 Set JavaDoc privateCredentials = subject.getPrivateCredentials();
90                 Iterator JavaDoc it = privateCredentials.iterator();
91                 JGuardCredential cred = null;
92
93                 while (it.hasNext()) {
94                     cred = (JGuardCredential) it.next();
95                     // if the id wanted by the webapp developer
96
// is encountered in the public credentials subject
97
if (cred.getId().equals(name)) {
98                         value = (String JavaDoc) cred.getValue();
99                         break;
100                     }
101                 }
102             }
103         } catch (SecurityException JavaDoc sex) {
104             if (logger.isErrorEnabled()) {
105                 logger.error("doEndTag() - you don't have the permission to access to the private credentialsn");
106             }
107             if (logger.isErrorEnabled()) {
108                 logger.error("doEndTag() - you should contact your administrator server n ");
109             }
110             value = "you don't have the permission to access to the private credentials";
111         }
112         if (value == null && defaut != null) {
113             value = defaut;
114         } else if (name == null) {
115             value = "";
116         }
117
118         if (logger.isDebugEnabled()) {
119             logger.debug("<jguard:PrivCredential> id=" + this.id);
120             logger.debug("<jguard:PrivCredential> default=" + defaut);
121             logger.debug("<jguard:PrivCredential> value=" + value);
122         }
123         try {
124             // output the value
125
pageContext.getOut().print(value);
126         } catch (IOException JavaDoc e) {
127             logger.error("doEndTag()", e);
128         }
129
130         return EVAL_PAGE;
131     }
132
133     /**
134      * @return def.
135      */

136     public String JavaDoc getDefault() {
137         return def;
138     }
139
140     /**
141      * @param def
142      * The def to set.
143      */

144     public void setDefault(String JavaDoc def) {
145         this.def = def;
146     }
147 }
148
Popular Tags