001: /*
002: * Copyright (c) 2005 Opensymphony. All Rights Reserved.
003: */
004: package com.opensymphony.webwork.validators;
005:
006: import com.opensymphony.webwork.dispatcher.ApplicationMap;
007: import com.opensymphony.webwork.dispatcher.DispatcherUtils;
008: import com.opensymphony.webwork.dispatcher.RequestMap;
009: import com.opensymphony.webwork.dispatcher.SessionMap;
010: import com.opensymphony.xwork.*;
011: import com.opensymphony.xwork.config.entities.ActionConfig;
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import uk.ltd.getahead.dwr.ExecutionContext;
015:
016: import javax.servlet.ServletContext;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: /**
023: * User: plightbo
024: * Date: Dec 11, 2004
025: * Time: 6:17:58 PM
026: * <p/>
027: * <dwr>
028: * <allow>
029: * <create creator="new" javascript="validator" class="com.opensymphony.webwork.validators.DWRValidator"/>
030: * <convert converter="bean" match="com.opensymphony.xwork.ValidationAwareSupport"/>
031: * </allow>
032: * </dwr>
033: */
034: public class DWRValidator {
035: private static final Log LOG = LogFactory
036: .getLog(DWRValidator.class);
037:
038: public ValidationAwareSupport doPost(String namespace,
039: String action, Map params) throws Exception {
040: HttpServletRequest req = ExecutionContext.get()
041: .getHttpServletRequest();
042: ServletContext servletContext = ExecutionContext.get()
043: .getServletContext();
044: HttpServletResponse res = ExecutionContext.get()
045: .getHttpServletResponse();
046:
047: Map requestParams = new HashMap(req.getParameterMap());
048: if (params != null) {
049: requestParams.putAll(params);
050: } else {
051: params = requestParams;
052: }
053: Map requestMap = new RequestMap(req);
054: Map session = new SessionMap(req);
055: Map application = new ApplicationMap(servletContext);
056: DispatcherUtils du = DispatcherUtils.getInstance();
057: HashMap ctx = du.createContextMap(requestMap, params, session,
058: application, req, res, servletContext);
059:
060: try {
061: ValidatorActionProxy proxy = new ValidatorActionProxy(
062: namespace, action, ctx);
063: proxy.execute();
064: Object a = proxy.getAction();
065:
066: if (a instanceof ValidationAware) {
067: ValidationAware aware = (ValidationAware) a;
068: ValidationAwareSupport vas = new ValidationAwareSupport();
069: vas.setActionErrors(aware.getActionErrors());
070: vas.setActionMessages(aware.getActionMessages());
071: vas.setFieldErrors(aware.getFieldErrors());
072:
073: return vas;
074: } else {
075: return null;
076: }
077: } catch (Exception e) {
078: LOG.error("Error while trying to validate", e);
079: return null;
080: }
081: }
082:
083: public static class ValidatorActionInvocation extends
084: DefaultActionInvocation {
085: protected ValidatorActionInvocation(ActionProxy proxy,
086: Map extraContext) throws Exception {
087: super (proxy, extraContext, true);
088: }
089:
090: protected String invokeAction(Object action,
091: ActionConfig actionConfig) throws Exception {
092: return Action.NONE; // don't actually execute the action
093: }
094: }
095:
096: public static class ValidatorActionProxy extends DefaultActionProxy {
097: protected ValidatorActionProxy(String namespace,
098: String actionName, Map extraContext) throws Exception {
099: super (namespace, actionName, extraContext, false, true);
100: }
101:
102: protected void prepare() throws Exception {
103: invocation = new ValidatorActionInvocation(this,
104: extraContext);
105: }
106: }
107: }
|