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
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.http.HttpSession;
27
28 import org.apache.commons.beanutils.PropertyUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts.action.Action;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.util.MessageResources;
36
37
38 /***
39 * Implementation of <strong>Action</strong> that validates and creates or
40 * updates the mail subscription entered by the user.
41 *
42 * @author Craig R. McClanahan
43 * @version $Rev: 421493 $ $Date: 2006-07-12 20:52:31 -0700 (Wed, 12 Jul 2006) $
44 */
45
46 public final class SaveSubscriptionAction extends Action {
47
48
49
50
51
52 /***
53 * The <code>Log</code> instance for this application.
54 */
55 private Log log =
56 LogFactory.getLog("org.apache.struts.webapp.Example");
57
58
59
60
61
62 /***
63 * Process the specified HTTP request, and create the corresponding HTTP
64 * response (or forward to another web component that will create it).
65 * Return an <code>ActionForward</code> instance describing where and how
66 * control should be forwarded, or <code>null</code> if the response has
67 * already been completed.
68 *
69 * @param mapping The ActionMapping used to select this instance
70 * @param form The optional ActionForm bean for this request (if any)
71 * @param request The HTTP request we are processing
72 * @param response The HTTP response we are creating
73 *
74 * @exception Exception if the application business logic throws
75 * an exception
76 */
77 public ActionForward execute(ActionMapping mapping,
78 ActionForm form,
79 HttpServletRequest request,
80 HttpServletResponse response)
81 throws Exception {
82
83
84 MessageResources messages = getResources(request);
85 HttpSession session = request.getSession();
86 SubscriptionForm subform = (SubscriptionForm) form;
87 String action = subform.getAction();
88 if (action == null) {
89 action = "?";
90 }
91 if (log.isDebugEnabled()) {
92 log.debug("SaveSubscriptionAction: Processing " + action +
93 " action");
94 }
95
96
97 User user = (User) session.getAttribute(Constants.USER_KEY);
98 if (user == null) {
99 if (log.isTraceEnabled()) {
100 log.trace(" User is not logged on in session "
101 + session.getId());
102 }
103 return (mapping.findForward("logon"));
104 }
105
106
107 if (isCancelled(request)) {
108 if (log.isTraceEnabled()) {
109 log.trace(" Transaction '" + action +
110 "' was cancelled");
111 }
112 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
113 return (mapping.findForward("success"));
114 }
115
116
117 Subscription subscription =
118 (Subscription) session.getAttribute(Constants.SUBSCRIPTION_KEY);
119 if ("Create".equals(action)) {
120 if (log.isTraceEnabled()) {
121 log.trace(" Creating subscription for mail server '" +
122 subform.getHost() + "'");
123 }
124 subscription =
125 user.createSubscription(subform.getHost());
126 }
127 if (subscription == null) {
128 if (log.isTraceEnabled()) {
129 log.trace(" Missing subscription for user '" +
130 user.getUsername() + "'");
131 }
132 response.sendError(HttpServletResponse.SC_BAD_REQUEST,
133 messages.getMessage("error.noSubscription"));
134 return (null);
135 }
136
137
138 if (action.equals("Delete")) {
139 if (log.isTraceEnabled()) {
140 log.trace(" Deleting mail server '" +
141 subscription.getHost() + "' for user '" +
142 user.getUsername() + "'");
143 }
144 user.removeSubscription(subscription);
145 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
146 try {
147 UserDatabase database = (UserDatabase)
148 servlet.getServletContext().
149 getAttribute(Constants.DATABASE_KEY);
150 database.save();
151 } catch (Exception e) {
152 log.error("Database save", e);
153 }
154 return (mapping.findForward("success"));
155 }
156
157
158
159
160 if (log.isTraceEnabled()) {
161 log.trace(" Populating database from form bean");
162 }
163 try {
164 PropertyUtils.copyProperties(subscription, subform);
165 } catch (InvocationTargetException e) {
166 Throwable t = e.getTargetException();
167 if (t == null)
168 t = e;
169 log.error("Subscription.populate", t);
170 throw new ServletException("Subscription.populate", t);
171 } catch (Throwable t) {
172 log.error("Subscription.populate", t);
173 throw new ServletException("Subscription.populate", t);
174 }
175
176 try {
177 UserDatabase database = (UserDatabase)
178 servlet.getServletContext().
179 getAttribute(Constants.DATABASE_KEY);
180 database.save();
181 } catch (Exception e) {
182 log.error("Database save", e);
183 }
184
185
186 if (mapping.getAttribute() != null) {
187 if ("request".equals(mapping.getScope()))
188 request.removeAttribute(mapping.getAttribute());
189 else
190 session.removeAttribute(mapping.getAttribute());
191 }
192 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
193
194
195 if (log.isTraceEnabled()) {
196 log.trace(" Forwarding to success page");
197 }
198 return (mapping.findForward("success"));
199
200 }
201
202
203 }