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 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.servlet.http.HttpSession;
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 import org.apache.struts.util.MessageResources;
39
40
41
42
43
44
45
46
47
48
49 public final class SaveSubscriptionAction 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 MessageResources messages = getResources(request);
88 HttpSession session = request.getSession();
89 SubscriptionForm subform = (SubscriptionForm) form;
90 String action = subform.getAction();
91 if (action == null) {
92 action = "?";
93 }
94 if (log.isDebugEnabled()) {
95 log.debug("SaveSubscriptionAction: Processing " + action +
96 " action");
97 }
98
99
100 User user = (User) session.getAttribute(Constants.USER_KEY);
101 if (user == null) {
102 if (log.isTraceEnabled()) {
103 log.trace(" User is not logged on in session "
104 + session.getId());
105 }
106 return (mapping.findForward("logon"));
107 }
108
109
110 if (isCancelled(request)) {
111 if (log.isTraceEnabled()) {
112 log.trace(" Transaction '" + action +
113 "' was cancelled");
114 }
115 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
116 return (mapping.findForward("success"));
117 }
118
119
120 Subscription subscription =
121 (Subscription) session.getAttribute(Constants.SUBSCRIPTION_KEY);
122 if ("Create".equals(action)) {
123 if (log.isTraceEnabled()) {
124 log.trace(" Creating subscription for mail server '" +
125 subform.getHost() + "'");
126 }
127 subscription =
128 user.createSubscription(subform.getHost());
129 }
130 if (subscription == null) {
131 if (log.isTraceEnabled()) {
132 log.trace(" Missing subscription for user '" +
133 user.getUsername() + "'");
134 }
135 response.sendError(HttpServletResponse.SC_BAD_REQUEST,
136 messages.getMessage("error.noSubscription"));
137 return (null);
138 }
139
140
141 if (action.equals("Delete")) {
142 if (log.isTraceEnabled()) {
143 log.trace(" Deleting mail server '" +
144 subscription.getHost() + "' for user '" +
145 user.getUsername() + "'");
146 }
147 user.removeSubscription(subscription);
148 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
149 try {
150 UserDatabase database = (UserDatabase)
151 servlet.getServletContext().
152 getAttribute(Constants.DATABASE_KEY);
153 database.save();
154 } catch (Exception e) {
155 log.error("Database save", e);
156 }
157 return (mapping.findForward("success"));
158 }
159
160
161
162
163 if (log.isTraceEnabled()) {
164 log.trace(" Populating database from form bean");
165 }
166 try {
167 PropertyUtils.copyProperties(subscription, subform);
168 } catch (InvocationTargetException e) {
169 Throwable t = e.getTargetException();
170 if (t == null)
171 t = e;
172 log.error("Subscription.populate", t);
173 throw new ServletException("Subscription.populate", t);
174 } catch (Throwable t) {
175 log.error("Subscription.populate", t);
176 throw new ServletException("Subscription.populate", t);
177 }
178
179 try {
180 UserDatabase database = (UserDatabase)
181 servlet.getServletContext().
182 getAttribute(Constants.DATABASE_KEY);
183 database.save();
184 } catch (Exception e) {
185 log.error("Database save", e);
186 }
187
188
189 if (mapping.getAttribute() != null) {
190 if ("request".equals(mapping.getScope()))
191 request.removeAttribute(mapping.getAttribute());
192 else
193 session.removeAttribute(mapping.getAttribute());
194 }
195 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
196
197
198 if (log.isTraceEnabled()) {
199 log.trace(" Forwarding to success page");
200 }
201 return (mapping.findForward("success"));
202
203 }
204
205
206 }