1 28 package net.sf.jguard.jee.taglib; 29 30 import java.io.IOException ; 31 import java.util.Iterator ; 32 import java.util.Set ; 33 34 import javax.security.auth.Subject ; 35 import javax.servlet.jsp.JspException ; 36 import javax.servlet.jsp.tagext.TagSupport ; 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 50 public class PrivCredential extends TagSupport implements HttpConstants { 51 52 53 private static final Log logger = LogFactory.getLog(PrivCredential.class); 54 55 58 private static final long serialVersionUID = 3546084670207767349L; 59 private String id = ""; 60 private String def; 61 62 65 public String getId() { 66 return id; 67 } 68 69 72 public void setId(String id) { 73 this.id = id; 74 } 75 76 79 public int doEndTag() throws JspException { 80 String value = null; 81 String defaut = (String ) ExpressionEvaluatorManager.evaluate("default", def, String .class, this, pageContext); 82 83 String name = (String ) ExpressionEvaluatorManager.evaluate("id", id, String .class, this, pageContext); 84 Subject subject = TagUtils.getSubject(pageContext); 85 86 try { 87 if (subject != null) { 88 89 Set privateCredentials = subject.getPrivateCredentials(); 90 Iterator it = privateCredentials.iterator(); 91 JGuardCredential cred = null; 92 93 while (it.hasNext()) { 94 cred = (JGuardCredential) it.next(); 95 if (cred.getId().equals(name)) { 98 value = (String ) cred.getValue(); 99 break; 100 } 101 } 102 } 103 } catch (SecurityException 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 pageContext.getOut().print(value); 126 } catch (IOException e) { 127 logger.error("doEndTag()", e); 128 } 129 130 return EVAL_PAGE; 131 } 132 133 136 public String getDefault() { 137 return def; 138 } 139 140 144 public void setDefault(String def) { 145 this.def = def; 146 } 147 } 148 | Popular Tags |