View Javadoc

1   /**
2    * Copyright 2005-2008 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.control.action;
16  
17  import org.figure8.join.util.LogUtil;
18  import org.figure8.join.control.JoinAction;
19  import org.figure8.join.control.form.LoginForm;
20  import org.figure8.join.businessfacades.security.UserManager;
21  import org.figure8.join.view.UserView;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.struts.action.ActionForm;
25  import org.apache.struts.action.ActionForward;
26  import org.apache.struts.action.ActionMapping;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  /**
31   * Struts action used for logging in.
32   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
33   * @version $Revision: 1.3 $
34   *
35   * @struts.action path="/login" name="loginForm"
36   *                scope="request" parameter="op" validate="true"
37   *                input="/pages/mainpage.jsp?body=/jsp/login.jsp"
38   * @struts.action-forward name="Next" path="/jsp/login.jsp"
39   */
40  public class LoginAction extends JoinAction{
41  
42     // Static -------------------------------------------------------------------
43  
44     /** Get a commons logger. */
45     protected static Log log = LogUtil.getLog(LoginAction.class);
46  
47     /** Operation code for entering the application */
48     public static final String ENTER_OP = "enter";
49  
50  
51     // Attributes ---------------------------------------------------------------
52  
53     /** Join application user manager. */
54     protected UserManager userManager;
55  
56  
57     // Public -------------------------------------------------------------------
58  
59     /** @param manager <code>UserManager</code> implementation instance */
60     public void setUserManager(UserManager manager){
61        this.userManager = manager;
62     }
63  
64  
65     // Override of JoinAction ---------------------------------------------------
66  
67     /**
68      * Realize the login execution using the {@link UserManager} facade.
69      * @param operation String representing the operation to invoke on Action
70      * @param mapping Mapping between forwards name and path for this action
71      * @param form The form object containing request parameters
72      * @param request The servlet container request wrapper
73      * @param response The servlet container response wrapper
74      * @return A forward to the next view to render and display
75      * @throws Exception if application is not correctly initialized
76      */
77     public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form,
78                                    HttpServletRequest request, HttpServletResponse response)
79             throws Exception{
80        // Trace operation to execute.
81        log.debug("doExecute() called for logging in");
82  
83        if (ENTER_OP.equals(operation)){
84           if (form instanceof LoginForm){
85              LoginForm lForm = (LoginForm)form;
86  
87              try{
88                 // Retrieve view and store it into container.
89                 UserView view = userManager.login(lForm.getLogin(), lForm.getPassword());
90                 getUserContainer(request).setView(view);
91                 getUserContainer(request).getView().setClearPassword(lForm.getPassword());
92  
93                 // Forward to target url if specified.
94                 if (lForm.getTargetUrl() != null && lForm.getTargetUrl().length() > 0){
95                    // Remove context information if present.
96                    String targetUrl = lForm.getTargetUrl();
97                    int ctxPos = targetUrl.indexOf(request.getContextPath());
98                    if (ctxPos != -1)
99                       targetUrl = targetUrl.substring(ctxPos + request.getContextPath().length());
100                   return new ActionForward(targetUrl);
101                }
102             }
103             catch (Exception e){
104                // Log and put exception into request for feedback.
105                log.warn("User '" + lForm.getLogin() + "' fails to login: " + e.getMessage());
106                request.setAttribute("exception", e);
107             }
108          }
109       }
110       return mapping.findForward("Next");
111    }
112 }