KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > web > struts > action > signon > LoginAction


1 package xpetstore.web.struts.action.signon;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionForward;
8 import org.apache.struts.action.ActionMapping;
9
10 import xpetstore.domain.customer.ejb.Customer;
11 import xpetstore.domain.signon.ejb.Account;
12
13 import xpetstore.services.petstore.ejb.Petstore;
14
15 import xpetstore.web.struts.action.BaseAction;
16
17
18 /**
19  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
20  *
21  * @struts.action
22  * name="signonForm"
23  * path="/login"
24  * input="/login.jsp"
25  * scope="request"
26  * validate="true"
27  *
28  * @struts.action-forward
29  * name="success"
30  * path="/index.jsp"
31  *
32  * @struts.action-forward
33  * name="error"
34  * path="/signon.jsp"
35  */

36 public class LoginAction
37     extends BaseAction
38 {
39     //~ Methods ----------------------------------------------------------------
40

41     public ActionForward doExecute( ActionMapping mapping,
42                                     ActionForm form,
43                                     HttpServletRequest JavaDoc request,
44                                     HttpServletResponse JavaDoc response )
45         throws Exception JavaDoc
46     {
47         SignonForm frm = ( SignonForm ) form;
48         Account account = frm.getAccount( );
49         String JavaDoc redirectUri = frm.getRedirectUri( );
50         Petstore petstore = getPetstore( );
51
52         if ( petstore.authenticate( account.getUserId( ), account.getPassword( ) ) )
53         {
54             Customer cust = petstore.getCustomer( account.getUserId( ) );
55             initSession( cust, request );
56
57             if ( ( redirectUri == null ) || ( redirectUri.length( ) == 0 ) )
58             {
59                 return mapping.findForward( SUCCESS );
60             }
61             else
62             {
63                 _log.info( "...redirecting to: " + redirectUri );
64                 response.sendRedirect( redirectUri );
65                 return null;
66             }
67         }
68         else
69         {
70             request.setAttribute( MESSAGE_KEY, getString( "authentication_failed" ) );
71
72             return mapping.findForward( ERROR );
73         }
74     }
75 }
76
Popular Tags