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 import org.apache.struts.util.MessageResources; 35 36 37 44 45 public final class SaveSubscriptionAction extends Action { 46 47 48 50 51 54 private Log log = 55 LogFactory.getLog("org.apache.struts.webapp.Example"); 56 57 58 60 61 76 public ActionForward execute(ActionMapping mapping, 77 ActionForm form, 78 HttpServletRequest request, 79 HttpServletResponse response) 80 throws Exception { 81 82 Locale locale = getLocale(request); 84 MessageResources messages = getResources(request); 85 HttpSession session = request.getSession(); 86 SubscriptionForm subform = (SubscriptionForm) form; 87 String action = subform.getAction(); 88 if (action == null) { 89 action = "?"; 90 } 91 if (log.isDebugEnabled()) { 92 log.debug("SaveSubscriptionAction: Processing " + action + 93 " action"); 94 } 95 96 User user = (User) session.getAttribute(Constants.USER_KEY); 98 if (user == null) { 99 if (log.isTraceEnabled()) { 100 log.trace(" User is not logged on in session " 101 + session.getId()); 102 } 103 return (mapping.findForward("logon")); 104 } 105 106 if (isCancelled(request)) { 108 if (log.isTraceEnabled()) { 109 log.trace(" Transaction '" + action + 110 "' was cancelled"); 111 } 112 session.removeAttribute(Constants.SUBSCRIPTION_KEY); 113 return (mapping.findForward("success")); 114 } 115 116 Subscription subscription = 118 (Subscription) session.getAttribute(Constants.SUBSCRIPTION_KEY); 119 if ("Create".equals(action)) { 120 if (log.isTraceEnabled()) { 121 log.trace(" Creating subscription for mail server '" + 122 subform.getHost() + "'"); 123 } 124 subscription = 125 user.createSubscription(subform.getHost()); 126 } 127 if (subscription == null) { 128 if (log.isTraceEnabled()) { 129 log.trace(" Missing subscription for user '" + 130 user.getUsername() + "'"); 131 } 132 response.sendError(HttpServletResponse.SC_BAD_REQUEST, 133 messages.getMessage("error.noSubscription")); 134 return (null); 135 } 136 137 if (action.equals("Delete")) { 139 if (log.isTraceEnabled()) { 140 log.trace(" Deleting mail server '" + 141 subscription.getHost() + "' for user '" + 142 user.getUsername() + "'"); 143 } 144 user.removeSubscription(subscription); 145 session.removeAttribute(Constants.SUBSCRIPTION_KEY); 146 try { 147 UserDatabase database = (UserDatabase) 148 servlet.getServletContext(). 149 getAttribute(Constants.DATABASE_KEY); 150 database.save(); 151 } catch (Exception e) { 152 log.error("Database save", e); 153 } 154 return (mapping.findForward("success")); 155 } 156 157 159 if (log.isTraceEnabled()) { 161 log.trace(" Populating database from form bean"); 162 } 163 try { 164 PropertyUtils.copyProperties(subscription, subform); 165 } catch (InvocationTargetException e) { 166 Throwable t = e.getTargetException(); 167 if (t == null) 168 t = e; 169 log.error("Subscription.populate", t); 170 throw new ServletException ("Subscription.populate", t); 171 } catch (Throwable t) { 172 log.error("Subscription.populate", t); 173 throw new ServletException ("Subscription.populate", t); 174 } 175 176 try { 177 UserDatabase database = (UserDatabase) 178 servlet.getServletContext(). 179 getAttribute(Constants.DATABASE_KEY); 180 database.save(); 181 } catch (Exception e) { 182 log.error("Database save", e); 183 } 184 185 if (mapping.getAttribute() != null) { 187 if ("request".equals(mapping.getScope())) 188 request.removeAttribute(mapping.getAttribute()); 189 else 190 session.removeAttribute(mapping.getAttribute()); 191 } 192 session.removeAttribute(Constants.SUBSCRIPTION_KEY); 193 194 if (log.isTraceEnabled()) { 196 log.trace(" Forwarding to success page"); 197 } 198 return (mapping.findForward("success")); 199 200 } 201 202 203 } 204 | Popular Tags |