KickJava   Java API By Example, From Geeks To Geeks.

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


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>create</b>, used to create a new Yazd User account.
119  * <p>
120  * Requires that attribute <b>id</b> be set to the name of a
121  * script variable for later use in JSP to retrieve User data
122  * using &lt;jsp:getProperty/&gt;.
123  * <p>
124  * If optional tag attribute <b>confirm</b>="true" users must also enter
125  * the password a second time to confirm their password change.
126  * <p>
127  * If optional tag attribute <b>password</b>="true" users will have a
128  * password generated for them automatically.
129  * <p>
130  * If optional tag attribute <b>login</b>="true" users will automatically
131  * be logged in if account is created.
132  * <p>
133  * If the create succeeds, includes the body of the create tag and
134  * user is authorized as if they had logged in.
135  * <p>
136  * The authorize tag must be used in the JSP prior to using this tag
137  * so that YazdState and YazdRequest are initialized.
138  * <p>
139  * Uses the the following HTTP input parameters
140  * <p><ul>
141  * <li><b>create</b> - name of submit button to use with HTML input form
142  * <li><b>email</b> - users email address
143  * <li><b>username</b> - users username (userid)
144  * <li><b>password</b> - password
145  * <li><b>confirm</b> - confirm password
146  * <li><b>name</b> - users real name
147  * <li><b>nameVisible</b> - check box returning a value if real name should be visi
148 ble
149  * <li><b>emailVisible</b> - check box returning a value if email address should be
150  visible
151  * </ul>
152  * <p>
153  * Plus any additional parameters as specified in yazd.user.properties file.
154  * <p>
155  * Sets one or more user errors if account creation fail.
156  * <p>
157  * JSP Tag Lib Descriptor
158  * <p><pre>
159  * &lt;name&gt;create&lt;/name&gt;
160  * &lt;tagclass&gt;com.Yasna.forum.tags.CreateTag&lt;/tagclass&gt;
161  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
162  * &lt;info&gt;Includes body of tag if create is successful.&lt;/info&gt;
163  * &lt;attribute&gt;
164  * &lt;name&gt;id&lt;/name&gt;
165  * &lt;required&gt;true&lt;/required&gt;
166  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
167  * &lt;/attribute&gt;
168  * &lt;attribute&gt;
169  * &lt;name&gt;confirm&lt;/name&gt;
170  * &lt;required&gt;false&lt;/required&gt;
171  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
172  * &lt;/attribute&gt;
173  * &lt;attribute&gt;
174  * &lt;name&gt;password&lt;/name&gt;
175  * &lt;required&gt;false&lt;/required&gt;
176  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
177  * &lt;/attribute&gt;
178  * &lt;attribute&gt;
179  * &lt;name&gt;login&lt;/name&gt;
180  * &lt;required&gt;false&lt;/required&gt;
181  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
182  * &lt;/attribute&gt;
183  * </pre>
184  *
185  * @see AuthorizeTag
186  * @see TagPropertyManager
187  * @see YazdProperty
188  * @see GetYazdPropertyTag
189  * @see SetYazdPropertyTag
190  * @see YazdState
191  * @see YazdRequest
192  *
193  * @see ErrorTag
194  * @see ErrorLoopTag
195  *
196  * @author Glenn Nielsen
197  */

198
199 public class CreateTag extends TagSupport implements YazdProperty
200 {
201   private Authorization auth = null;
202   private YazdState js = null;
203   private YazdRequest jr = null;
204   private User user = null;
205   // Required input Parameters
206
private String JavaDoc email = null;
207   private String JavaDoc username = null;
208   private String JavaDoc password = null;
209   // Optional input parameters
210
private String JavaDoc confirm = null;
211   private String JavaDoc name = null;
212   private String JavaDoc nameVisible = null;
213   private String JavaDoc emailVisible = null;
214   // Extended properties of User as configured in yazd.user.properties
215
private Map properties = new HashMap();
216   // Required extended properties of User as configured in yazd.user.properties
217
private Map required = new HashMap();
218   // Flag that we must confirm password if user is setting new password
219
// Set by the account tag confirm attribute
220
private boolean confirm_password = false;
221   // Flag that password will be generated for user
222
// Set by the account tag password attribute
223
private boolean generate_password = false;
224   // Flag that logs in user if account created
225
// Set by the account tag login attribute
226
private boolean auto_login = false;
227   // Flag indicating that account was created
228
private boolean created = false;
229
230   /**
231    * Creates User account if this is an HTML form submission.
232    *
233    * @throws JspException on system level error
234    *
235    * @return <b>SKIP_BODY</b> if a create form was not submitted or create account failed, <b>EVAL_BODY_INCLUDE</b> if account creation succeeded
236    */

237
238   public final int doStartTag() throws JspException
239   {
240     boolean extended_required = true;
241
242     // Get the user state information
243
js = (YazdState)pageContext.getAttribute("yazdUserState",
244         PageContext.SESSION_SCOPE);
245     if( js == null ) {
246       throw new JspException("Yazd create tag could not get yazd state.");
247     }
248
249     // Get the user request information
250
jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest",
251                 PageContext.REQUEST_SCOPE);
252     if( js == null ) {
253       throw new JspException("Yazd create tag could not get yazd request.");
254     }
255
256     // Save the script variable so JSP author can access user data
257
pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
258
259     // Get names and default values for any extended user properties defined
260
// in yazd.user.properties
261
ServletRequest req = pageContext.getRequest();
262     Enumeration enume = TagPropertyManager.getUserPropertyNames();
263     String JavaDoc tmp;
264     String JavaDoc prop;
265     String JavaDoc propname;
266     while( enume.hasMoreElements() ) {
267       prop = (String JavaDoc)enume.nextElement();
268       // Set the default value
269
if( prop.startsWith( "required." ) ) {
270         propname = prop.substring(9);
271         required.put(propname,TagPropertyManager.getUserProperty(prop));
272       } else {
273         properties.put(prop,TagPropertyManager.getUserProperty(prop));
274       }
275     }
276
277     // See if this is a form submission
278
tmp = req.getParameter("create");
279     if( tmp == null || tmp.length() == 0 ) {
280       // User didn't submit a form, and we are done initializing data
281
// Skip body of create tag since account hasn't been created yet
282
return SKIP_BODY;
283     }
284
285     // User has submitted a page requesting an account be created
286

287     // Harvest the standard user data input parameters from page
288
email = req.getParameter("email");
289     username = req.getParameter("username");
290     password = req.getParameter("password");
291     confirm = req.getParameter("confirm");
292     name = req.getParameter("name");
293     nameVisible = req.getParameter("nameVisible");
294     emailVisible = req.getParameter("emailVisible");
295
296     // Harvest the input parameters for any required extended properties
297
for( Iterator it=required.keySet().iterator(); it.hasNext(); ) {
298       prop = (String JavaDoc)it.next();
299       if( (tmp = req.getParameter(prop)) != null && tmp.length() > 0 ) {
300         required.put(prop,tmp);
301       } else {
302         required.put(prop,"");
303         extended_required = false;
304       }
305     }
306
307     // Harvest the input parameters for any optional extended properties
308
for( Iterator it=properties.keySet().iterator(); it.hasNext(); ) {
309       prop = (String JavaDoc)it.next();
310       if( (tmp = req.getParameter(prop)) != null ) {
311         properties.put(prop,tmp);
312       }
313     }
314
315     // Validate required fields, error string comes from yazd.tag.properties
316
if( email == null || email.length() == 0 || !extended_required ||
317     username == null || username.length() == 0 ||
318         ((password == null || password.length() == 0) && !generate_password) ) {
319         jr.addError(TagPropertyManager.getTagProperty("yazd.tag.create.required"));
320         return SKIP_BODY;
321     }
322
323     // Validate password confirmation if confirm is true.
324
// Error string comes from yazd.tag.properties
325
if( confirm_password && (confirm == null || !password.equals(confirm)) ) {
326         password = null;
327         confirm = null;
328         jr.addError(TagPropertyManager.getTagProperty("yazd.tag.create.confirm_password"));
329         return SKIP_BODY;
330     }
331
332     // Generate a password for user if requested
333
if( generate_password ) {
334       password = jr.GeneratePassword();
335       properties.put("password",password);
336     }
337
338     // Try to create the new user account
339
ForumFactory ff = jr.getForumFactory();
340     ProfileManager pm = jr.getProfileManager();
341     try {
342       user = pm.createUser( username, password, email );
343     } catch( UserAlreadyExistsException e ) {
344       // userid is already in use, user has to try again
345
jr.addError(TagPropertyManager.getTagProperty("yazd.tag.create.userexists"));
346       username = null;
347       return SKIP_BODY;
348     }
349
350     // new user account created, save user data
351
try {
352       // Save standard user data
353
if( name != null && name.length() > 0 ) {
354         user.setName( name );
355       }
356       if( nameVisible != null && nameVisible.length() > 0) {
357         user.setNameVisible( true );
358       } else {
359         user.setNameVisible( false );
360       }
361       if( emailVisible != null && emailVisible.length() > 0 ) {
362         user.setEmailVisible( true );
363       } else {
364         user.setEmailVisible( false );
365       }
366
367       // Save any extended user properties
368
// thread_depth, message_depth, and items_per_page are special
369
// extended properties used for display preferences
370
for( Iterator it=properties.keySet().iterator(); it.hasNext(); ) {
371     tmp = (String JavaDoc)it.next();
372         user.setProperty( tmp, (String JavaDoc)properties.get(tmp));
373         if( tmp.equals(YazdState.MESSAGE_DEPTH) )
374       js.setMessageDepth(Integer.valueOf((String JavaDoc)properties.get(tmp)).intValue());
375         if( tmp.equals(YazdState.THREAD_DEPTH) )
376           js.setThreadDepth(Integer.valueOf((String JavaDoc)properties.get(tmp)).intValue());
377         if( tmp.equals(YazdState.ITEMS_PER_PAGE) )
378           js.setItemsPerPage(Integer.valueOf((String JavaDoc)properties.get(tmp)).intValue());
379       }
380
381       // Save any required extended user properties
382
// thread_depth, message_depth, and items_per_page are special
383
// extended properties used for display preferences
384
for( Iterator it=required.keySet().iterator(); it.hasNext(); ) {
385         tmp = (String JavaDoc)it.next();
386         user.setProperty( tmp, (String JavaDoc)required.get(tmp));
387         if( tmp.equals(YazdState.MESSAGE_DEPTH) )
388           js.setMessageDepth(Integer.valueOf((String JavaDoc)required.get(tmp)).intValue());
389         if( tmp.equals(YazdState.THREAD_DEPTH) )
390           js.setThreadDepth(Integer.valueOf((String JavaDoc)required.get(tmp)).intValue());
391         if( tmp.equals(YazdState.ITEMS_PER_PAGE) )
392           js.setItemsPerPage(Integer.valueOf((String JavaDoc)required.get(tmp)).intValue());
393       }
394       // Finally, get authorization for new user account
395
auth = AuthorizationFactory.getAuthorization( username, password );
396     } catch(UnauthorizedException e) {
397       throw new JspException("Create tag not authorized to set user values.");
398     }
399
400     // Account is now created
401
created = true;
402
403     // See if user should be automatically logged in
404
if( auto_login ) {
405       // User is now authorized as if they had logged in
406
js.setAuthorization(auth);
407       js.setLoggedIn(true);
408     }
409
410     // All done, the body of the create tag can now redirect the
411
// user some where else if needed.
412
return EVAL_BODY_INCLUDE;
413   }
414
415   /**
416    * Method called at end of create Tag
417    *
418    * @return <b>SKIP_PAGE</b> if new user account created, <b>EVAL_PAGE</b> if user account has not been created yet.
419    */

420
421   public final int doEndTag() throws JspException
422   {
423     if( created )
424       return SKIP_PAGE;
425     return EVAL_PAGE;
426   }
427
428   /**
429    * Set a flag indicating that password changes should be confirmed
430    * (Optional attribute).
431    */

432   public final void setConfirm(String JavaDoc name)
433   {
434     if( name.equals("true") )
435       confirm_password = true;
436   }
437
438   /**
439    * Set a flag indicating that password will be generated for user
440    * (Optional attribute).
441    */

442   public final void setPassword(String JavaDoc name)
443   {
444     if( name.equals("true") )
445       generate_password = true;
446   }
447
448   /**
449    * Set a flag indicating that user should be logged in once account
450    * is created
451    * (Optional attribute).
452    */

453   public final void setLogin(String JavaDoc name)
454   {
455     if( name.equals("true") )
456       auto_login = true;
457   }
458
459   // Get methods for standard user data which is made
460
// availabe to JSP author via a script variable
461

462   /**
463    * User Email address property which can be obtained by the JSP page
464    * using &lt;jsp:getProperty name=<i>"id"</i> property="email"/&gt;
465    *
466    * @return String - user email address
467    */

468   public final String JavaDoc getEmail()
469   {
470     if( email == null )
471       return "";
472     return email;
473   }
474
475   /**
476    * User Username (userid) property which can be obtained by the JSP page
477    * using &lt;jsp:getProperty name=<i>"id"</i> property="username"/&gt;
478    *
479    * @return String - user username (forum userid)
480    */

481   public final String JavaDoc getUsername()
482   {
483     if( username == null )
484       return "";
485     return username;
486   }
487
488   /**
489    * Password entered in previous HTML form submisson which can be
490    * obtained by the JSP page using
491    * &lt;jsp:getProperty name=<i>"id"</i> property="password"/&gt;
492    *
493    * @return String - password the user entered in the HTML form submitted
494    */

495   public final String JavaDoc getPassword()
496   {
497     if( password == null )
498       return "";
499     return password;
500   }
501
502   /**
503    * Confirm Password entered in previous HTML form submisson which can be
504    * obtained by the JSP page using
505    * &lt;jsp:getProperty name=<i>"id"</i> property="confirm"/&gt;
506    *
507    * @return String - confirm password the user entered in the HTML form submitted
508    */

509   public final String JavaDoc getConfirm()
510   {
511     if( confirm == null )
512       return "";
513     return confirm;
514   }
515
516   /**
517    * User Name (real name) property which can be obtained by the JSP page
518    * using &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
519    *
520    * @return String - user name (real name)
521    */

522   public final String JavaDoc getName()
523   {
524     if( name == null )
525       return "";
526     return name;
527   }
528
529   /**
530    * User NameVisible (real name) property which can be obtained by the
531    * JSP Page using &lt;jsp:getProperty name=<i>"id"</i> property="nameVisible"/&gt;
532    *
533    * @return checkbox value if user real name should be visible.
534    */

535   public final String JavaDoc getNameVisible()
536   {
537     if( nameVisible != null )
538       return "checked";
539     return "";
540   }
541
542   /**
543    * User EmailVisible (email address) property which can be obtained by the
544    * JSP Page using &lt;jsp:getProperty name=<i>"id"</i> property="emailVisible"/&gt;
545    *
546    * @return checkbox value if user email address should be visible.
547    */

548   public final String JavaDoc getEmailVisible()
549   {
550     if( emailVisible != null )
551       return "checked";
552     return "";
553   }
554
555   /**
556    * Method used by the getYazdProperty tag to get an extended User
557    * property from the create tag script variable.
558    *
559    * @return String - value of the property
560    */

561   public final String JavaDoc getProperty(String JavaDoc name)
562   {
563     String JavaDoc tmp = (String JavaDoc)properties.get(name);
564     if( tmp != null )
565       return tmp;
566     tmp = (String JavaDoc)required.get(name);
567     if( tmp != null )
568       return tmp;
569     return "";
570   }
571
572   /**
573    * Method used by the setYazdProperty tag to set an extended User
574    * property from the create tag script variable.
575    */

576   public final void setProperty(String JavaDoc name, String JavaDoc value)
577   {
578     user.setProperty(name,value);
579     if( properties.containsKey(name) )
580       properties.put(name,value);
581     else if( required.containsKey(name) )
582       properties.put(name,value);
583   }
584 }
585
Popular Tags