KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > validators > DWRValidator


1 /*
2  * Copyright (c) 2005 Opensymphony. All Rights Reserved.
3  */

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 JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 /**
18  * User: plightbo
19  * Date: Dec 11, 2004
20  * Time: 6:17:58 PM
21  * <p/>
22  * <dwr>
23  * <allow>
24  * <create creator="new" javascript="validator" class="com.opensymphony.webwork.validators.DWRValidator"/>
25  * <convert converter="bean" match="com.opensymphony.xwork.ValidationAwareSupport"/>
26  * </allow>
27  * </dwr>
28  */

29 public class DWRValidator {
30     private static final Log LOG = LogFactory.getLog(DWRValidator.class);
31
32     public ValidationAwareSupport doPost(String JavaDoc namespace, String JavaDoc action, Map JavaDoc params) throws Exception JavaDoc {
33         ServletConfig JavaDoc sc = ExecutionContext.get().getServletConfig();
34         HashMap JavaDoc ctx = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc extraContext) throws Exception JavaDoc {
63             super(proxy, extraContext, true);
64         }
65
66         protected String JavaDoc invokeAction(Action action, ActionConfig actionConfig) throws Exception JavaDoc {
67             return Action.NONE; // don't actually execute the action
68
}
69     }
70
71     public static class ValidatorActionProxy extends DefaultActionProxy {
72         protected ValidatorActionProxy(String JavaDoc namespace, String JavaDoc actionName, Map JavaDoc extraContext) throws Exception JavaDoc {
73             super(namespace, actionName, extraContext, false);
74         }
75
76         protected void prepare() throws Exception JavaDoc {
77             invocation = new ValidatorActionInvocation(this, extraContext);
78         }
79     }
80 }
81
Popular Tags