001: /*
002: * $Id: DWRValidator.java 476642 2006-11-18 22:40:18Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.validators;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.struts2.dispatcher.ApplicationMap;
033: import org.apache.struts2.dispatcher.Dispatcher;
034: import org.apache.struts2.dispatcher.RequestMap;
035: import org.apache.struts2.dispatcher.SessionMap;
036:
037: import uk.ltd.getahead.dwr.WebContextFactory;
038:
039: import com.opensymphony.xwork2.Action;
040: import com.opensymphony.xwork2.ActionProxy;
041: import com.opensymphony.xwork2.DefaultActionInvocation;
042: import com.opensymphony.xwork2.DefaultActionProxy;
043: import com.opensymphony.xwork2.ObjectFactory;
044: import com.opensymphony.xwork2.UnknownHandler;
045: import com.opensymphony.xwork2.ValidationAware;
046: import com.opensymphony.xwork2.ValidationAwareSupport;
047: import com.opensymphony.xwork2.config.Configuration;
048: import com.opensymphony.xwork2.config.entities.ActionConfig;
049:
050: /**
051: * <p/>
052: * Use the dwr configuration as follows :-
053: *
054: * <pre>
055: * <!-- START SNIPPET: dwrConfiguration -->
056: *
057: * <dwr<
058: * <allow<
059: * <create creator="new" javascript="validator" class="org.apache.struts2.validators.DWRValidator"/<
060: * <convert converter="bean" match="com.opensymphony.xwork2.ValidationAwareSupport"/<
061: * </allow<
062: * </dwr<
063: *
064: * <!-- END SNIPPET: dwrConfiguration -->
065: * </pre>
066: */
067: public class DWRValidator {
068: private static final Log LOG = LogFactory
069: .getLog(DWRValidator.class);
070:
071: public ValidationAwareSupport doPost(String namespace,
072: String action, Map params) throws Exception {
073: HttpServletRequest req = WebContextFactory.get()
074: .getHttpServletRequest();
075: ServletContext servletContext = WebContextFactory.get()
076: .getServletContext();
077: HttpServletResponse res = WebContextFactory.get()
078: .getHttpServletResponse();
079:
080: Map requestParams = new HashMap(req.getParameterMap());
081: if (params != null) {
082: requestParams.putAll(params);
083: } else {
084: params = requestParams;
085: }
086: Map requestMap = new RequestMap(req);
087: Map session = new SessionMap(req);
088: Map application = new ApplicationMap(servletContext);
089: Dispatcher du = Dispatcher.getInstance();
090: HashMap ctx = du.createContextMap(requestMap, params, session,
091: application, req, res, servletContext);
092:
093: try {
094: Configuration cfg = du.getConfigurationManager()
095: .getConfiguration();
096: ValidatorActionProxy proxy = new ValidatorActionProxy(
097: namespace, action, ctx);
098: cfg.getContainer().inject(proxy);
099: proxy.prepare();
100: proxy.execute();
101: Object a = proxy.getAction();
102:
103: if (a instanceof ValidationAware) {
104: ValidationAware aware = (ValidationAware) a;
105: ValidationAwareSupport vas = new ValidationAwareSupport();
106: vas.setActionErrors(aware.getActionErrors());
107: vas.setActionMessages(aware.getActionMessages());
108: vas.setFieldErrors(aware.getFieldErrors());
109:
110: return vas;
111: } else {
112: return null;
113: }
114: } catch (Exception e) {
115: LOG.error("Error while trying to validate", e);
116: return null;
117: }
118: }
119:
120: public static class ValidatorActionInvocation extends
121: DefaultActionInvocation {
122: private static final long serialVersionUID = -7645433725470191275L;
123:
124: protected ValidatorActionInvocation(
125: ObjectFactory objectFactory, UnknownHandler handler,
126: ActionProxy proxy, Map extraContext) throws Exception {
127: super (objectFactory, handler, proxy, extraContext, true);
128: }
129:
130: protected String invokeAction(Object action,
131: ActionConfig actionConfig) throws Exception {
132: return Action.NONE; // don't actually execute the action
133: }
134: }
135:
136: public static class ValidatorActionProxy extends DefaultActionProxy {
137: private static final long serialVersionUID = 5754781916414047963L;
138:
139: protected ValidatorActionProxy(String namespace,
140: String actionName, Map extraContext) throws Exception {
141: super (namespace, actionName, extraContext, false, true);
142: }
143:
144: public void prepare() throws Exception {
145: super .prepare();
146: invocation = new ValidatorActionInvocation(objectFactory,
147: unknownHandler, this, extraContext);
148: }
149: }
150: }
|