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