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

« back to all changes in this revision

Viewing changes to contrib/struts-faces/src/example2/org/apache/struts/webapp/example2/SaveSubscriptionAction.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-2002,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.example2;
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.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;
35
 
 
36
 
 
37
 
/**
38
 
 * Implementation of <strong>Action</strong> that validates and creates or
39
 
 * updates the mail subscription entered by the user.
40
 
 *
41
 
 * @author Craig R. McClanahan
42
 
 * @version $Revision: 1.3 $ $Date: 2004/04/29 03:58:40 $
43
 
 */
44
 
 
45
 
public final class SaveSubscriptionAction 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 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
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 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();
88
 
        if (action == null) {
89
 
            action = "?";
90
 
        }
91
 
        if (log.isDebugEnabled()) {
92
 
            log.debug("SaveSubscriptionAction:  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
 
        // Was this transaction cancelled?
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
 
        // 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() + "'");
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
 
        // 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() + "'");
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
 
        // All required validations were done by the form itself
158
 
 
159
 
        // Update the persistent subscription information
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
 
        // Remove the obsolete form bean and current subscription
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
 
        // Forward control to the specified success URI
195
 
        if (log.isTraceEnabled()) {
196
 
            log.trace(" Forwarding to success page");
197
 
        }
198
 
        return (mapping.findForward("success"));
199
 
 
200
 
    }
201
 
 
202
 
 
203
 
}