View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  package org.apache.struts.webapp.example;
24  
25  
26  import java.lang.reflect.InvocationTargetException;
27  
28  import jakarta.servlet.ServletException;
29  import jakarta.servlet.http.HttpServletRequest;
30  import jakarta.servlet.http.HttpServletResponse;
31  import jakarta.servlet.http.HttpSession;
32  
33  import org.apache.commons.beanutils.PropertyUtils;
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.apps.mailreader.dao.Subscription;
39  import org.apache.struts.apps.mailreader.dao.User;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  
44  /**
45   * Implementation of <strong>Action</strong> that populates an instance of
46   * <code>SubscriptionForm</code> from the currently specified subscription.
47   *
48   * @author Craig R. McClanahan
49   * @version $Rev$ $Date$
50   */
51  
52  public final class EditSubscriptionAction extends Action {
53      private static final long serialVersionUID = -205590678807295830L;
54  
55  
56      // ----------------------------------------------------- Instance Variables
57  
58  
59      /**
60       * The {@code Log} instance for this class.
61       */
62      private final static Logger LOG =
63          LoggerFactory.getLogger(EditSubscriptionAction.class);
64  
65  
66      // --------------------------------------------------------- Public Methods
67  
68  
69      /**
70       * Process the specified HTTP request, and create the corresponding HTTP
71       * response (or forward to another web component that will create it).
72       * Return an <code>ActionForward</code> instance describing where and how
73       * control should be forwarded, or <code>null</code> if the response has
74       * already been completed.
75       *
76       * @param mapping The ActionMapping used to select this instance
77       * @param form The optional ActionForm bean for this request (if any)
78       * @param request The HTTP request we are processing
79       * @param response The HTTP response we are creating
80       *
81       * @exception Exception if the application business logic throws
82       *  an exception
83       */
84      public ActionForward execute(ActionMapping mapping,
85                   ActionForm form,
86                   HttpServletRequest request,
87                   HttpServletResponse response)
88      throws Exception {
89  
90      // Extract attributes we will need
91      HttpSession session = request.getSession();
92      String action = request.getParameter("action");
93      if (action == null) {
94          action = "Create";
95      }
96      String host = request.getParameter("host");
97      LOG.debug("EditSubscriptionAction:  Processing {} action",
98          action);
99  
100     // Is there a currently logged on user?
101     User user = (User) session.getAttribute(Constants.USER_KEY);
102     if (user == null) {
103         LOG.trace(" User is not logged on in session {}",
104             session.getId());
105         return (mapping.findForward("logon"));
106     }
107 
108     // Identify the relevant subscription
109     Subscription subscription =
110             user.findSubscription(request.getParameter("host"));
111     if ((subscription == null) && !action.equals("Create")) {
112         LOG.trace(" No subscription for user {} and host {}",
113             user.getUsername(), host);
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         LOG.trace(" Creating new SubscriptionForm bean under key {}",
123             mapping.getAttribute());
124         form = new SubscriptionForm();
125             if ("request".equals(mapping.getScope())) {
126                 request.setAttribute(mapping.getAttribute(), form);
127             } else {
128                 session.setAttribute(mapping.getAttribute(), form);
129             }
130     }
131     SubscriptionForm subform = (SubscriptionForm) form;
132     subform.setAction(action);
133         if (!action.equals("Create")) {
134             LOG.trace(" Populating form from {}", subscription);
135             try {
136                 PropertyUtils.copyProperties(subform, subscription);
137                 subform.setAction(action);
138             } catch (InvocationTargetException e) {
139                 Throwable t = e.getTargetException();
140                 if (t == null)
141                     t = e;
142                 LOG.error("SubscriptionForm.populate", t);
143                 throw new ServletException("SubscriptionForm.populate", t);
144             } catch (Throwable t) {
145                 LOG.error("SubscriptionForm.populate", t);
146                 throw new ServletException("SubscriptionForm.populate", t);
147             }
148         }
149 
150     // Forward control to the edit subscription page
151     LOG.trace(" Forwarding to 'success' page");
152     return (mapping.findForward("success"));
153 
154     }
155 }