KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > struts > actions > UserAction


1 package hero.struts.actions;
2
3 import java.io.IOException JavaDoc;
4 import javax.servlet.ServletException JavaDoc;
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import javax.servlet.http.HttpServletResponse JavaDoc;
7 import org.apache.struts.action.ActionError;
8 import org.apache.struts.action.ActionErrors;
9 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionMapping;
11 import org.apache.struts.action.ActionForward;
12
13 import hero.struts.forms.*;
14 import hero.interfaces.*;
15
16 /**
17  * <strong>UserAction</strong>
18  * Action that allows the admin to add new users
19  *
20  *@author Miguel Valdes Faura
21  */

22
23 public class UserAction extends AbstStrutsActionBase
24 {
25   public boolean authenticate(String JavaDoc username, String JavaDoc password)
26   {
27     return(true);
28   }
29     /**
30      * @param mapping The ActionMapping used to select this instance
31      * @param actionForm The optional AbstActionFormBase bean for this request (if any)
32      * @param request The HTTP request we are processing
33      * @param response The HTTP response we are creating
34      * @exception IOException if an input/output error occurs
35      * @exception ServletException if a servlet exception occurs
36      */

37     public ActionForward perform(ActionMapping mapping, ActionForm form,
38         HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
39     {
40         ActionForward actionForward;
41         // Create the container for any errors that occur
42
ActionErrors errors = new ActionErrors();
43
44     String JavaDoc logged = request.getParameter("user");
45     actionForward = mapping.findForward(NEWUSER);
46
47     try
48     {
49       if (!isCancelled(request))
50       {
51           UserForm userForm = (UserForm) form;
52           String JavaDoc name = userForm.getName();
53           String JavaDoc password = userForm.getPassword();
54           String JavaDoc email = userForm.getEmail();
55           String JavaDoc jabber = userForm.getJabber();
56
57           if (name.length()!=0 && password.length()!=0 && email.length()!=0)
58           {
59           hero.interfaces.UserRegistrationLocalHome userh = (UserRegistrationLocalHome)hero.interfaces.UserRegistrationUtil.getLocalHome();
60           hero.interfaces.UserRegistrationLocal user = userh.create();
61
62           if (jabber.length()!=0)
63               user.userCreate(name,password,email,jabber);
64           else
65               user.userCreate(name,password,email);
66
67           request.getSession(true).setAttribute("newuser", "true");
68           request.getSession(true).setAttribute("newusername", name);
69           }
70           else
71           {
72           errors.add("user_error", new ActionError("error.user.mismatch"));
73           actionForward = mapping.findForward(USERNOTLOGGED);
74           }
75       }
76       else
77           actionForward = mapping.findForward(LOGIN);
78     }catch(Exception JavaDoc e){e.printStackTrace();errors.add("user_error", new ActionError("error.user"));
79     if (logged.equals("notLogged"))
80         actionForward = mapping.findForward(USERNOTLOGGED);
81     else
82         actionForward = mapping.findForward(USER);}
83
84     if (!errors.empty()) {
85         saveErrors(request, errors);
86         }
87     
88     // Forward control to the appropriate URI as determined by the action.
89
return (actionForward);
90     }
91 }
92
Popular Tags