1 18 19 package org.apache.struts.webapp.example; 20 21 import java.lang.reflect.InvocationTargetException ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 28 import org.apache.commons.beanutils.PropertyUtils; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.struts.action.Action; 32 import org.apache.struts.action.ActionForm; 33 import org.apache.struts.action.ActionForward; 34 import org.apache.struts.action.ActionMapping; 35 36 42 public final class EditSubscriptionAction extends Action { 43 44 46 49 private Log log = LogFactory.getLog("org.apache.struts.webapp.Example"); 50 51 53 public ActionForward execute( 55 ActionMapping mapping, 56 ActionForm form, 57 HttpServletRequest request, 58 HttpServletResponse response) 59 throws Exception { 60 61 HttpSession session = request.getSession(); 63 String action = request.getParameter("action"); 64 if (action == null) { 65 action = "Create"; 66 } 67 68 String host = request.getParameter("host"); 69 if (log.isDebugEnabled()) { 70 log.debug("EditSubscriptionAction: Processing " + action + " action"); 71 } 72 73 User user = (User) session.getAttribute(Constants.USER_KEY); 75 if (user == null) { 76 if (log.isTraceEnabled()) { 77 log.trace(" User is not logged on in session " + session.getId()); 78 } 79 return (mapping.findForward("logon")); 80 } 81 82 Subscription subscription = 84 user.findSubscription(request.getParameter("host")); 85 86 if ((subscription == null) && !action.equals("Create")) { 87 if (log.isTraceEnabled()) { 88 log.trace( 89 " No subscription for user " 90 + user.getUsername() 91 + " and host " 92 + host); 93 } 94 95 return (mapping.findForward("failure")); 96 } 97 98 if (subscription != null) { 99 session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription); 100 } 101 102 SubscriptionForm subform = (SubscriptionForm) form; 103 subform.setAction(action); 104 if (!action.equals("Create")) { 105 if (log.isTraceEnabled()) { 106 log.trace(" Populating form from " + subscription); 107 } 108 109 try { 110 PropertyUtils.copyProperties(subform, subscription); 111 subform.setAction(action); 112 113 } catch (InvocationTargetException e) { 114 Throwable t = e.getTargetException(); 115 if (t == null) 116 t = e; 117 log.error("SubscriptionForm.populate", t); 118 throw new ServletException ("SubscriptionForm.populate", t); 119 120 } catch (Throwable t) { 121 log.error("SubscriptionForm.populate", t); 122 throw new ServletException ("SubscriptionForm.populate", t); 123 } 124 } 125 126 if (log.isTraceEnabled()) { 128 log.trace(" Forwarding to 'success' page"); 129 } 130 131 return (mapping.findForward("success")); 132 133 } 134 135 } 136 | Popular Tags |