1 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 35 36 43 44 public final class EditSubscriptionAction extends Action { 45 46 47 49 50 53 private Log log = 54 LogFactory.getLog("org.apache.struts.webapp.Example"); 55 56 57 59 60 75 public ActionForward execute(ActionMapping mapping, 76 ActionForm form, 77 HttpServletRequest request, 78 HttpServletResponse response) 79 throws Exception { 80 81 Locale locale = getLocale(request); 83 HttpSession session = request.getSession(); 84 String action = request.getParameter("action"); 85 if (action == null) { 86 action = "Create"; 87 } 88 String host = request.getParameter("host"); 89 if (log.isDebugEnabled()) { 90 log.debug("EditSubscriptionAction: Processing " + action + 91 " action"); 92 } 93 94 User user = (User) session.getAttribute(Constants.USER_KEY); 96 if (user == null) { 97 if (log.isTraceEnabled()) { 98 log.trace(" User is not logged on in session " 99 + session.getId()); 100 } 101 return (mapping.findForward("logon")); 102 } 103 104 Subscription subscription = 106 user.findSubscription(request.getParameter("host")); 107 if ((subscription == null) && !action.equals("Create")) { 108 if (log.isTraceEnabled()) { 109 log.trace(" No subscription for user " + 110 user.getUsername() + " and host " + host); 111 } 112 return (mapping.findForward("failure")); 113 } 114 if (subscription != null) { 115 session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription); 116 } 117 118 if (form == null) { 120 if (log.isTraceEnabled()) { 121 log.trace(" Creating new SubscriptionForm bean under key " 122 + mapping.getAttribute()); 123 } 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 if (log.isTraceEnabled()) { 135 log.trace(" Populating form from " + subscription); 136 } 137 try { 138 PropertyUtils.copyProperties(subform, subscription); 139 subform.setAction(action); 140 } catch (InvocationTargetException e) { 141 Throwable t = e.getTargetException(); 142 if (t == null) 143 t = e; 144 log.error("SubscriptionForm.populate", t); 145 throw new ServletException ("SubscriptionForm.populate", t); 146 } catch (Throwable t) { 147 log.error("SubscriptionForm.populate", t); 148 throw new ServletException ("SubscriptionForm.populate", t); 149 } 150 } 151 152 if (log.isTraceEnabled()) { 154 log.trace(" Forwarding to 'success' page"); 155 } 156 return (mapping.findForward("success")); 157 158 } 159 160 161 } 162 | Popular Tags |