001: /*
002: * $Id: EditSubscriptionAction.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.webapp.example2;
023:
024: import java.lang.reflect.InvocationTargetException;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import javax.servlet.http.HttpSession;
029: import org.apache.commons.beanutils.PropertyUtils;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.struts.action.Action;
033: import org.apache.struts.action.ActionForm;
034: import org.apache.struts.action.ActionForward;
035: import org.apache.struts.action.ActionMapping;
036:
037: /**
038: * Implementation of <strong>Action</strong> that populates an instance of
039: * <code>SubscriptionForm</code> from the currently specified subscription.
040: *
041: * @author Craig R. McClanahan
042: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
043: */
044:
045: public final class EditSubscriptionAction extends Action {
046:
047: // ----------------------------------------------------- Instance Variables
048:
049: /**
050: * The <code>Log</code> instance for this application.
051: */
052: private Log log = LogFactory
053: .getLog("org.apache.struts.webapp.Example");
054:
055: // --------------------------------------------------------- Public Methods
056:
057: /**
058: * Process the specified HTTP request, and create the corresponding HTTP
059: * response (or forward to another web component that will create it).
060: * Return an <code>ActionForward</code> instance describing where and how
061: * control should be forwarded, or <code>null</code> if the response has
062: * already been completed.
063: *
064: * @param mapping The ActionMapping used to select this instance
065: * @param form The optional ActionForm bean for this request (if any)
066: * @param request The HTTP request we are processing
067: * @param response The HTTP response we are creating
068: *
069: * @exception Exception if the application business logic throws
070: * an exception
071: */
072: public ActionForward execute(ActionMapping mapping,
073: ActionForm form, HttpServletRequest request,
074: HttpServletResponse response) throws Exception {
075:
076: // Extract attributes we will need
077: HttpSession session = request.getSession();
078: String action = request.getParameter("action");
079: if (action == null) {
080: action = "Create";
081: }
082: String host = request.getParameter("host");
083: if (log.isDebugEnabled()) {
084: log.debug("EditSubscriptionAction: Processing " + action
085: + " action");
086: }
087:
088: // Is there a currently logged on user?
089: User user = (User) session.getAttribute(Constants.USER_KEY);
090: if (user == null) {
091: if (log.isTraceEnabled()) {
092: log.trace(" User is not logged on in session "
093: + session.getId());
094: }
095: return (mapping.findForward("logon"));
096: }
097:
098: // Identify the relevant subscription
099: Subscription subscription = user.findSubscription(request
100: .getParameter("host"));
101: if ((subscription == null) && !action.equals("Create")) {
102: if (log.isTraceEnabled()) {
103: log.trace(" No subscription for user "
104: + user.getUsername() + " and host " + host);
105: }
106: return (mapping.findForward("failure"));
107: }
108: if (subscription != null) {
109: session.setAttribute(Constants.SUBSCRIPTION_KEY,
110: subscription);
111: }
112:
113: // Populate the subscription form
114: if (form == null) {
115: if (log.isTraceEnabled()) {
116: log
117: .trace(" Creating new SubscriptionForm bean under key "
118: + mapping.getAttribute());
119: }
120: form = new SubscriptionForm();
121: if ("request".equals(mapping.getScope())) {
122: request.setAttribute(mapping.getAttribute(), form);
123: } else {
124: session.setAttribute(mapping.getAttribute(), form);
125: }
126: }
127: SubscriptionForm subform = (SubscriptionForm) form;
128: subform.setAction(action);
129: if (!action.equals("Create")) {
130: if (log.isTraceEnabled()) {
131: log.trace(" Populating form from " + subscription);
132: }
133: try {
134: PropertyUtils.copyProperties(subform, subscription);
135: subform.setAction(action);
136: } catch (InvocationTargetException e) {
137: Throwable t = e.getTargetException();
138: if (t == null)
139: t = e;
140: log.error("SubscriptionForm.populate", t);
141: throw new ServletException("SubscriptionForm.populate",
142: t);
143: } catch (Throwable t) {
144: log.error("SubscriptionForm.populate", t);
145: throw new ServletException("SubscriptionForm.populate",
146: 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: }
|