001: /*
002: * $Id: CheckLogonTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.webapp.example2;
023:
024: import javax.servlet.http.HttpSession;
025: import javax.servlet.jsp.JspException;
026: import javax.servlet.jsp.tagext.TagSupport;
027:
028: /**
029: * Check for a valid User logged on in the current session. If there is no
030: * such user, forward control to the logon page.
031: *
032: * @author Craig R. McClanahan
033: * @author Marius Barduta
034: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
035: */
036:
037: public final class CheckLogonTag extends TagSupport {
038:
039: // --------------------------------------------------- Instance Variables
040:
041: /**
042: * The key of the session-scope bean we look for.
043: */
044: private String name = Constants.USER_KEY;
045:
046: /**
047: * The page to which we should forward for the user to log on.
048: */
049: private String page = "/logon.jsp";
050:
051: // ----------------------------------------------------------- Properties
052:
053: /**
054: * Return the bean name.
055: */
056: public String getName() {
057:
058: return (this .name);
059:
060: }
061:
062: /**
063: * Set the bean name.
064: *
065: * @param name The new bean name
066: */
067: public void setName(String name) {
068:
069: this .name = name;
070:
071: }
072:
073: /**
074: * Return the forward page.
075: */
076: public String getPage() {
077:
078: return (this .page);
079:
080: }
081:
082: /**
083: * Set the forward page.
084: *
085: * @param page The new forward page
086: */
087: public void setPage(String page) {
088:
089: this .page = page;
090:
091: }
092:
093: // ------------------------------------------------------- Public Methods
094:
095: /**
096: * Defer our checking until the end of this tag is encountered.
097: *
098: * @exception JspException if a JSP exception has occurred
099: */
100: public int doStartTag() throws JspException {
101:
102: return (SKIP_BODY);
103:
104: }
105:
106: /**
107: * Perform our logged-in user check by looking for the existence of
108: * a session scope bean under the specified name. If this bean is not
109: * present, control is forwarded to the specified logon page.
110: *
111: * @exception JspException if a JSP exception has occurred
112: */
113: public int doEndTag() throws JspException {
114:
115: // Is there a valid user logged on?
116: boolean valid = false;
117: HttpSession session = pageContext.getSession();
118: if ((session != null) && (session.getAttribute(name) != null))
119: valid = true;
120:
121: // Forward control based on the results
122: if (valid)
123: return (EVAL_PAGE);
124: else {
125: try {
126: pageContext.forward(page);
127: } catch (Exception e) {
128: throw new JspException(e.toString());
129: }
130: return (SKIP_PAGE);
131: }
132:
133: }
134:
135: /**
136: * Release any acquired resources.
137: */
138: public void release() {
139:
140: super .release();
141: this .name = Constants.USER_KEY;
142: this .page = "/logon.jsp";
143:
144: }
145:
146: }
|