1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.struts.webapp.example;
24
25
26 import java.lang.reflect.InvocationTargetException;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.beanutils.PropertyUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.struts.action.Action;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38
39
40
41
42
43
44
45
46
47
48
49 public final class EditRegistrationAction extends Action {
50
51
52
53
54
55
56
57
58 private Log log =
59 LogFactory.getLog("org.apache.struts.webapp.Example");
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public ActionForward execute(ActionMapping mapping,
81 ActionForm form,
82 HttpServletRequest request,
83 HttpServletResponse response)
84 throws Exception {
85
86
87 HttpSession session = request.getSession();
88 String action = request.getParameter("action");
89 if (action == null)
90 action = "Create";
91 if (log.isDebugEnabled()) {
92 log.debug("EditRegistrationAction: Processing " + action +
93 " action");
94 }
95
96
97 User user = null;
98 if (!"Create".equals(action)) {
99 user = (User) session.getAttribute(Constants.USER_KEY);
100 if (user == null) {
101 if (log.isDebugEnabled()) {
102 log.debug(" User is not logged on in session "
103 + session.getId());
104 }
105 return (mapping.findForward("logon"));
106 }
107 }
108
109
110 if (form == null) {
111 if (log.isTraceEnabled()) {
112 log.trace(" Creating new RegistrationForm bean under key "
113 + mapping.getAttribute());
114 }
115 form = new RegistrationForm();
116 if ("request".equals(mapping.getScope()))
117 request.setAttribute(mapping.getAttribute(), form);
118 else
119 session.setAttribute(mapping.getAttribute(), form);
120 }
121 RegistrationForm regform = (RegistrationForm) form;
122 if (user != null) {
123 if (log.isTraceEnabled()) {
124 log.trace(" Populating form from " + user);
125 }
126 try {
127 PropertyUtils.copyProperties(regform, user);
128 regform.setAction(action);
129 regform.setPassword(null);
130 regform.setPassword2(null);
131 } catch (InvocationTargetException e) {
132 Throwable t = e.getTargetException();
133 if (t == null)
134 t = e;
135 log.error("RegistrationForm.populate", t);
136 throw new ServletException("RegistrationForm.populate", t);
137 } catch (Throwable t) {
138 log.error("RegistrationForm.populate", t);
139 throw new ServletException("RegistrationForm.populate", t);
140 }
141 }
142
143
144 if (log.isTraceEnabled()) {
145 log.trace(" Setting transactional control token");
146 }
147 saveToken(request);
148
149
150 if (log.isTraceEnabled()) {
151 log.trace(" Forwarding to 'success' page");
152 }
153 return (mapping.findForward("success"));
154
155 }
156
157
158 }