KickJava   Java API By Example, From Geeks To Geeks.

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


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.jee.authentication.http.HttpConstants;
39  import net.sf.jguard.core.authentication.credentials.JGuardCredential;
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 Public Credential of the Subject.
47   *
48   * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
49   *
50   */

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

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

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

71     public void setId(String JavaDoc id) {
72         this.id = id;
73     }
74  
75     public int doEndTag() throws JspException JavaDoc {
76         String JavaDoc value = null;
77         String JavaDoc defaut = (String JavaDoc) ExpressionEvaluatorManager.evaluate("default", def, String JavaDoc.class, this, pageContext);
78         String JavaDoc name = (String JavaDoc) ExpressionEvaluatorManager.evaluate("id", id, String JavaDoc.class, this, pageContext);
79  
80         Subject JavaDoc subject = TagUtils.getSubject(pageContext);
81  
82         if (subject != null) {
83             Set JavaDoc publicCredentials = subject.getPublicCredentials();
84             Iterator JavaDoc it = publicCredentials.iterator();
85             JGuardCredential cred = null;
86  
87             while (it.hasNext()) {
88                 cred = (JGuardCredential) it.next();
89                 // if the id wanted by the webapp developer
90
// is encountered in the public credentials subject
91
if (cred.getId().equals(name)) {
92                     value = (String JavaDoc) cred.getValue();
93                     break;
94                 }
95             }
96         }
97  
98         if (value == null && defaut != null) {
99             value = defaut;
100         } else if (name == null) {
101             value = "";
102         }
103  
104         if (logger.isDebugEnabled()) {
105             logger.debug("<jguard:PubCredential> id=" + this.id);
106             logger.debug("<jguard:PubCredential> default=" + defaut);
107             logger.debug("<jguard:PubCredential> value=" + value);
108         }
109         try {
110             // output the value
111
pageContext.getOut().print(value);
112         } catch (IOException JavaDoc e) {
113             logger.error("doEndTag()", e);
114         }
115  
116         return EVAL_PAGE;
117     }
118  
119     /**
120      * @return Returns the def.
121      */

122     public String JavaDoc getDefault() {
123         return def;
124     }
125  
126     /**
127      * @param def
128      * The def to set.
129      */

130     public void setDefault(String JavaDoc def) {
131         this.def = def;
132     }
133  }
134
Popular Tags