KickJava   Java API By Example, From Geeks To Geeks.

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


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.HttpSession JavaDoc;
7 import javax.servlet.http.HttpServletResponse JavaDoc;
8 import org.apache.struts.action.ActionError;
9 import org.apache.struts.action.ActionErrors;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionMapping;
12 import org.apache.struts.action.ActionForward;
13 import hero.interfaces.*;
14
15 /**
16  * <strong>LoginAction</strong>
17  * Action that performs users authentication and forward activity or project
18  * information
19  *
20  *@author Miguel Valdes Faura
21  */

22 public class LoginAction extends AbstStrutsActionBase
23 {
24     /**
25      *@param String containing username
26      *@param String containing password
27      *@return boolean - true authenticated, false not authenticated.
28      */

29     public boolean authenticate(String JavaDoc username, String JavaDoc password)
30     {
31         return(true);
32     }
33
34     /**
35      * @param mapping The ActionMapping used to select this instance
36      * @param actionForm The optional AbstActionFormBase bean for this request (if any)
37      * @param request The HTTP request we are processing
38      * @param response The HTTP response we are creating
39      * @exception IOException if an input/output error occurs
40      * @exception ServletException if a servlet exception occurs
41      */

42
43     public ActionForward perform(ActionMapping mapping, ActionForm form,
44         HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
45     {
46         // Assume that the login fails, so that the login page is redisplayed
47
// if the user isn't authenticated
48
ActionForward actionForward = mapping.findForward(LOGIN);
49         // Create the container for any errors that occur
50
ActionErrors errors = new ActionErrors();
51         
52     // Extract attributes and parameters we will need from the incoming
53
// form.
54

55     HttpSession JavaDoc session = request.getSession();
56     String JavaDoc action = (String JavaDoc)session.getAttribute("action");
57
58     try {
59
60         hero.interfaces.UserSessionLocalHome userh = (UserSessionLocalHome)hero.interfaces.UserSessionUtil.getLocalHome();
61         hero.interfaces.UserSessionLocal usersession = userh.create();
62
63         request.getSession(true).setAttribute("username", usersession.getUser());
64         request.getSession(true).setAttribute("password", usersession.getUserPassword());
65             request.getSession(true).setAttribute("workElement", "null");
66         
67         if (action.equals("user"))
68         {
69         request.getSession(true).setAttribute("user", usersession);
70
71         String JavaDoc project = (String JavaDoc)session.getAttribute("project");
72
73         if (project.equals("clone"))
74         {
75             // Forward control to the specified 'success' URI specified in the structs-config.xml
76
actionForward = mapping.findForward(CLONEPROJECT);
77         }
78         if (project.equals("details"))
79         {
80             // Forward control to the specified 'success' URI specified in the structs-config.xml
81
actionForward = mapping.findForward(PROJECTDETAILS);
82         }
83         }
84         else
85         {
86           try{
87                  String JavaDoc project = (String JavaDoc)session.getAttribute("projectname");
88
89                  hero.interfaces.ProjectSessionLocalHome projecth = (ProjectSessionLocalHome)hero.interfaces.ProjectSessionUtil.getLocalHome();
90                  hero.interfaces.ProjectSessionLocal projectsession = projecth.create();
91                  projectsession.initProject(project);
92
93                 if (action.equals("node"))
94         {
95             String JavaDoc name = (String JavaDoc)session.getAttribute("nodename");
96             String JavaDoc username = (String JavaDoc)session.getAttribute("username");
97                     hero.util.StrutsNodeValue node = projectsession.getStrutsNode(name);
98             request.getSession(true).setAttribute("node", node);
99
100             if (!node.getDeadline().equals("Deadline needed"))
101             {
102             java.sql.Timestamp JavaDoc deadline = java.sql.Timestamp.valueOf(node.getDeadline()+" 00:00:00");
103             if (deadline.getTime() <= (new java.util.Date JavaDoc()).getTime() && !node.getState().equals("TERMINATED"));
104                 errors.add("deadline_error", new ActionError("error.deadline"));
105             }
106
107             request.getSession(true).setAttribute("project", projectsession);
108             request.getSession(true).setAttribute("proAct", "false");
109             request.getSession(true).setAttribute("edit", "false");
110                 
111             // Forward control to the specified 'success' URI specified in the structs-config.xml
112
actionForward = mapping.findForward(ACTIVITY);
113         }
114         if (action.equals("project"))
115         {
116             request.getSession(true).setAttribute("project", projectsession);
117             request.getSession(true).setAttribute("proAct", "true");
118             request.getSession(true).setAttribute("edit", "false");
119
120             request.getSession(true).setAttribute("nodes", projectsession.getStrutsNodes());
121             request.getSession(true).setAttribute("edges", projectsession.getStrutsEdges());
122
123             
124             // Forward control to the specified 'success' URI specified in the structs-config.xml
125
actionForward = mapping.findForward(PROJECT);
126         }
127           }catch(Exception JavaDoc pe){actionForward = mapping.findForward(INITIAL);errors.add("project_error", new ActionError("error.project.mismatch"));}
128         }
129     }catch(Exception JavaDoc e){errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.password.mismatch"));}
130     
131     // If any messages is required, save the specified error messages keys
132
// into the HTTP request for use by the <struts:errors> tag.
133
if (!errors.empty()) {
134             saveErrors(request, errors);
135         }
136     
137     // Forward control to the appropriate URI as determined by the action.
138
return (actionForward);
139     }
140 }
141
Popular Tags