KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > example2 > CheckLogonTag


1 /*
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.struts.webapp.example2;
19
20
21 import javax.servlet.http.HttpSession JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25
26 /**
27  * Check for a valid User logged on in the current session. If there is no
28  * such user, forward control to the logon page.
29  *
30  * @author Craig R. McClanahan
31  * @author Marius Barduta
32  * @version $Rev: 54934 $ $Date: 2004-10-16 18:07:50 +0100 (Sat, 16 Oct 2004) $
33  */

34
35 public final class CheckLogonTag extends TagSupport JavaDoc {
36
37
38     // --------------------------------------------------- Instance Variables
39

40
41     /**
42      * The key of the session-scope bean we look for.
43      */

44     private String JavaDoc name = Constants.USER_KEY;
45
46
47     /**
48      * The page to which we should forward for the user to log on.
49      */

50     private String JavaDoc page = "/logon.jsp";
51
52
53     // ----------------------------------------------------------- Properties
54

55
56     /**
57      * Return the bean name.
58      */

59     public String JavaDoc getName() {
60
61     return (this.name);
62
63     }
64
65
66     /**
67      * Set the bean name.
68      *
69      * @param name The new bean name
70      */

71     public void setName(String JavaDoc name) {
72
73     this.name = name;
74
75     }
76
77
78     /**
79      * Return the forward page.
80      */

81     public String JavaDoc getPage() {
82
83     return (this.page);
84
85     }
86
87
88     /**
89      * Set the forward page.
90      *
91      * @param page The new forward page
92      */

93     public void setPage(String JavaDoc page) {
94
95     this.page = page;
96
97     }
98
99
100     // ------------------------------------------------------- Public Methods
101

102
103     /**
104      * Defer our checking until the end of this tag is encountered.
105      *
106      * @exception JspException if a JSP exception has occurred
107      */

108     public int doStartTag() throws JspException JavaDoc {
109
110     return (SKIP_BODY);
111
112     }
113
114
115     /**
116      * Perform our logged-in user check by looking for the existence of
117      * a session scope bean under the specified name. If this bean is not
118      * present, control is forwarded to the specified logon page.
119      *
120      * @exception JspException if a JSP exception has occurred
121      */

122     public int doEndTag() throws JspException JavaDoc {
123
124     // Is there a valid user logged on?
125
boolean valid = false;
126     HttpSession JavaDoc session = pageContext.getSession();
127     if ((session != null) && (session.getAttribute(name) != null))
128         valid = true;
129
130     // Forward control based on the results
131
if (valid)
132         return (EVAL_PAGE);
133     else {
134         try {
135         pageContext.forward(page);
136         } catch (Exception JavaDoc e) {
137         throw new JspException JavaDoc(e.toString());
138         }
139         return (SKIP_PAGE);
140     }
141
142     }
143
144
145     /**
146      * Release any acquired resources.
147      */

148     public void release() {
149
150         super.release();
151         this.name = Constants.USER_KEY;
152         this.page = "/logon.jsp";
153
154     }
155
156
157 }
158
Popular Tags