1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.webapp.example;
19
20
21 import java.lang.reflect.InvocationTargetException;
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpSession;
25 import javax.servlet.http.HttpServletResponse;
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: 421493 $ $Date: 2006-07-12 20:52:31 -0700 (Wed, 12 Jul 2006) $
42 */
43
44 public final class EditRegistrationAction extends Action {
45
46
47
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
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
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
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
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
139 if (log.isTraceEnabled()) {
140 log.trace(" Setting transactional control token");
141 }
142 saveToken(request);
143
144
145 if (log.isTraceEnabled()) {
146 log.trace(" Forwarding to 'success' page");
147 }
148 return (mapping.findForward("success"));
149
150 }
151
152
153 }