KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > logic > PresentTag


1 /*
2  * $Id: PresentTag.java 54929 2004-10-16 16:38:42Z germuska $
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
19 package org.apache.struts.taglib.logic;
20
21 import java.security.Principal JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import javax.servlet.http.Cookie JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27
28 import org.apache.struts.taglib.TagUtils;
29
30 /**
31  * Evalute the nested body content of this tag if the specified value
32  * is present for this request.
33  *
34  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
35  */

36 public class PresentTag extends ConditionalTagBase {
37
38
39     public static final String JavaDoc ROLE_DELIMITER = ",";
40     
41     // ------------------------------------------------------ Protected Methods
42

43
44     /**
45      * Evaluate the condition that is being tested by this particular tag,
46      * and return <code>true</code> if the nested body content of this tag
47      * should be evaluated, or <code>false</code> if it should be skipped.
48      * This method must be implemented by concrete subclasses.
49      *
50      * @exception JspException if a JSP exception occurs
51      */

52     protected boolean condition() throws JspException JavaDoc {
53
54         return (condition(true));
55
56     }
57
58
59     /**
60      * Evaluate the condition that is being tested by this particular tag,
61      * and return <code>true</code> if the nested body content of this tag
62      * should be evaluated, or <code>false</code> if it should be skipped.
63      * This method must be implemented by concrete subclasses.
64      *
65      * @param desired Desired outcome for a true result
66      *
67      * @exception JspException if a JSP exception occurs
68      */

69     protected boolean condition(boolean desired) throws JspException JavaDoc {
70         // Evaluate the presence of the specified value
71
boolean present = false;
72         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
73         
74         if (cookie != null) {
75             present = this.isCookiePresent(request);
76             
77         } else if (header != null) {
78             String JavaDoc 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 JavaDoc value = request.getParameter(parameter);
86             present = (value != null);
87             
88         } else if (role != null) {
89             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(role, ROLE_DELIMITER, false);
90             while (!present && st.hasMoreTokens()) {
91                 present = request.isUserInRole(st.nextToken());
92             }
93             
94         } else if (user != null) {
95             Principal JavaDoc principal = request.getUserPrincipal();
96             present = (principal != null) && user.equals(principal.getName());
97             
98         } else {
99             JspException JavaDoc e = new JspException JavaDoc
100                 (messages.getMessage("logic.selector"));
101             TagUtils.getInstance().saveException(pageContext, e);
102             throw e;
103         }
104
105         return (present == desired);
106
107     }
108
109     /**
110      * Returns true if the bean given in the <code>name</code> attribute is found.
111      * @since Struts 1.2
112      */

113     protected boolean isBeanPresent() {
114         Object JavaDoc 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 JavaDoc e) {
122             value = null;
123         }
124         
125         return (value != null);
126     }
127
128     /**
129      * Returns true if the cookie is present in the request.
130      * @since Struts 1.2
131      */

132     protected boolean isCookiePresent(HttpServletRequest JavaDoc request) {
133         Cookie JavaDoc 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