2
* Copyright 1999-2002,2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
package org.apache.struts.webapp.example2;
21
import java.lang.reflect.InvocationTargetException;
22
import java.util.Locale;
23
import javax.servlet.ServletException;
24
import javax.servlet.http.HttpServletRequest;
25
import javax.servlet.http.HttpServletResponse;
26
import javax.servlet.http.HttpSession;
27
import org.apache.commons.beanutils.PropertyUtils;
28
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.LogFactory;
30
import org.apache.struts.action.Action;
31
import org.apache.struts.action.ActionForm;
32
import org.apache.struts.action.ActionForward;
33
import org.apache.struts.action.ActionMapping;
34
import org.apache.struts.util.MessageResources;
38
* Implementation of <strong>Action</strong> that validates and creates or
39
* updates the mail subscription entered by the user.
41
* @author Craig R. McClanahan
42
* @version $Revision: 1.3 $ $Date: 2004/04/29 03:58:40 $
45
public final class SaveSubscriptionAction extends Action {
48
// ----------------------------------------------------- Instance Variables
52
* The <code>Log</code> instance for this application.
55
LogFactory.getLog("org.apache.struts.webapp.Example");
58
// --------------------------------------------------------- Public Methods
62
* Process the specified HTTP request, and create the corresponding HTTP
63
* response (or forward to another web component that will create it).
64
* Return an <code>ActionForward</code> instance describing where and how
65
* control should be forwarded, or <code>null</code> if the response has
66
* already been completed.
68
* @param mapping The ActionMapping used to select this instance
69
* @param actionForm The optional ActionForm bean for this request (if any)
70
* @param request The HTTP request we are processing
71
* @param response The HTTP response we are creating
73
* @exception Exception if the application business logic throws
76
public ActionForward execute(ActionMapping mapping,
78
HttpServletRequest request,
79
HttpServletResponse response)
82
// Extract attributes and parameters we will need
83
Locale locale = getLocale(request);
84
MessageResources messages = getResources(request);
85
HttpSession session = request.getSession();
86
SubscriptionForm subform = (SubscriptionForm) form;
87
String action = subform.getAction();
91
if (log.isDebugEnabled()) {
92
log.debug("SaveSubscriptionAction: Processing " + action +
96
// Is there a currently logged on user?
97
User user = (User) session.getAttribute(Constants.USER_KEY);
99
if (log.isTraceEnabled()) {
100
log.trace(" User is not logged on in session "
103
return (mapping.findForward("logon"));
106
// Was this transaction cancelled?
107
if (isCancelled(request)) {
108
if (log.isTraceEnabled()) {
109
log.trace(" Transaction '" + action +
112
session.removeAttribute(Constants.SUBSCRIPTION_KEY);
113
return (mapping.findForward("success"));
116
// Is there a related Subscription object?
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() + "'");
125
user.createSubscription(subform.getHost());
127
if (subscription == null) {
128
if (log.isTraceEnabled()) {
129
log.trace(" Missing subscription for user '" +
130
user.getUsername() + "'");
132
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
133
messages.getMessage("error.noSubscription"));
137
// Was this transaction a Delete?
138
if (action.equals("Delete")) {
139
if (log.isTraceEnabled()) {
140
log.trace(" Deleting mail server '" +
141
subscription.getHost() + "' for user '" +
142
user.getUsername() + "'");
144
user.removeSubscription(subscription);
145
session.removeAttribute(Constants.SUBSCRIPTION_KEY);
147
UserDatabase database = (UserDatabase)
148
servlet.getServletContext().
149
getAttribute(Constants.DATABASE_KEY);
151
} catch (Exception e) {
152
log.error("Database save", e);
154
return (mapping.findForward("success"));
157
// All required validations were done by the form itself
159
// Update the persistent subscription information
160
if (log.isTraceEnabled()) {
161
log.trace(" Populating database from form bean");
164
PropertyUtils.copyProperties(subscription, subform);
165
} catch (InvocationTargetException e) {
166
Throwable t = e.getTargetException();
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);
177
UserDatabase database = (UserDatabase)
178
servlet.getServletContext().
179
getAttribute(Constants.DATABASE_KEY);
181
} catch (Exception e) {
182
log.error("Database save", e);
185
// Remove the obsolete form bean and current subscription
186
if (mapping.getAttribute() != null) {
187
if ("request".equals(mapping.getScope()))
188
request.removeAttribute(mapping.getAttribute());
190
session.removeAttribute(mapping.getAttribute());
192
session.removeAttribute(Constants.SUBSCRIPTION_KEY);
194
// Forward control to the specified success URI
195
if (log.isTraceEnabled()) {
196
log.trace(" Forwarding to success page");
198
return (mapping.findForward("success"));