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 import org.apache.struts.util.MessageResources; 36 37 43 44 public final class SaveSubscriptionAction extends Action { 45 46 48 51 private Log log = LogFactory.getLog("org.apache.struts.webapp.Example"); 52 53 55 public ActionForward execute( 57 ActionMapping mapping, 58 ActionForm form, 59 HttpServletRequest request, 60 HttpServletResponse response) 61 throws Exception { 62 63 MessageResources messages = getResources(request); 65 HttpSession session = request.getSession(); 66 SubscriptionForm subform = (SubscriptionForm) form; 67 String action = subform.getAction(); 68 if (action == null) { 69 action = "?"; 70 } 71 if (log.isDebugEnabled()) { 72 log.debug("SaveSubscriptionAction: Processing " + action + " action"); 73 } 74 75 User user = (User) session.getAttribute(Constants.USER_KEY); 77 if (user == null) { 78 if (log.isTraceEnabled()) { 79 log.trace(" User is not logged on in session " + session.getId()); 80 } 81 return (mapping.findForward("logon")); 82 } 83 84 if (isCancelled(request)) { 86 if (log.isTraceEnabled()) { 87 log.trace(" Transaction '" + action + "' was cancelled"); 88 } 89 session.removeAttribute(Constants.SUBSCRIPTION_KEY); 90 return (mapping.findForward("success")); 91 } 92 93 Subscription subscription = 95 (Subscription) session.getAttribute(Constants.SUBSCRIPTION_KEY); 96 if ("Create".equals(action)) { 97 subscription = user.createSubscription(request.getParameter("host")); 98 } 99 if (subscription == null) { 100 if (log.isTraceEnabled()) { 101 log.trace( 102 " Missing subscription for user '" + user.getUsername() + "'"); 103 } 104 response.sendError( 105 HttpServletResponse.SC_BAD_REQUEST, 106 messages.getMessage("error.noSubscription")); 107 return (null); 108 } 109 110 if (action.equals("Delete")) { 112 if (log.isTraceEnabled()) { 113 log.trace( 114 " Deleting mail server '" 115 + subscription.getHost() 116 + "' for user '" 117 + user.getUsername() 118 + "'"); 119 } 120 user.removeSubscription(subscription); 121 session.removeAttribute(Constants.SUBSCRIPTION_KEY); 122 try { 123 UserDatabase database = 124 (UserDatabase) servlet.getServletContext().getAttribute( 125 Constants.DATABASE_KEY); 126 database.save(); 127 } catch (Exception e) { 128 log.error("Database save", e); 129 } 130 return (mapping.findForward("success")); 131 } 132 133 135 if (log.isTraceEnabled()) { 137 log.trace(" Populating database from form bean"); 138 } 139 try { 140 PropertyUtils.copyProperties(subscription, subform); 141 } catch (InvocationTargetException e) { 142 Throwable t = e.getTargetException(); 143 if (t == null) 144 t = e; 145 log.error("Subscription.populate", t); 146 throw new ServletException ("Subscription.populate", t); 147 } catch (Throwable t) { 148 log.error("Subscription.populate", t); 149 throw new ServletException ("Subscription.populate", t); 150 } 151 152 try { 153 UserDatabase database = 154 (UserDatabase) servlet.getServletContext().getAttribute( 155 Constants.DATABASE_KEY); 156 database.save(); 157 } catch (Exception e) { 158 log.error("Database save", e); 159 } 160 161 if (mapping.getAttribute() != null) { 163 if ("request".equals(mapping.getScope())) 164 request.removeAttribute(mapping.getAttribute()); 165 else 166 session.removeAttribute(mapping.getAttribute()); 167 } 168 session.removeAttribute(Constants.SUBSCRIPTION_KEY); 169 170 if (log.isTraceEnabled()) { 172 log.trace(" Forwarding to success page"); 173 } 174 return (mapping.findForward("success")); 175 176 } 177 178 } 179 | Popular Tags |