2
* Copyright 1999-2001,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.example;
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.HttpSession;
26
import javax.servlet.http.HttpServletResponse;
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 populates an instance of
39
* <code>SubscriptionForm</code> from the currently specified subscription.
41
* @author Craig R. McClanahan
42
* @version $Rev: 54936 $ $Date: 2004-10-16 18:57:09 +0100 (Sat, 16 Oct 2004) $
45
public final class EditSubscriptionAction 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 form 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 we will need
83
Locale locale = getLocale(request);
84
MessageResources messages = getResources(request);
85
HttpSession session = request.getSession();
86
String action = request.getParameter("action");
90
String host = request.getParameter("host");
91
if (log.isDebugEnabled()) {
92
log.debug("EditSubscriptionAction: 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
// Identify the relevant subscription
107
Subscription subscription =
108
user.findSubscription(request.getParameter("host"));
109
if ((subscription == null) && !action.equals("Create")) {
110
if (log.isTraceEnabled()) {
111
log.trace(" No subscription for user " +
112
user.getUsername() + " and host " + host);
114
return (mapping.findForward("failure"));
116
if (subscription != null) {
117
session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription);
120
// Populate the subscription form
122
if (log.isTraceEnabled()) {
123
log.trace(" Creating new SubscriptionForm bean under key "
124
+ mapping.getAttribute());
126
form = new SubscriptionForm();
127
if ("request".equals(mapping.getScope())) {
128
request.setAttribute(mapping.getAttribute(), form);
130
session.setAttribute(mapping.getAttribute(), form);
133
SubscriptionForm subform = (SubscriptionForm) form;
134
subform.setAction(action);
135
if (!action.equals("Create")) {
136
if (log.isTraceEnabled()) {
137
log.trace(" Populating form from " + subscription);
140
PropertyUtils.copyProperties(subform, subscription);
141
subform.setAction(action);
142
} catch (InvocationTargetException e) {
143
Throwable t = e.getTargetException();
146
log.error("SubscriptionForm.populate", t);
147
throw new ServletException("SubscriptionForm.populate", t);
148
} catch (Throwable t) {
149
log.error("SubscriptionForm.populate", t);
150
throw new ServletException("SubscriptionForm.populate", t);
154
// Forward control to the edit subscription page
155
if (log.isTraceEnabled()) {
156
log.trace(" Forwarding to 'success' page");
158
return (mapping.findForward("success"));