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