1 7 package com.inversoft.verge.mvc.controller.form; 8 9 10 import java.lang.reflect.Method ; 11 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 15 import org.apache.log4j.Logger; 16 17 import com.inversoft.beans.BeanException; 18 import com.inversoft.util.ReflectionException; 19 import com.inversoft.util.ReflectionTools; 20 import com.inversoft.verge.mvc.MVCConstants; 21 import com.inversoft.verge.mvc.MVCException; 22 import com.inversoft.verge.mvc.MVCRequest; 23 import com.inversoft.verge.mvc.controller.Action; 24 import com.inversoft.verge.mvc.controller.ControllerHandler; 25 import com.inversoft.verge.mvc.controller.DefaultLongTxnHandler; 26 import com.inversoft.verge.mvc.controller.GenericResult; 27 import com.inversoft.verge.mvc.controller.LongTxnHandler; 28 import com.inversoft.verge.mvc.controller.LongTxnSetup; 29 import com.inversoft.verge.mvc.controller.Result; 30 import com.inversoft.verge.mvc.controller.form.config.ActionConfig; 31 import com.inversoft.verge.mvc.controller.form.config.FormConfig; 32 import com.inversoft.verge.mvc.controller.form.config.FormConfigStruct; 33 import com.inversoft.verge.mvc.controller.form.config.FormMVCConfigRegistry; 34 import com.inversoft.verge.mvc.controller.form.config.MappingConfig; 35 import com.inversoft.verge.mvc.controller.form.config.ValidatorConfig; 36 37 38 52 public class FormControllerHandler implements ControllerHandler { 53 54 57 private static final Logger logger = Logger.getLogger(FormControllerHandler.class); 58 59 62 private volatile LongTxnHandler handler = new DefaultLongTxnHandler(); 63 64 65 81 public void registerHandler(LongTxnHandler handler) { 82 this.handler = handler; 83 } 84 85 88 public void unregisterHandler() { 89 this.handler = null; 90 } 91 92 99 public void preExecute(MVCRequest mvcRequest) throws MVCException { 100 101 FormMVCMetaData md = FormURLTools.decodeURL( 103 mvcRequest.getControllerInfo().getURLValues()); 104 105 HttpServletRequest request = mvcRequest.getRequest(); 106 if (md.getAction() == null) { 107 FormURLTools.locateAction(request, md); 108 if (md.getAction() == null) { 109 throw new MVCException("Unable to locate valid action"); 110 } 111 } 112 113 FormConfig config = null; 114 if (md.getForm() != null) { 115 config = (FormConfig) md.findFormConfig(request); 116 } 117 118 String actStr = md.getAction(); 119 ActionConfig actionConfig = 120 FormMVCConfigRegistry.getInstance(request).lookupAction(actStr, 121 config); 122 if (actionConfig == null) { 123 if (config == null) { 124 throw new MVCException("Invalid action named: " + actStr); 125 } else { 126 throw new MVCException("Invalid action named: " + actStr + 127 " for form named: " + config.getName()); 128 } 129 } 130 131 FormConfigStruct struct = new FormConfigStruct(config, actionConfig, md); 133 mvcRequest.setConfiguration(struct); 134 135 if (config != null) { 136 mvcRequest.setModelEnabled(actionConfig.isModelEnabled()); 137 mvcRequest.setValidationEnabled(actionConfig.isValidationEnabled()); 138 mvcRequest.addValidatorHandlerToCall(MVCConstants.FORM_NAME); 139 } 140 141 HttpServletResponse response = mvcRequest.getResponse(); 143 FormAction formAction = new FormAction(config, actStr, request, response, 144 mvcRequest.getRequestContext()); 145 mvcRequest.setAction(formAction); 146 } 147 148 153 public Result execute(MVCRequest mvcRequest) throws MVCException { 154 155 HttpServletRequest request = mvcRequest.getRequest(); 156 HttpServletResponse response = mvcRequest.getResponse(); 157 FormConfigStruct struct = (FormConfigStruct) mvcRequest.getConfiguration(); 158 FormConfig config = (FormConfig) struct.baseFormConfig; 159 160 if (struct.failedValidatorConfig != null) { 162 return ((ValidatorConfig) struct.failedValidatorConfig).getFailureMapping(); 163 } 164 165 ActionConfig actionConfig = struct.actionConfig; 167 Action action = mvcRequest.getAction(); 168 assert (actionConfig != null) : "actionConfig == null"; 169 170 preHandle(request, response, config, actionConfig, action); 171 172 Object result = null; 173 try { 174 result = actionConfig.getHandle().invokeHandle(action, request); 175 } catch (BeanException be) { 176 throw new MVCException(be); 177 } 178 179 return postHandle(request, response, config, actionConfig, action, result); 180 } 181 182 185 void preHandle(HttpServletRequest request, HttpServletResponse response, 186 FormConfig config, ActionConfig actionConfig, Action action) 187 throws MVCException { 188 189 Method preHandle = actionConfig.getPreHandleMethod(); 191 if (preHandle != null) { 192 Object handler; 193 try { 194 handler = actionConfig.getHandle().getWebBean().getInstance(request); 195 } catch (BeanException be) { 196 throw new MVCException(be); 197 } 198 199 try { 200 ReflectionTools.invokeMethod(preHandle, handler, 201 new Object []{action}); 202 } catch (ReflectionException re) { 203 throw new MVCException(re); 204 } 205 } 206 207 if (actionConfig.isLongTxnEnabled()) { 209 String url = actionConfig.getLongTxnStartURL(); 210 if (url == null) { 211 if (config != null) { 212 LongTxnSetup setup = config.getLongTxnSetup(); 213 if (setup != null) { 214 url = setup.getLongTxnStartURL(action); 215 } 216 } 217 } 218 219 if (handler != null) { 220 handler.handleStartLongTxn(request, response, url); 221 } 222 } 223 } 224 225 229 Result postHandle(HttpServletRequest request, HttpServletResponse response, 230 FormConfig config, ActionConfig actionConfig, Action action, 231 Object result) 232 throws MVCException { 233 234 Method postHandle = actionConfig.getPostHandleMethod(); 236 if (postHandle != null) { 237 238 Object handler; 239 try { 240 handler = actionConfig.getHandle().getWebBean().getInstance(request); 241 } catch (BeanException be) { 242 throw new MVCException(be); 243 } 244 245 try { 246 ReflectionTools.invokeMethod(postHandle, handler, 247 new Object []{action, result}); 248 } catch (ReflectionException re) { 249 throw new MVCException(re); 250 } 251 } 252 253 Result ret = null; 255 if (result != null && result instanceof String ) { 256 257 if (logger.isDebugEnabled()) { 258 logger.debug("Looking up mapping named: " + result); 259 } 260 261 MappingConfig mapping = 263 FormMVCConfigRegistry.getInstance(request).lookupMapping( 264 (String ) result, config); 265 if (mapping == null) { 266 throw new MVCException("Invalid mapping name: " + result); 267 } 268 269 ret = mapping; 270 } 271 272 if (actionConfig.isLongTxnEnabled() && handler != null) { 274 String url = actionConfig.getLongTxnEndURL(); 275 if (url == null) { 276 if (config != null) { 277 LongTxnSetup setup = config.getLongTxnSetup(); 278 if (setup != null) { 279 url = setup.getLongTxnEndURL(action); 280 } 281 } 282 } 283 284 if (ret != null && ret.isForward()) { 287 ret = new GenericResult(ret.getURL(), ret.getCategory(), false); 288 } 289 290 handler.handleEndLongTxn(request, response, url, ret); 291 292 ret = null; 294 } 295 296 return ret; 297 } 298 } | Popular Tags |