KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CheckLogonTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2000-2004 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.webapp.example;
19
20
21 import java.io.IOException JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27 import org.apache.struts.config.ModuleConfig;
28
29
30 /**
31  * Check for a valid User logged on in the current session. If there is no
32  * such user, forward control to the logon page.
33  *
34  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
35  */

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

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

45     private String JavaDoc name = Constants.USER_KEY;
46
47
48     /**
49      * Path to use if a login is needed.
50      */

51     private static String JavaDoc LOGIN_PATH = "/Logon.do";
52
53
54     /**
55      * The action to which we should forward for the user to log on.
56      */

57     private String JavaDoc page = LOGIN_PATH;
58
59
60     // ------------------------------------------------------- Public Methods
61

62
63     /**
64      * Defer our checking until the end of this tag is encountered.
65      *
66      * @exception JspException if a JSP exception has occurred
67      */

68     public int doStartTag() throws JspException JavaDoc {
69
70        return (SKIP_BODY);
71
72     }
73
74
75     /**
76      * Perform our logged-in user check by looking for the existence of
77      * a session scope bean under the specified name. If this bean is not
78      * present, control is forwarded to the specified logon page.
79      *
80      * @exception JspException if a JSP exception has occurred
81      */

82     public int doEndTag() throws JspException JavaDoc {
83     
84         // Is there a valid user logged on?
85
boolean valid = false;
86         HttpSession JavaDoc session = pageContext.getSession();
87         if ((session != null) && (session.getAttribute(name) != null)) {
88             valid = true;
89         }
90     
91         // Forward control based on the results
92
if (valid) {
93             return (EVAL_PAGE);
94         } else {
95             ModuleConfig config =
96                 (ModuleConfig) pageContext.getServletContext().getAttribute(
97                     org.apache.struts.Globals.MODULE_KEY);
98             
99                 try {
100                     pageContext.forward(config.getPrefix() + page);
101                 } catch (ServletException JavaDoc e) {
102                     throw new JspException JavaDoc(e.toString());
103                 } catch (IOException JavaDoc e) {
104                     throw new JspException JavaDoc(e.toString());
105                 }
106              
107             return (SKIP_PAGE);
108         }
109     
110     }
111
112
113     /**
114      * Release any acquired resources.
115      */

116     public void release() {
117
118         super.release();
119         this.name = Constants.USER_KEY;
120         this.page = LOGIN_PATH;
121
122     }
123
124 }
125
Popular Tags