View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.struts.chain.commands.servlet;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.struts.action.ActionErrors;
21  import org.apache.struts.action.ActionForm;
22  import org.apache.struts.action.ActionMapping;
23  import org.apache.struts.chain.commands.AbstractValidateActionForm;
24  import org.apache.struts.chain.contexts.ActionContext;
25  import org.apache.struts.chain.contexts.ServletActionContext;
26  import org.apache.struts.config.ActionConfig;
27  
28  /***
29   * <p>Validate the properties of the form bean for this request.  If there are
30   * any validation errors, execute the child commands in our chain; otherwise,
31   * proceed normally.  Also, if any errors are found and the request is a
32   * multipart request, rollback the <code>MultipartRequestHandler</code>.</p>
33   *
34   * @version $Rev: 421119 $ $Date: 2005-06-04 10:58:46 -0400 (Sat, 04 Jun 2005)
35   *          $
36   */
37  public class ValidateActionForm extends AbstractValidateActionForm {
38      // ------------------------------------------------------ Instance Variables
39      private static final Log log = LogFactory.getLog(ValidateActionForm.class);
40  
41      // ------------------------------------------------------- Protected Methods
42  
43      /***
44       * <p>Call the <code>validate()</code> method of the specified form bean,
45       * and return the resulting <code>ActionErrors</code> object.</p>
46       *
47       * @param context    The context for this request
48       * @param actionForm The form bean for this request
49       */
50      protected ActionErrors validate(ActionContext context,
51          ActionConfig actionConfig, ActionForm actionForm) {
52          ServletActionContext saContext = (ServletActionContext) context;
53          ActionErrors errors =
54              (actionForm.validate((ActionMapping) actionConfig,
55                  saContext.getRequest()));
56  
57          // Special handling for multipart request
58          if ((errors != null) && !errors.isEmpty()) {
59              if (actionForm.getMultipartRequestHandler() != null) {
60                  if (log.isTraceEnabled()) {
61                      log.trace("  Rolling back multipart request");
62                  }
63  
64                  actionForm.getMultipartRequestHandler().rollback();
65              }
66          }
67  
68          return errors;
69      }
70  }