View Javadoc

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.example2;
19  
20  
21  import java.lang.reflect.InvocationTargetException;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpSession;
26  import org.apache.commons.beanutils.PropertyUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.struts.action.Action;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  
34  
35  /***
36   * Implementation of <strong>Action</strong> that populates an instance of
37   * <code>SubscriptionForm</code> from the currently specified subscription.
38   *
39   * @author Craig R. McClanahan
40   * @version $Rev: 421494 $ $Date: 2006-07-12 20:55:17 -0700 (Wed, 12 Jul 2006) $
41   */
42  
43  public final class EditSubscriptionAction extends Action {
44  
45  
46      // ----------------------------------------------------- Instance Variables
47  
48  
49      /***
50       * The <code>Log</code> instance for this application.
51       */
52      private Log log =
53          LogFactory.getLog("org.apache.struts.webapp.Example");
54  
55  
56      // --------------------------------------------------------- Public Methods
57  
58  
59      /***
60       * Process the specified HTTP request, and create the corresponding HTTP
61       * response (or forward to another web component that will create it).
62       * Return an <code>ActionForward</code> instance describing where and how
63       * control should be forwarded, or <code>null</code> if the response has
64       * already been completed.
65       *
66       * @param mapping The ActionMapping used to select this instance
67       * @param form The optional ActionForm bean for this request (if any)
68       * @param request The HTTP request we are processing
69       * @param response The HTTP response we are creating
70       *
71       * @exception Exception if the application business logic throws
72       *  an exception
73       */
74      public ActionForward execute(ActionMapping mapping,
75  				 ActionForm form,
76  				 HttpServletRequest request,
77  				 HttpServletResponse response)
78  	throws Exception {
79  
80  	// Extract attributes we will need
81  	HttpSession session = request.getSession();
82  	String action = request.getParameter("action");
83  	if (action == null) {
84  	    action = "Create";
85          }
86  	String host = request.getParameter("host");
87          if (log.isDebugEnabled()) {
88              log.debug("EditSubscriptionAction:  Processing " + action +
89                        " action");
90          }
91  
92  	// Is there a currently logged on user?
93  	User user = (User) session.getAttribute(Constants.USER_KEY);
94  	if (user == null) {
95              if (log.isTraceEnabled()) {
96                  log.trace(" User is not logged on in session "
97                            + session.getId());
98              }
99  	    return (mapping.findForward("logon"));
100 	}
101 
102 	// Identify the relevant subscription
103 	Subscription subscription =
104             user.findSubscription(request.getParameter("host"));
105 	if ((subscription == null) && !action.equals("Create")) {
106             if (log.isTraceEnabled()) {
107                 log.trace(" No subscription for user " +
108                           user.getUsername() + " and host " + host);
109             }
110 	    return (mapping.findForward("failure"));
111 	}
112         if (subscription != null) {
113             session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription);
114         }
115 
116 	// Populate the subscription form
117 	if (form == null) {
118             if (log.isTraceEnabled()) {
119                 log.trace(" Creating new SubscriptionForm bean under key "
120                           + mapping.getAttribute());
121             }
122 	    form = new SubscriptionForm();
123             if ("request".equals(mapping.getScope())) {
124                 request.setAttribute(mapping.getAttribute(), form);
125             } else {
126                 session.setAttribute(mapping.getAttribute(), form);
127             }
128 	}
129 	SubscriptionForm subform = (SubscriptionForm) form;
130 	subform.setAction(action);
131         if (!action.equals("Create")) {
132             if (log.isTraceEnabled()) {
133                 log.trace(" Populating form from " + subscription);
134             }
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         if (log.isTraceEnabled()) {
152             log.trace(" Forwarding to 'success' page");
153         }
154 	return (mapping.findForward("success"));
155 
156     }
157 
158 
159 }