View Javadoc

1   /*
2    * Copyright 1999-2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.struts.webapp.example2;
19  
20  
21  import java.lang.reflect.InvocationTargetException;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpSession;
26  import org.apache.commons.beanutils.PropertyUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.struts.action.Action;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  
34  
35  /***
36   * Implementation of <strong>Action</strong> that populates an instance of
37   * <code>RegistrationForm</code> from the profile of the currently logged on
38   * User (if any).
39   *
40   * @author Craig R. McClanahan
41   * @version $Rev: 421494 $ $Date: 2006-07-12 20:55:17 -0700 (Wed, 12 Jul 2006) $
42   */
43  
44  public final class EditRegistrationAction extends Action {
45  
46  
47      // ----------------------------------------------------- Instance Variables
48  
49  
50      /***
51       * The <code>Log</code> instance for this application.
52       */
53      private Log log =
54          LogFactory.getLog("org.apache.struts.webapp.Example");
55  
56  
57      // --------------------------------------------------------- Public Methods
58  
59  
60      /***
61       * Process the specified HTTP request, and create the corresponding HTTP
62       * response (or forward to another web component that will create it).
63       * Return an <code>ActionForward</code> instance describing where and how
64       * control should be forwarded, or <code>null</code> if the response has
65       * already been completed.
66       *
67       * @param mapping The ActionMapping used to select this instance
68       * @param form The optional ActionForm bean for this request (if any)
69       * @param request The HTTP request we are processing
70       * @param response The HTTP response we are creating
71       *
72       * @exception Exception if the application business logic throws
73       *  an exception
74       */
75      public ActionForward execute(ActionMapping mapping,
76  				 ActionForm form,
77  				 HttpServletRequest request,
78  				 HttpServletResponse response)
79  	throws Exception {
80  
81  	// Extract attributes we will need
82  	HttpSession session = request.getSession();
83  	String action = request.getParameter("action");
84  	if (action == null)
85  	    action = "Create";
86          if (log.isDebugEnabled()) {
87              log.debug("EditRegistrationAction:  Processing " + action +
88                          " action");
89          }
90  
91  	// Is there a currently logged on user?
92  	User user = null;
93  	if (!"Create".equals(action)) {
94  	    user = (User) session.getAttribute(Constants.USER_KEY);
95  	    if (user == null) {
96                  if (log.isDebugEnabled()) {
97                      log.debug(" User is not logged on in session "
98                                + session.getId());
99                  }
100 		return (mapping.findForward("logon"));
101 	    }
102 	}
103 
104 	// Populate the user registration form
105 	if (form == null) {
106             if (log.isTraceEnabled()) {
107                 log.trace(" Creating new RegistrationForm bean under key "
108                           + mapping.getAttribute());
109             }
110 	    form = new RegistrationForm();
111             if ("request".equals(mapping.getScope()))
112                 request.setAttribute(mapping.getAttribute(), form);
113             else
114                 session.setAttribute(mapping.getAttribute(), form);
115 	}
116 	RegistrationForm regform = (RegistrationForm) form;
117 	if (user != null) {
118             if (log.isTraceEnabled()) {
119                 log.trace(" Populating form from " + user);
120             }
121             try {
122                 PropertyUtils.copyProperties(regform, user);
123                 regform.setAction(action);
124                 regform.setPassword(null);
125                 regform.setPassword2(null);
126             } catch (InvocationTargetException e) {
127                 Throwable t = e.getTargetException();
128                 if (t == null)
129                     t = e;
130                 log.error("RegistrationForm.populate", t);
131                 throw new ServletException("RegistrationForm.populate", t);
132             } catch (Throwable t) {
133                 log.error("RegistrationForm.populate", t);
134                 throw new ServletException("RegistrationForm.populate", t);
135             }
136 	}
137 
138         // Set a transactional control token to prevent double posting
139         if (log.isTraceEnabled()) {
140             log.trace(" Setting transactional control token");
141         }
142         saveToken(request);
143 
144 	// Forward control to the edit user registration page
145         if (log.isTraceEnabled()) {
146             log.trace(" Forwarding to 'success' page");
147         }
148         if ("Create".equals(action)) {
149             return (mapping.findForward("register"));
150         } else {
151             return (mapping.findForward("success"));
152         }
153 
154     }
155 
156 
157 }