KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > web > tag > CheckLoginTag


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: CheckLoginTag.java,v $
31  * Revision 1.3 2005/04/10 20:10:06 colinmacleod
32  * Added new themes.
33  * Changed id type to String.
34  * Changed i tag to em and b tag to strong.
35  * Improved PicoContainerFactory with NanoContainer scripts.
36  *
37  * Revision 1.2 2005/04/09 17:19:59 colinmacleod
38  * Changed copyright text to GPL v2 explicitly.
39  *
40  * Revision 1.1.1.1 2005/03/10 17:51:32 colinmacleod
41  * Restructured ivata op around Hibernate/PicoContainer.
42  * Renamed ivata groupware.
43  *
44  * Revision 1.3 2004/12/31 19:29:10 colinmacleod
45  * Added checking that the field writer factory is in application scope.
46  *
47  * Revision 1.2 2004/11/12 15:57:20 colinmacleod
48  * Removed dependencies on SSLEXT.
49  * Moved Persistence classes to ivata masks.
50  *
51  * Revision 1.1 2004/09/30 15:16:02 colinmacleod
52  * Split off addressbook elements into security subproject.
53  *
54  * Revision 1.4 2004/07/13 19:41:15 colinmacleod
55  * Moved project to POJOs from EJBs.
56  * Applied PicoContainer to services layer (replacing session EJBs).
57  * Applied Hibernate to persistence layer (replacing entity EJBs).
58  *
59  * Revision 1.3 2004/03/21 21:16:08 colinmacleod
60  * Shortened name to ivata op.
61  *
62  * Revision 1.2 2004/02/01 22:00:33 colinmacleod
63  * Added full names to author tags
64  *
65  * Revision 1.1.1.1 2004/01/27 20:57:56 colinmacleod
66  * Moved ivata openportal to SourceForge..
67  *
68  * Revision 1.1.1.1 2003/10/13 20:50:09 colin
69  * Restructured portal into subprojects
70  *
71  * Revision 1.4 2003/05/27 11:37:43 peter
72  * fixed: return added after dispatcher forward
73  *
74  * Revision 1.3 2003/05/27 10:31:03 peter
75  * fixed conditions for going back to login.jsp
76  *
77  * Revision 1.2 2003/03/04 19:07:14 colin
78  * moved charset stuff from head to checkLogin
79  *
80  * Revision 1.1 2003/02/24 19:33:33 colin
81  * moved to jsp
82  *
83  * Revision 1.10 2003/02/20 09:44:24 colin
84  * split off site checking code to checkSite
85  *
86  * Revision 1.9 2003/02/04 17:43:50 colin
87  * copyright notice
88  *
89  * Revision 1.8 2003/01/27 07:29:03 colin
90  * much simplified login checking (because of keepAlive.jsp)
91  *
92  * Revision 1.6 2003/01/23 13:58:06 colin
93  * modifications to allow URL rewriting sessions (instead of
94  * cookie)
95  *
96  * Revision 1.5 2002/10/21 13:51:24 jano
97  * name of file has changed
98  *
99  * Revision 1.4 2002/10/21 12:41:08 jano
100  * i forgot, we have diffrent name of site
101  *
102  * Revision 1.3 2002/10/21 11:24:21 jano
103  * check for file existing,
104  * if exist - display deploing message
105  *
106  * Revision 1.2 2002/09/30 15:32:46 colin
107  * bugfixes
108  *
109  * Revision 1.1 2002/09/26 08:16:19 colin
110  * first version of check login tag
111  * -----------------------------------------------------------------------------
112  */

113 package com.ivata.groupware.web.tag;
114
115 import javax.servlet.RequestDispatcher JavaDoc;
116 import javax.servlet.http.HttpServletRequest JavaDoc;
117 import javax.servlet.http.HttpServletResponse JavaDoc;
118 import javax.servlet.http.HttpSession JavaDoc;
119 import javax.servlet.jsp.JspException JavaDoc;
120 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
121
122 import org.apache.log4j.Logger;
123 import org.apache.struts.taglib.TagUtils;
124 import org.picocontainer.PicoContainer;
125
126 import com.ivata.groupware.admin.security.server.SecuritySession;
127 import com.ivata.groupware.admin.setting.Settings;
128 import com.ivata.mask.web.field.FieldWriterFactory;
129
130
131 /**
132  *
133  * <p>Check that the user is logged into the system and that the
134  * session has not timed. If the session <em>has</em> timed out, then
135  * this tag forwards to the login page, returning to the page where
136  * this tag was upon successful login</p>
137  *
138  * <p>This tag should appear as the first tag in each page where we
139  * want to confirm that the user is logged in. In particular, this tag
140  * should appear before any object is read from the session
141  * (user/settings).</p>
142  * <p><strong>Tag attributes:</strong><br/>
143  * <table cellpadding='2' cellspacing='5' border='0' align='center'
144  * width='85%'>
145  * <tr class='TableHeadingColor'>
146  * <th>attribute</th>
147  * <th>reqd.</th>
148  * <th>param. class</th>
149  * <th width='100%'>description</th>
150  * </tr>
151  * <tr class='TableRowColor'>
152  * <td>forward</td>
153  * <td>true</td>
154  * <td><code>String</code></td>
155  * <td>Stores the action forward we should pass control when the
156  * user logs back in after a timeout.</td>
157  * </tr>
158  * <tr class='TableRowColor'>
159  * <td>forwardFormName</td>
160  * <td>true</td>
161  * <td><code>String</code></td>
162  * <td>Specifies a form name to store while the user logs back in.
163  * The form is looked for in request and session scopes, in that
164  * order.</td>
165  * </tr>
166  * </table>
167  * </p>
168  *
169  * @since 2002-09-25
170  * @author Colin MacLeod
171  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
172  * @version $Revision: 1.3 $
173  */

174 public class CheckLoginTag extends TagSupport JavaDoc {
175     /**
176      * Refer to {@link Logger}.
177      */

178     private static Logger log = Logger.getLogger(CheckLoginTag.class);
179     /**
180      * <p>Stores the action forward we should pass control when the user
181      * logs back in after a timeout.</p>
182      */

183     private String JavaDoc forward = null;
184     /**
185      * <p>Value to be returned by <code>doEndTag()</code>.</p>
186      */

187     int endTagReturn = EVAL_PAGE;
188
189     /**
190      * <p>Checks the user is logged into the system and forwards to the
191      * login page if the user is not..</p>
192      *
193      * <p>This method is called when the JSP engine encounters the start
194      * tag, after the attributes are processed.<p>
195      *
196      * <p>Scripting variables (if any) have their values set here.</p>
197      *
198      * @return <code>SKIP_BODY</code> since there is no tag body to
199      * evaluate.
200      * @exception JspException if there is problem forwarding to the login
201      * page.
202      */

203     public int doStartTag() throws JspException JavaDoc {
204         // get the http session & request first of all
205
HttpSession JavaDoc session = pageContext.getSession();
206         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
207         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
208
209         // see if we have a user name and a settings object in the session
210
// if not, we will have to forward to the login page
211
SecuritySession securitySession = (SecuritySession)
212             session.getAttribute("securitySession");
213         FieldWriterFactory fieldWriterFactory = (FieldWriterFactory)
214             TagUtils.getInstance().lookup(pageContext,
215                 FieldWriterFactory.APPLICATION_ATTRIBUTE,
216                 "application");
217         if ((securitySession == null)
218                 || (fieldWriterFactory == null)) {
219             RequestDispatcher JavaDoc dispatcher = pageContext.getServletContext()
220                 .getRequestDispatcher("/loginGuest.action");
221
222             try {
223                 dispatcher.forward(request, pageContext.getResponse());
224                 return SKIP_BODY;
225             } catch (Exception JavaDoc e) {
226                 log.error("CheckLoginTag: could not forward to /loginGuest.action: "
227                     + e.getMessage(),
228                     e);
229             }
230         }
231         PicoContainer container = securitySession.getContainer();
232         Settings settings = (Settings) container.getComponentInstance(Settings.class);
233         // indicates that the body should not be evaluated - this tag has no body
234
return SKIP_BODY;
235     }
236
237     /**
238      * <p>Get the action forward we should pass control when the user logs
239      * back in after a timeout.</p>
240      *
241      * @return the current value of forward to pass control to after a
242      * login.
243      */

244     public final String JavaDoc getForward() {
245         return forward;
246     }
247
248     /**
249      * <p>Set the action forward we should pass control when the user logs
250      * back in after a timeout.</p>
251      *
252      * @param forward new value of forward to pass control to after a
253      * login.
254      */

255     public final void setForward(final String JavaDoc forward) {
256         this.forward = forward;
257     }
258
259     /**
260      * <p>We decide here if we should display the rest of the page or not.
261      * If the session times out, we display the contents of the login
262      * page
263      * instead. This method is called after the JSP engine finished
264      * processing
265      * the tag.</p>
266      *
267      * @exception JspException encapsulates any exception when calling
268      * <code>out.println</code>
269      * @return <code>EVAL_PAGE</code> if we want to evaluate
270      * the page after this tag, otherwise <code>SKIP_PAGE</code>.
271      */

272     public int doEndTag() throws JspException JavaDoc {
273         // return value set in doStartTag
274
return endTagReturn;
275     }
276 }
277
Popular Tags