001: /*
002: * $Id: UploadForm.java 471754 2006-11-06 14:55:09Z husted $
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:
022: package org.apache.struts.webapp.upload;
023:
024: import javax.servlet.http.HttpServletRequest;
025:
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.struts.action.ActionMessage;
028: import org.apache.struts.action.ActionMessages;
029: import org.apache.struts.action.ActionErrors;
030: import org.apache.struts.validator.ValidatorForm;
031: import org.apache.struts.upload.FormFile;
032: import org.apache.struts.upload.MultipartRequestHandler;
033:
034: /**
035: * This class is a placeholder for form values. In a multipart request, files are represented by
036: * set and get methods that use the class org.apache.struts.upload.FormFile, an interface with
037: * basic methods to retrieve file information. The actual structure of the FormFile is dependant
038: * on the underlying impelementation of multipart request handling. The default implementation
039: * that struts uses is org.apache.struts.upload.CommonsMultipartRequestHandler.
040: *
041: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
042: */
043: public class UploadForm extends ValidatorForm {
044:
045: /**
046: * The value of the text the user has sent as form data
047: */
048: protected String theText;
049:
050: /**
051: * The value of the embedded query string parameter
052: */
053: protected String queryParam;
054:
055: /**
056: * Whether or not to write to a file
057: */
058: protected boolean writeFile;
059:
060: /**
061: * The file that the user has uploaded
062: */
063: protected FormFile theFile;
064:
065: /**
066: * The file path to write to
067: */
068: protected String filePath;
069:
070: /**
071: * Retrieve the value of the text the user has sent as form data
072: */
073: public String getTheText() {
074: return theText;
075: }
076:
077: /**
078: * Set the value of the form data text
079: */
080: public void setTheText(String theText) {
081: this .theText = theText;
082: }
083:
084: /**
085: * Retrieve the value of the query string parameter
086: */
087: public String getQueryParam() {
088: return queryParam;
089: }
090:
091: /**
092: * Set the value of the query string parameter
093: */
094: public void setQueryParam(String queryParam) {
095: this .queryParam = queryParam;
096: }
097:
098: /**
099: * Retrieve a representation of the file the user has uploaded
100: */
101: public FormFile getTheFile() {
102: return theFile;
103: }
104:
105: /**
106: * Set a representation of the file the user has uploaded
107: */
108: public void setTheFile(FormFile theFile) {
109: this .theFile = theFile;
110: }
111:
112: /**
113: * Set whether or not to write to a file
114: */
115: public void setWriteFile(boolean writeFile) {
116: this .writeFile = writeFile;
117: }
118:
119: /**
120: * Get whether or not to write to a file
121: */
122: public boolean getWriteFile() {
123: return writeFile;
124: }
125:
126: /**
127: * Set the path to write a file to
128: */
129: public void setFilePath(String filePath) {
130: this .filePath = filePath;
131: }
132:
133: /**
134: * Get the path to write a file to
135: */
136: public String getFilePath() {
137: return filePath;
138: }
139:
140: public void reset() {
141: writeFile = false;
142: }
143:
144: /**
145: * Check to make sure the client hasn't exceeded the maximum allowed upload size inside of this
146: * validate method.
147: */
148: public ActionErrors validate(ActionMapping mapping,
149: HttpServletRequest request) {
150:
151: ActionErrors errors = super .validate(mapping, request);
152:
153: //has the maximum length been exceeded?
154: Boolean maxLengthExceeded = (Boolean) request
155: .getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
156:
157: if ((maxLengthExceeded != null)
158: && (maxLengthExceeded.booleanValue())) {
159: if (errors == null) {
160: errors = new ActionErrors();
161: }
162: errors.add(ActionMessages.GLOBAL_MESSAGE,
163: new ActionMessage("maxLengthExceeded"));
164: errors.add(ActionMessages.GLOBAL_MESSAGE,
165: new ActionMessage("maxLengthExplanation"));
166: }
167: return errors;
168:
169: }
170: }
|