View Javadoc

1   package org.sourceforge.vlibrary.security.actions;
2   
3   import java.util.ArrayList;
4   import javax.servlet.http.HttpServletRequest;
5   import javax.servlet.http.HttpServletResponse;
6   import org.apache.struts.action.ActionForm;
7   import org.apache.struts.action.ActionForward;
8   import org.apache.struts.action.ActionMapping;
9   import org.apache.struts.util.MessageResources;
10  import org.apache.log4j.Logger;
11  
12  import org.sourceforge.vlibrary.Constants;
13  import org.sourceforge.vlibrary.exceptions.LibraryException;
14  import org.sourceforge.vlibrary.security.forms.AdminPasswordResetForm;
15  import org.sourceforge.vlibrary.security.forms.PasswordResetForm;
16  import org.sourceforge.vlibrary.user.exceptions.ReaderNotFoundException;
17  import org.sourceforge.vlibrary.user.actions.LibraryAction;
18  
19  /**
20   * Action supporting user password reset
21   *
22   * @version $Revision$ $Date$
23   */
24  
25  public class PasswordResetAction extends LibraryAction {
26      
27      /** log4j Logger */
28      private static Logger logger =
29       Logger.getLogger(PasswordResetAction.class.getName());
30      
31      /**
32       * Perform password reset action
33       *
34       * @param mapping ActionMapping used to select this instance
35       * @param form  TransactionForm associated with this requewst
36       * @param request HTTP request
37       * @param response HTTP response
38       * @param messages message resources
39       *
40       * @exception Exception
41       */
42      public ActionForward executeAction(ActionMapping mapping, ActionForm form,
43       HttpServletRequest request, HttpServletResponse response,
44       MessageResources messages) throws Exception {
45          ArrayList errors = new ArrayList();
46          
47          /*
48           * If this is admin pwd reset, get uid from post; otherwise set uid =
49           * remote user id
50           */
51          String uid = null;
52          String newPwd = null;
53          
54          if (mapping.getPath().startsWith(Constants.ADMIN_PATH)) {
55              AdminPasswordResetForm frm = (AdminPasswordResetForm) form;
56              newPwd = frm.getPwd();
57              uid = frm.getUid();
58          } else {
59              PasswordResetForm frm = (PasswordResetForm) form;
60              newPwd = frm.getPwd();
61              uid = request.getRemoteUser();
62          }
63          
64          try {
65              logger.info(messages.getMessage("reader.resetting.password", uid));
66              
67              // Verify that the reader exists, since the LibraryManager
68              // resetPassword will not throw in that case
69              if (libraryManager.retrieveByUid(uid) == null) {
70                  String errMsg = messages.getMessage(
71                   "error.nonexistent.reader", uid);
72                  logger.error(errMsg);
73                  throw new ReaderNotFoundException(errMsg);
74              }
75              
76              libraryManager.resetPassword(uid, newPwd);
77              logger.info(messages.getMessage("reader.password.reset.successful",
78               uid));
79          } catch (Throwable t) {
80              String errString = messages.getMessage("error.password.reset", uid);
81              logger.error(errString, t);
82              errors.add(new LibraryException(errString, t));
83          }
84          
85          return standardForward(mapping, request, errors);
86      }
87  }