KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > tags > LoginTag


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum.tags;
108
109 import java.util.*;
110 import javax.servlet.*;
111 import javax.servlet.jsp.*;
112 import javax.servlet.jsp.tagext.*;
113 import javax.servlet.http.*;
114 import com.Yasna.forum.*;
115 import com.Yasna.forum.tags.*;
116
117 /**
118  * JSP Tag <b>login</b>, used to login a Yazd user.
119  * <p>
120  * Uses the following HTTP input parameters
121  * <p><ul>
122  * <li><b>login</b> - indicates user is submitting the login form
123  * <li><b>username</b> - Yazd user name (userid) to login
124  * <li><b>password</b> - users password
125  * </ul>
126  * If login fails a user error is set, if login succeeds the body of the
127  * login tag is included and the remainder of the page is skipped.
128  * <p>
129  * Initializes YazdState and YazdRequest if necessary.
130  * <p>
131  * JSP Tag Lib Descriptor
132  * <p><pre>
133  * &lt;name&gt;login&lt;/name&gt;
134  * &lt;tagclass&gt;com.Yasna.forum.tags.LoginTag&lt;/tagclass&gt;
135  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
136  * &lt;info&gt;Login user using username and password. Includes body of tag if login succeeded.&lt;/info&gt;
137  * </pre>
138  *
139  * @see YazdState
140  * @see YazdRequest
141  *
142  * @see ErrorTag
143  * @see ErrorLoopTag
144  *
145  * @author Glenn Nielsen
146  */

147 public class LoginTag extends TagSupport
148 {
149   private Authorization auth = null;
150   private YazdState js = null;
151   private YazdRequest jr = null;
152
153   /**
154    * Attempts a user login if this is an HTML form submission.
155    * <p>
156    * If login succeeds, reads in user data and initializes state information
157    * <p>
158    * Includes body of tag if user is already logged in, or a new login
159    * is successful.
160    *
161    * @throws JspException on system level error
162    *
163    * @return <b>SKIP_BODY</b> if a login form was not submitted or login failed, <b>EVAL_BODY_INCLUDE</b> if login succeeded
164    */

165   public final int doStartTag() throws JspException
166   {
167     // Initialize YazdState
168
js = (YazdState)pageContext.getAttribute("yazdUserState",
169         PageContext.SESSION_SCOPE);
170     if( js == null ) {
171       js = new YazdState();
172       pageContext.setAttribute("yazdUserState",js,PageContext.SESSION_SCOPE);
173     }
174
175     if( js.getLoggedIn() ) {
176       return EVAL_BODY_INCLUDE;
177     }
178
179     // Initialize YazdRequest
180
jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest",
181                 PageContext.REQUEST_SCOPE);
182     if( jr == null ) {
183       jr = new YazdRequest();
184       jr.setYazdState(js);
185       pageContext.setAttribute("yazdUserRequest",jr,PageContext.REQUEST_SCOPE);
186     }
187
188     // Get username and password
189
ServletRequest req = pageContext.getRequest();
190     String JavaDoc tmp = req.getParameter("login");
191     if( tmp == null || tmp.length() == 0 )
192       return SKIP_BODY;
193     String JavaDoc u = req.getParameter("username");
194     String JavaDoc p = req.getParameter("password");
195     if( p.length() == 0 || u.length() == 0) {
196       jr.addError(TagPropertyManager.getTagProperty("yazd.tag.login.failed"));
197       return SKIP_BODY;
198     }
199     try {
200       auth = AuthorizationFactory.getAuthorization( u, p );
201     }
202     catch( UnauthorizedException ue ) {
203       jr.addError(TagPropertyManager.getTagProperty("yazd.tag.login.failed"));
204       return SKIP_BODY;
205     }
206
207     // Set user message_depth, thread_depth, and items_per_page
208
ProfileManager pm = jr.getProfileManager();
209     User user = null;
210     try {
211       user = pm.getUser(auth.getUserID());
212     } catch( UserNotFoundException ex ) {
213       throw new JspException("Yazd login tag could not find user.");
214     }
215
216     // Get the users display preferences
217
long val;
218     tmp = user.getProperty(YazdState.MESSAGE_DEPTH);
219     if( tmp != null ) {
220       try {
221         val = Long.valueOf(tmp).longValue();
222         js.setMessageDepth((int)val);
223       } catch(NumberFormatException JavaDoc e) {
224       }
225     }
226     tmp = user.getProperty(YazdState.THREAD_DEPTH);
227     if( tmp != null ) {
228       try {
229         val = Long.valueOf(tmp).longValue();
230         js.setThreadDepth((int)val);
231       } catch(NumberFormatException JavaDoc e) {
232       }
233     }
234     tmp = user.getProperty(YazdState.ITEMS_PER_PAGE);
235     if( tmp != null ) {
236       try {
237         val = Long.valueOf(tmp).longValue();
238         js.setItemsPerPage((int)val);
239       } catch(NumberFormatException JavaDoc e) {
240       }
241     }
242
243     // Reset the users last visit to Yazd
244
tmp = user.getProperty(YazdState.LAST_VISIT);
245     if( tmp != null ) {
246       try {
247         val = Long.valueOf(tmp).longValue();
248         js.setLastVisit(new Date(val));
249       } catch(NumberFormatException JavaDoc e) {
250       }
251     }
252
253     // Save the user state information
254
js.setAuthorization(auth);
255
256     // Reset the users lastForumVisit from user extended properties
257
js.resetLastForumVisitDate(jr);
258
259     js.setLoggedIn(true);
260     return EVAL_BODY_INCLUDE;
261   }
262
263   /**
264    * Determine if login was successful.
265    *
266    * @return EVAL_PAGE if login failed, SKIP_PAGE if login succeeded
267    */

268   public final int doEndTag() throws JspException
269   {
270     if( js.getLoggedIn() )
271       return SKIP_PAGE;
272     return EVAL_PAGE;
273   }
274 }
275
Popular Tags