View Javadoc

1   /*
2    * $Id: UploadForm.java 421488 2006-07-13 03:43:08Z wsmoak $
3    *
4    * Copyright 1999-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.struts.webapp.upload;
20  
21  import javax.servlet.http.HttpServletRequest;
22  
23  import org.apache.struts.action.ActionMapping;
24  import org.apache.struts.action.ActionMessage;
25  import org.apache.struts.action.ActionMessages;
26  import org.apache.struts.action.ActionErrors;
27  import org.apache.struts.validator.ValidatorForm;
28  import org.apache.struts.upload.FormFile;
29  import org.apache.struts.upload.MultipartRequestHandler;
30  
31  /***
32   * This class is a placeholder for form values.  In a multipart request, files are represented by
33   * set and get methods that use the class org.apache.struts.upload.FormFile, an interface with
34   * basic methods to retrieve file information.  The actual structure of the FormFile is dependant
35   * on the underlying impelementation of multipart request handling.  The default implementation
36   * that struts uses is org.apache.struts.upload.CommonsMultipartRequestHandler.
37   *
38   * @version $Rev: 421488 $ $Date: 2006-07-12 20:43:08 -0700 (Wed, 12 Jul 2006) $
39   */
40  public class UploadForm extends ValidatorForm {
41  
42      /***
43       * The value of the text the user has sent as form data
44       */
45      protected String theText;
46  
47      /***
48       * The value of the embedded query string parameter
49       */
50      protected String queryParam;
51  
52      /***
53       * Whether or not to write to a file
54       */
55      protected boolean writeFile;
56  
57      /***
58       * The file that the user has uploaded
59       */
60      protected FormFile theFile;
61  
62      /***
63       * The file path to write to
64       */
65      protected String filePath;
66  
67      /***
68       * Retrieve the value of the text the user has sent as form data
69       */
70      public String getTheText() {
71          return theText;
72      }
73  
74      /***
75       * Set the value of the form data text
76       */
77      public void setTheText(String theText) {
78          this.theText = theText;
79      }
80  
81      /***
82       * Retrieve the value of the query string parameter
83       */
84      public String getQueryParam() {
85          return queryParam;
86      }
87  
88      /***
89       * Set the value of the query string parameter
90       */
91      public void setQueryParam(String queryParam) {
92          this.queryParam = queryParam;
93      }
94  
95      /***
96       * Retrieve a representation of the file the user has uploaded
97       */
98      public FormFile getTheFile() {
99          return theFile;
100     }
101 
102     /***
103      * Set a representation of the file the user has uploaded
104      */
105     public void setTheFile(FormFile theFile) {
106         this.theFile = theFile;
107     }
108 
109     /***
110      * Set whether or not to write to a file
111      */
112     public void setWriteFile(boolean writeFile) {
113         this.writeFile = writeFile;
114     }
115 
116     /***
117      * Get whether or not to write to a file
118      */
119     public boolean getWriteFile() {
120         return writeFile;
121     }
122 
123     /***
124      * Set the path to write a file to
125      */
126     public void setFilePath(String filePath) {
127         this.filePath = filePath;
128     }
129 
130     /***
131      * Get the path to write a file to
132      */
133     public String getFilePath() {
134         return filePath;
135     }
136 
137     public void reset() {
138         writeFile = false;
139     }
140 
141     /***
142      * Check to make sure the client hasn't exceeded the maximum allowed upload size inside of this
143      * validate method.
144      */
145     public ActionErrors validate(
146         ActionMapping mapping,
147         HttpServletRequest request) {
148 
149         ActionErrors errors = super.validate(mapping, request);
150 
151         //has the maximum length been exceeded?
152         Boolean maxLengthExceeded =
153             (Boolean) request.getAttribute(
154                 MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
155 
156         if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
157             if (errors == null) {
158                 errors = new ActionErrors();
159             }
160             errors.add(
161                 ActionMessages.GLOBAL_MESSAGE ,
162                 new ActionMessage("maxLengthExceeded"));
163             errors.add(
164                 ActionMessages.GLOBAL_MESSAGE ,
165                 new ActionMessage("maxLengthExplanation"));
166         }
167         return errors;
168 
169     }
170 }