1 /*
2 * $Id$
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
22 package org.apache.struts.webapp.upload;
23
24 import jakarta.servlet.http.HttpServletRequest;
25
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionMessage;
28 import org.apache.struts.action.ActionMessages;
29 import org.apache.struts.action.ActionErrors;
30 import org.apache.struts.validator.ValidatorForm;
31 import org.apache.struts.upload.FormFile;
32 import org.apache.struts.upload.MultipartRequestHandler;
33
34 /**
35 * This class is a placeholder for form values. In a multipart request, files are represented by
36 * set and get methods that use the class org.apache.struts.upload.FormFile, an interface with
37 * basic methods to retrieve file information. The actual structure of the FormFile is dependant
38 * on the underlying impelementation of multipart request handling. The default implementation
39 * that struts uses is org.apache.struts.upload.CommonsMultipartRequestHandler.
40 *
41 * @version $Rev$ $Date$
42 */
43 public class UploadForm extends ValidatorForm {
44 private static final long serialVersionUID = 9109939239360280904L;
45
46 /**
47 * The value of the text the user has sent as form data
48 */
49 protected String theText;
50
51 /**
52 * The value of the embedded query string parameter
53 */
54 protected String queryParam;
55
56 /**
57 * Whether or not to write to a file
58 */
59 protected boolean writeFile;
60
61 /**
62 * The file that the user has uploaded
63 */
64 protected FormFile theFile;
65
66 /**
67 * The file path to write to
68 */
69 protected String filePath;
70
71 /**
72 * Retrieve the value of the text the user has sent as form data
73 */
74 public String getTheText() {
75 return theText;
76 }
77
78 /**
79 * Set the value of the form data text
80 */
81 public void setTheText(String theText) {
82 this.theText = theText;
83 }
84
85 /**
86 * Retrieve the value of the query string parameter
87 */
88 public String getQueryParam() {
89 return queryParam;
90 }
91
92 /**
93 * Set the value of the query string parameter
94 */
95 public void setQueryParam(String queryParam) {
96 this.queryParam = queryParam;
97 }
98
99 /**
100 * Retrieve a representation of the file the user has uploaded
101 */
102 public FormFile getTheFile() {
103 return theFile;
104 }
105
106 /**
107 * Set a representation of the file the user has uploaded
108 */
109 public void setTheFile(FormFile theFile) {
110 this.theFile = theFile;
111 }
112
113 /**
114 * Set whether or not to write to a file
115 */
116 public void setWriteFile(boolean writeFile) {
117 this.writeFile = writeFile;
118 }
119
120 /**
121 * Get whether or not to write to a file
122 */
123 public boolean getWriteFile() {
124 return writeFile;
125 }
126
127 /**
128 * Set the path to write a file to
129 */
130 public void setFilePath(String filePath) {
131 this.filePath = filePath;
132 }
133
134 /**
135 * Get the path to write a file to
136 */
137 public String getFilePath() {
138 return filePath;
139 }
140
141 public void reset() {
142 writeFile = false;
143 }
144
145 /**
146 * Check to make sure the client hasn't exceeded the maximum allowed upload size inside of this
147 * validate method.
148 */
149 public ActionErrors validate(
150 ActionMapping mapping,
151 HttpServletRequest request) {
152
153 ActionErrors errors = super.validate(mapping, request);
154
155 //has the maximum length been exceeded?
156 Boolean maxLengthExceeded =
157 (Boolean) request.getAttribute(
158 MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
159
160 if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
161 if (errors == null) {
162 errors = new ActionErrors();
163 }
164 errors.add(
165 ActionMessages.GLOBAL_MESSAGE ,
166 new ActionMessage("maxLengthExceeded"));
167 errors.add(
168 ActionMessages.GLOBAL_MESSAGE ,
169 new ActionMessage("maxLengthExplanation"));
170 }
171 return errors;
172
173 }
174 }