1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.validators;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import javax.servlet.ServletContext;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.struts2.dispatcher.ApplicationMap;
30 import org.apache.struts2.dispatcher.Dispatcher;
31 import org.apache.struts2.dispatcher.RequestMap;
32 import org.apache.struts2.dispatcher.SessionMap;
33
34 import uk.ltd.getahead.dwr.WebContextFactory;
35
36 import com.opensymphony.xwork2.Action;
37 import com.opensymphony.xwork2.ActionProxy;
38 import com.opensymphony.xwork2.DefaultActionInvocation;
39 import com.opensymphony.xwork2.DefaultActionProxy;
40 import com.opensymphony.xwork2.ValidationAware;
41 import com.opensymphony.xwork2.ValidationAwareSupport;
42 import com.opensymphony.xwork2.config.Configuration;
43 import com.opensymphony.xwork2.config.entities.ActionConfig;
44
45 /***
46 * <p/>
47 * Use the dwr configuration as follows :-
48 *
49 * <pre>
50 * <!-- START SNIPPET: dwrConfiguration -->
51 *
52 * <dwr<
53 * <allow<
54 * <create creator="new" javascript="validator" class="org.apache.struts2.validators.DWRValidator"/<
55 * <convert converter="bean" match="com.opensymphony.xwork2.ValidationAwareSupport"/<
56 * </allow<
57 * </dwr<
58 *
59 * <!-- END SNIPPET: dwrConfiguration -->
60 * </pre>
61 */
62 public class DWRValidator {
63 private static final Log LOG = LogFactory.getLog(DWRValidator.class);
64
65 public ValidationAwareSupport doPost(String namespace, String action, Map params) throws Exception {
66 HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
67 ServletContext servletContext = WebContextFactory.get().getServletContext();
68 HttpServletResponse res = WebContextFactory.get().getHttpServletResponse();
69
70 Map requestParams = new HashMap(req.getParameterMap());
71 if (params != null) {
72 requestParams.putAll(params);
73 } else {
74 params = requestParams;
75 }
76 Map requestMap = new RequestMap(req);
77 Map session = new SessionMap(req);
78 Map application = new ApplicationMap(servletContext);
79 Dispatcher du = Dispatcher.getInstance();
80 HashMap ctx = du.createContextMap(requestMap,
81 params,
82 session,
83 application,
84 req,
85 res,
86 servletContext);
87
88 try {
89 Configuration cfg = du.getConfigurationManager().getConfiguration();
90 ValidatorActionProxy proxy = new ValidatorActionProxy(cfg, namespace, action, ctx);
91 proxy.execute();
92 Object a = proxy.getAction();
93
94 if (a instanceof ValidationAware) {
95 ValidationAware aware = (ValidationAware) a;
96 ValidationAwareSupport vas = new ValidationAwareSupport();
97 vas.setActionErrors(aware.getActionErrors());
98 vas.setActionMessages(aware.getActionMessages());
99 vas.setFieldErrors(aware.getFieldErrors());
100
101 return vas;
102 } else {
103 return null;
104 }
105 } catch (Exception e) {
106 LOG.error("Error while trying to validate", e);
107 return null;
108 }
109 }
110
111 public static class ValidatorActionInvocation extends DefaultActionInvocation {
112 private static final long serialVersionUID = -7645433725470191275L;
113
114 protected ValidatorActionInvocation(ActionProxy proxy, Map extraContext) throws Exception {
115 super(proxy, extraContext, true);
116 }
117
118 protected String invokeAction(Object action, ActionConfig actionConfig) throws Exception {
119 return Action.NONE;
120 }
121 }
122
123 public static class ValidatorActionProxy extends DefaultActionProxy {
124 private static final long serialVersionUID = 5754781916414047963L;
125
126 protected ValidatorActionProxy(Configuration config, String namespace, String actionName, Map extraContext) throws Exception {
127 super(config, namespace, actionName, extraContext, false, true);
128 }
129
130 protected void prepare() throws Exception {
131 invocation = new ValidatorActionInvocation(this, extraContext);
132 }
133 }
134 }