View Javadoc

1   /*
2    * $Id: PresentTag.java 376842 2006-02-10 21:02:03Z husted $
3    *
4    * Copyright 2000-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.taglib.logic;
19  
20  import org.apache.struts.taglib.TagUtils;
21  
22  import javax.servlet.http.Cookie;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.jsp.JspException;
25  
26  import java.security.Principal;
27  
28  import java.util.StringTokenizer;
29  
30  /***
31   * Evalute the nested body content of this tag if the specified value is
32   * present for this request.
33   *
34   * @version $Rev: 376842 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
35   *          $
36   */
37  public class PresentTag extends ConditionalTagBase {
38      public static final String ROLE_DELIMITER = ",";
39  
40      // ------------------------------------------------------ Protected Methods
41  
42      /***
43       * Evaluate the condition that is being tested by this particular tag, and
44       * return <code>true</code> if the nested body content of this tag should
45       * be evaluated, or <code>false</code> if it should be skipped. This
46       * method must be implemented by concrete subclasses.
47       *
48       * @throws JspException if a JSP exception occurs
49       */
50      protected boolean condition()
51          throws JspException {
52          return (condition(true));
53      }
54  
55      /***
56       * Evaluate the condition that is being tested by this particular tag, and
57       * return <code>true</code> if the nested body content of this tag should
58       * be evaluated, or <code>false</code> if it should be skipped. This
59       * method must be implemented by concrete subclasses.
60       *
61       * @param desired Desired outcome for a true result
62       * @throws JspException if a JSP exception occurs
63       */
64      protected boolean condition(boolean desired)
65          throws JspException {
66          // Evaluate the presence of the specified value
67          boolean present = false;
68          HttpServletRequest request =
69              (HttpServletRequest) pageContext.getRequest();
70  
71          if (cookie != null) {
72              present = this.isCookiePresent(request);
73          } else if (header != null) {
74              String value = request.getHeader(header);
75  
76              present = (value != null);
77          } else if (name != null) {
78              present = this.isBeanPresent();
79          } else if (parameter != null) {
80              String value = request.getParameter(parameter);
81  
82              present = (value != null);
83          } else if (role != null) {
84              StringTokenizer st =
85                  new StringTokenizer(role, ROLE_DELIMITER, false);
86  
87              while (!present && st.hasMoreTokens()) {
88                  present = request.isUserInRole(st.nextToken());
89              }
90          } else if (user != null) {
91              Principal principal = request.getUserPrincipal();
92  
93              present = (principal != null) && user.equals(principal.getName());
94          } else {
95              JspException e =
96                  new JspException(messages.getMessage("logic.selector"));
97  
98              TagUtils.getInstance().saveException(pageContext, e);
99              throw e;
100         }
101 
102         return (present == desired);
103     }
104 
105     /***
106      * Returns true if the bean given in the <code>name</code> attribute is
107      * found.
108      *
109      * @since Struts 1.2
110      */
111     protected boolean isBeanPresent() {
112         Object value = null;
113 
114         try {
115             if (this.property != null) {
116                 value =
117                     TagUtils.getInstance().lookup(pageContext, name,
118                         this.property, scope);
119             } else {
120                 value = TagUtils.getInstance().lookup(pageContext, name, scope);
121             }
122         } catch (JspException e) {
123             value = null;
124         }
125 
126         return (value != null);
127     }
128 
129     /***
130      * Returns true if the cookie is present in the request.
131      *
132      * @since Struts 1.2
133      */
134     protected boolean isCookiePresent(HttpServletRequest request) {
135         Cookie[] cookies = request.getCookies();
136 
137         if (cookies == null) {
138             return false;
139         }
140 
141         for (int i = 0; i < cookies.length; i++) {
142             if (this.cookie.equals(cookies[i].getName())) {
143                 return true;
144             }
145         }
146 
147         return false;
148     }
149 }