~ubuntu-branches/ubuntu/lucid/libstruts1.2-java/lucid

« back to all changes in this revision

Viewing changes to contrib/struts-faces/example1-webapp/src/java/org/apache/struts/webapp/example/EditSubscriptionAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2006-04-24 12:14:23 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060424121423-naev53qigqgks0sa
Tags: 1.2.9-1
New upstream  release Fixes  three security  problems: CVE-2006-1546,
CVE-2006-1547,  CVE-2006-1548  (closes:  #360551),  thanks  to  Moritz
Muehlenhoff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 1999-2001,2004 The Apache Software Foundation.
 
3
 * 
 
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
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
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.
 
15
 */
 
16
 
 
17
 
 
18
package org.apache.struts.webapp.example;
 
19
 
 
20
 
 
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;
 
35
 
 
36
 
 
37
/**
 
38
 * Implementation of <strong>Action</strong> that populates an instance of
 
39
 * <code>SubscriptionForm</code> from the currently specified subscription.
 
40
 *
 
41
 * @author Craig R. McClanahan
 
42
 * @version $Rev: 54936 $ $Date: 2004-10-16 18:57:09 +0100 (Sat, 16 Oct 2004) $
 
43
 */
 
44
 
 
45
public final class EditSubscriptionAction extends Action {
 
46
 
 
47
 
 
48
    // ----------------------------------------------------- Instance Variables
 
49
 
 
50
 
 
51
    /**
 
52
     * The <code>Log</code> instance for this application.
 
53
     */
 
54
    private Log log =
 
55
        LogFactory.getLog("org.apache.struts.webapp.Example");
 
56
 
 
57
 
 
58
    // --------------------------------------------------------- Public Methods
 
59
 
 
60
 
 
61
    /**
 
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.
 
67
     *
 
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
 
72
     *
 
73
     * @exception Exception if the application business logic throws
 
74
     *  an exception
 
75
     */
 
76
    public ActionForward execute(ActionMapping mapping,
 
77
                                 ActionForm form,
 
78
                                 HttpServletRequest request,
 
79
                                 HttpServletResponse response)
 
80
        throws Exception {
 
81
 
 
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");
 
87
        if (action == null) {
 
88
            action = "Create";
 
89
        }
 
90
        String host = request.getParameter("host");
 
91
        if (log.isDebugEnabled()) {
 
92
            log.debug("EditSubscriptionAction:  Processing " + action +
 
93
                      " action");
 
94
        }
 
95
 
 
96
        // Is there a currently logged on user?
 
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
        // 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);
 
113
            }
 
114
            return (mapping.findForward("failure"));
 
115
        }
 
116
        if (subscription != null) {
 
117
            session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription);
 
118
        }
 
119
 
 
120
        // Populate the subscription form
 
121
        if (form == null) {
 
122
            if (log.isTraceEnabled()) {
 
123
                log.trace(" Creating new SubscriptionForm bean under key "
 
124
                          + mapping.getAttribute());
 
125
            }
 
126
            form = new SubscriptionForm();
 
127
            if ("request".equals(mapping.getScope())) {
 
128
                request.setAttribute(mapping.getAttribute(), form);
 
129
            } else {
 
130
                session.setAttribute(mapping.getAttribute(), form);
 
131
            }
 
132
        }
 
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);
 
138
            }
 
139
            try {
 
140
                PropertyUtils.copyProperties(subform, subscription);
 
141
                subform.setAction(action);
 
142
            } catch (InvocationTargetException e) {
 
143
                Throwable t = e.getTargetException();
 
144
                if (t == null)
 
145
                    t = e;
 
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);
 
151
            }
 
152
        }
 
153
 
 
154
        // Forward control to the edit subscription page
 
155
        if (log.isTraceEnabled()) {
 
156
            log.trace(" Forwarding to 'success' page");
 
157
        }
 
158
        return (mapping.findForward("success"));
 
159
 
 
160
    }
 
161
 
 
162
 
 
163
}