1 64 65 package com.jcorporate.expresso.core.controller; 66 67 import com.jcorporate.expresso.core.misc.StringUtil; 68 import com.jcorporate.expresso.core.misc.URLUTF8Encoder; 69 import org.apache.commons.logging.Log; 70 import org.apache.commons.logging.LogFactory; 71 import org.apache.commons.validator.Validator; 72 import org.apache.commons.validator.ValidatorException; 73 import org.apache.commons.validator.ValidatorResources; 74 import org.apache.commons.validator.ValidatorResults; 75 import org.apache.struts.action.ActionErrors; 76 import org.apache.struts.action.ActionForm; 77 import org.apache.struts.action.ActionMapping; 78 import org.apache.struts.validator.Resources; 79 80 import javax.servlet.ServletContext ; 81 import javax.servlet.ServletRequest ; 82 import javax.servlet.http.HttpServletRequest ; 83 import java.io.Serializable ; 84 import java.util.Enumeration ; 85 import java.util.Hashtable ; 86 import java.util.Iterator ; 87 import java.util.Locale ; 88 import java.util.StringTokenizer ; 89 90 91 99 public class DefaultForm 100 extends ActionForm 101 implements Serializable , StateForm { 102 private Hashtable formData = new Hashtable (); 103 private Hashtable formAttributes = null; 104 105 public DefaultForm() { 106 } 107 108 public ActionErrors validate(ActionMapping mapping, ServletRequest request) { 109 return null; 110 } 111 112 public void saveForm(ControllerRequest req) 113 throws ControllerException { 114 formData.clear(); 115 formData.putAll(req.getParameters()); 116 } 117 118 public void restoreForm(ControllerRequest req) 119 throws ControllerException { 120 String oneKey = null; 121 122 for (Enumeration e = formData.elements(); e.hasMoreElements();) { 123 oneKey = (String ) e.nextElement(); 124 req.setParameter(oneKey, (String ) formData.get(oneKey)); 125 } 126 } 127 128 public Hashtable getFields() { 129 return formData; 130 } 131 132 public void clear() { 133 formData.clear(); 134 } 135 136 public synchronized void setAttribute(String fieldName, Object newValue) { 137 if (formAttributes == null) { 138 formAttributes = new Hashtable (); 139 } 140 141 formAttributes.put(fieldName, newValue); 142 } 143 144 public synchronized void setField(String fieldName, String fieldValue) 145 throws ControllerException { 146 147 if (fieldName == null || fieldName.length() == 0) { 148 throw new ControllerException("Field name cannot be null here"); 149 } 150 formData.put(fieldName, fieldValue); 151 } 152 153 156 public synchronized void setUsingHashtableParameters(Hashtable parameters) throws ControllerException { 157 for (Iterator i = parameters.keySet().iterator(); i.hasNext();) { 158 String key = (String ) i.next(); 159 setField(key, (String ) parameters.get(key)); 160 } 161 } 162 163 169 public Object getAttribute(String fieldName) throws ControllerException { 170 if (formAttributes == null) { 171 return null; 172 } 173 174 if (fieldName == null || fieldName.length() == 0) { 175 throw new ControllerException("Field name cannot be null here"); 176 } 177 178 return formAttributes.get(fieldName); 179 180 } 181 182 185 public Hashtable getAttributes() { 186 187 return formAttributes; 188 189 } 190 191 public String getField(String fieldName) 192 throws ControllerException { 193 if (StringUtil.notNull(fieldName).equals("")) { 194 throw new ControllerException("Field name cannot be null here"); 195 } 196 197 return (String ) formData.get(fieldName); 198 } 199 200 211 214 public ActionErrors validate(ActionMapping mapping, 215 HttpServletRequest request) { 216 217 ServletContext application = getServlet().getServletContext(); 218 ActionErrors errors = new ErrorCollection(); 219 220 try { 221 Validator validator = 222 initValidator(getState(request), this, 223 application, request, 224 errors, page); 225 226 validatorResults = validator.validate(); 227 } catch (ControllerException ex) { 228 } catch (ValidatorException e) { 229 log.error(e.getMessage(), e); 230 } 231 232 return errors; 233 } 234 235 246 249 public ErrorCollection validate(ControllerRequest request) { 250 if (request instanceof ServletControllerRequest) { 251 ServletControllerRequest scr = (ServletControllerRequest) request; 252 return (ErrorCollection) this.validate(scr.getMapping(), (HttpServletRequest ) scr.getServletRequest()); 253 } else { 254 return new ErrorCollection(); 255 } 256 } 257 258 259 265 266 269 private static String getState(HttpServletRequest request) throws 270 ControllerException { 271 272 log.debug("Adding button parameters"); 273 274 String oneParamName = null; 275 Object oneParamValue = null; 276 boolean gotControllerFromButton = false; 277 278 for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) { 279 oneParamName = (String ) e.nextElement(); 280 oneParamValue = StringUtil.notNull(request.getParameter(oneParamName)); 281 282 if (oneParamName.startsWith("button_")) { 283 284 String buttonName = oneParamName.substring(oneParamName.indexOf("_") + 1); 287 288 296 if (buttonName.endsWith(".x")) { 297 buttonName = buttonName.substring(0, buttonName.length() - 2); 298 } 299 if (buttonName.endsWith(".y")) { 300 buttonName = buttonName.substring(0, buttonName.length() - 2); 301 } 302 303 if (log.isDebugEnabled()) { 304 log.debug("There is a button parameter called '" + 305 buttonName + "'"); 306 } 307 308 309 String paramString = (String ) request.getParameter(buttonName + 310 "_params"); 311 312 if (paramString != null) { 313 log.debug("Button parameters:"); 314 315 String encodeType = (String ) request.getParameter(buttonName + 318 "_encoding"); 319 320 if ("u".equals(encodeType)) { 321 322 try { 324 paramString = URLUTF8Encoder.decode(paramString); 325 } catch (Exception ex) { 326 log.error("Could not URLDecode: " + paramString, 327 ex); 328 } 329 } 330 331 332 333 StringTokenizer stkpm = null; 334 stkpm = new StringTokenizer (paramString, "&"); 335 336 while (stkpm.hasMoreTokens()) { 337 String pairValue = stkpm.nextToken(); 338 339 try { 342 pairValue = URLUTF8Encoder.decode(pairValue); 343 } catch (Exception ex) { 344 log.error("Could not URLDecode: " + pairValue, ex); 345 } 346 347 String paramName = pairValue; 348 String paramValue = ""; 349 int position = pairValue.indexOf("="); 350 if (position != -1) { 351 paramName = pairValue.substring(0, position); 352 position++; if (position < pairValue.length()) { 354 paramValue = pairValue.substring(position); 355 } 356 } 357 358 if (paramName.equals(Controller.CONTROLLER_PARAM_KEY)) { 359 gotControllerFromButton = true; 360 } 361 if (log.isDebugEnabled()) { 362 log.debug("Parameter '" + paramName + "', value '" 363 + paramValue + "'"); 364 } 365 if (paramName.equals(Controller.STATE_PARAM_KEY)) { 366 return paramValue; 367 } 368 369 } 370 371 372 log.debug("End button parameters"); 373 } else { 374 throw new ControllerException("Button '" + buttonName + 375 "' was clicked, but no " + 376 "button parameters field called '" + 377 buttonName + 378 "_params' was found."); 379 } 380 } 381 382 383 } 384 385 386 return ""; 387 } 388 389 402 405 public Validator initValidator(String key, 406 Object bean, 407 ServletContext application, 408 HttpServletRequest request, 409 ActionErrors errors, 410 int page) { 411 412 ValidatorResources resources = Resources.getValidatorResources(application, 413 request); 414 415 Locale locale = Resources.getLocale(request); 416 417 Validator validator = new Validator(resources, key); 418 validator.setUseContextClassLoader(true); 419 420 validator.setPage(page); 421 422 validator.addResource(Resources. 423 SERVLET_CONTEXT_KEY, application); 424 validator.addResource(Resources. 425 HTTP_SERVLET_REQUEST_KEY, request); 426 validator.addResource(Validator.LOCALE_KEY, locale); 427 validator.addResource(Resources. 428 ACTION_ERRORS_KEY, errors); 429 validator.addResource(Validator.BEAN_KEY, bean); 430 431 return validator; 432 } 433 434 437 440 protected int page = 0; 441 442 446 449 protected ValidatorResults validatorResults = null; 450 451 454 457 private static Log log = LogFactory.getLog(DefaultForm.class); 458 459 } 460 | Popular Tags |