View Javadoc

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