View Javadoc

1   /*
2    * $Id: DWRValidator.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * &lt;dwr&lt;
53   *    &lt;allow&lt;
54   *      &lt;create creator="new" javascript="validator" class="org.apache.struts2.validators.DWRValidator"/&lt;
55   *      &lt;convert converter="bean" match="com.opensymphony.xwork2.ValidationAwareSupport"/&lt;
56   *    &lt;/allow&lt;
57   * &lt;/dwr&lt;
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; // don't actually execute the action
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 }