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.example2;
24
25
26 import java.lang.reflect.InvocationTargetException;
27
28 import jakarta.servlet.ServletException;
29 import jakarta.servlet.http.HttpServletRequest;
30 import jakarta.servlet.http.HttpServletResponse;
31 import jakarta.servlet.http.HttpSession;
32
33 import org.apache.commons.beanutils.PropertyUtils;
34 import org.apache.struts.action.Action;
35 import org.apache.struts.action.ActionErrors;
36 import org.apache.struts.action.ActionForm;
37 import org.apache.struts.action.ActionForward;
38 import org.apache.struts.action.ActionMapping;
39 import org.apache.struts.action.ActionMessage;
40 import org.apache.struts.apps.mailreader.dao.User;
41 import org.apache.struts.apps.mailreader.dao.UserDatabase;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45
46
47
48
49
50
51
52
53
54
55 public final class SaveRegistrationAction extends Action {
56 private static final long serialVersionUID = -7931388143259911830L;
57
58
59
60
61
62
63
64
65 private final static Logger LOG =
66 LoggerFactory.getLogger(SaveRegistrationAction.class);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public ActionForward execute(ActionMapping mapping,
88 ActionForm form,
89 HttpServletRequest request,
90 HttpServletResponse response)
91 throws Exception {
92
93
94 HttpSession session = request.getSession();
95 RegistrationForm regform = (RegistrationForm) form;
96 String action = regform.getAction();
97 if (action == null) {
98 action = "Create";
99 }
100 UserDatabase database = (UserDatabase)
101 servlet.getServletContext().getAttribute(Constants.DATABASE_KEY);
102 LOG.debug("SaveRegistrationAction: Processing {} action",
103 action);
104
105
106 User user = (User) session.getAttribute(Constants.USER_KEY);
107 if (!"Create".equals(action) && (user == null)) {
108 LOG.trace(" User is not logged on in session {}",
109 session.getId());
110 return (mapping.findForward("logon"));
111 }
112
113
114 if (isCancelled(request)) {
115 LOG.trace(" Transaction '{}' was cancelled",
116 action);
117 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
118 return (mapping.findForward("failure"));
119 }
120
121
122 ActionErrors errors = new ActionErrors();
123 LOG.trace(" Checking transactional control token");
124 if (!isTokenValid(request)) {
125 errors.add(ActionErrors.GLOBAL_MESSAGE,
126 new ActionMessage("error.transaction.token"));
127 }
128 resetToken(request);
129
130
131 LOG.trace(" Performing extra validations");
132 String value = null;
133 value = regform.getUsername();
134 if (("Create".equals(action)) &&
135 (database.findUser(value) != null)) {
136 errors.add("username",
137 new ActionMessage("error.username.unique",
138 regform.getUsername()));
139 }
140 if ("Create".equals(action)) {
141 value = regform.getPassword();
142 if ((value == null) || (value.length() <1)) {
143 errors.add("password",
144 new ActionMessage("error.password.required"));
145 }
146 value = regform.getPassword2();
147 if ((value == null) || (value.length() < 1)) {
148 errors.add("password2",
149 new ActionMessage("error.password2.required"));
150 }
151 }
152
153
154 if (!errors.isEmpty()) {
155 saveErrors(request, errors);
156 saveToken(request);
157 return (mapping.getInputForward());
158 }
159
160
161 try {
162 if ("Create".equals(action)) {
163 user = database.createUser(regform.getUsername());
164 }
165 String oldPassword = user.getPassword();
166 PropertyUtils.copyProperties(user, regform);
167 if ((regform.getPassword() == null) ||
168 (regform.getPassword().length() < 1)) {
169 user.setPassword(oldPassword);
170 }
171 } catch (InvocationTargetException e) {
172 Throwable t = e.getTargetException();
173 if (t == null) {
174 t = e;
175 }
176 LOG.error("Registration.populate", t);
177 throw new ServletException("Registration.populate", t);
178 } catch (Throwable t) {
179 LOG.error("Registration.populate", t);
180 throw new ServletException("Subscription.populate", t);
181 }
182
183 try {
184 database.save();
185 } catch (Exception e) {
186 LOG.error("Database save", e);
187 }
188
189
190 if ("Create".equals(action)) {
191 session.setAttribute(Constants.USER_KEY, user);
192 LOG.trace(" User '{}' logged on in session {}",
193 user.getUsername(), session.getId());
194 }
195
196
197 if (mapping.getAttribute() != null) {
198 if ("request".equals(mapping.getScope()))
199 request.removeAttribute(mapping.getAttribute());
200 else
201 session.removeAttribute(mapping.getAttribute());
202 }
203
204
205 LOG.trace(" Forwarding to success page");
206 return (mapping.findForward("success"));
207 }
208 }