1 4 package com.opensymphony.webwork.validators; 5 6 import com.opensymphony.webwork.WebWorkStatics; 7 import com.opensymphony.xwork.*; 8 import com.opensymphony.xwork.config.entities.ActionConfig; 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import uk.ltd.getahead.dwr.ExecutionContext; 12 13 import javax.servlet.ServletConfig ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 29 public class DWRValidator { 30 private static final Log LOG = LogFactory.getLog(DWRValidator.class); 31 32 public ValidationAwareSupport doPost(String namespace, String action, Map params) throws Exception { 33 ServletConfig sc = ExecutionContext.get().getServletConfig(); 34 HashMap ctx = new HashMap (); 35 36 ctx.put(ActionContext.PARAMETERS, params); 37 ctx.put(WebWorkStatics.SERVLET_CONTEXT, sc.getServletContext()); 38 39 try { 40 ValidatorActionProxy proxy = new ValidatorActionProxy(namespace, action, ctx); 41 proxy.execute(); 42 Object a = proxy.getAction(); 43 44 if (a instanceof ValidationAware) { 45 ValidationAware aware = (ValidationAware) a; 46 ValidationAwareSupport vas = new ValidationAwareSupport(); 47 vas.setActionErrors(aware.getActionErrors()); 48 vas.setActionMessages(aware.getActionMessages()); 49 vas.setFieldErrors(aware.getFieldErrors()); 50 51 return vas; 52 } else { 53 return null; 54 } 55 } catch (Exception e) { 56 LOG.error("Error while trying to validate", e); 57 return null; 58 } 59 } 60 61 public static class ValidatorActionInvocation extends DefaultActionInvocation { 62 protected ValidatorActionInvocation(ActionProxy proxy, Map extraContext) throws Exception { 63 super(proxy, extraContext, true); 64 } 65 66 protected String invokeAction(Action action, ActionConfig actionConfig) throws Exception { 67 return Action.NONE; } 69 } 70 71 public static class ValidatorActionProxy extends DefaultActionProxy { 72 protected ValidatorActionProxy(String namespace, String actionName, Map extraContext) throws Exception { 73 super(namespace, actionName, extraContext, false); 74 } 75 76 protected void prepare() throws Exception { 77 invocation = new ValidatorActionInvocation(this, extraContext); 78 } 79 } 80 } 81 | Popular Tags |