001: /*
002: * $Id: FileUploadField.java 462295 2006-09-17 20:45:56Z ehillenius $
003: * $Revision: 462295 $
004: * $Date: 2006-09-17 22:45:56 +0200 (Sun, 17 Sep 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.markup.html.form.upload;
020:
021: import wicket.Component;
022: import wicket.Request;
023: import wicket.markup.ComponentTag;
024: import wicket.markup.html.form.FormComponent;
025: import wicket.model.IModel;
026: import wicket.protocol.http.IMultipartWebRequest;
027: import wicket.util.upload.FileItem;
028:
029: /**
030: * Form component that corresponds to a <input type="file">.
031: * When a FileInput component is nested in a
032: * {@link wicket.markup.html.form.Form}, that has multipart == true, its model
033: * is updated with the {@link wicket.util.upload.FileItem}for this
034: * component.
035: *
036: * @author Eelco Hillenius
037: */
038: public class FileUploadField extends FormComponent {
039: private static final long serialVersionUID = 1L;
040:
041: /** True if a model has been set explicitly */
042: private boolean hasExplicitModel;
043:
044: private transient FileUpload fileUpload;
045:
046: /**
047: * @see wicket.Component#Component(String)
048: */
049: public FileUploadField(final String id) {
050: super (id);
051: }
052:
053: /**
054: * @param id
055: * See Component
056: * @param model
057: * See Component
058: */
059: public FileUploadField(final String id, IModel model) {
060: super (id, model);
061: hasExplicitModel = true;
062: }
063:
064: /**
065: * @return The uploaded file
066: */
067: public FileUpload getFileUpload() {
068: // Get request
069: final Request request = getRequest();
070:
071: // If we successfully installed a multipart request
072: if (request instanceof IMultipartWebRequest) {
073: // Get the item for the path
074: final FileItem item = ((IMultipartWebRequest) request)
075: .getFile(getInputName());
076:
077: // Only update the model when there is a file (larger than zero
078: // bytes)
079: if (item != null && item.getSize() > 0) {
080: if (fileUpload == null) {
081: fileUpload = new FileUpload(item);
082: }
083:
084: return fileUpload;
085: }
086: }
087: return null;
088: }
089:
090: /**
091: * @see wicket.Component#setModel(wicket.model.IModel)
092: */
093: public Component setModel(IModel model) {
094: hasExplicitModel = true;
095: return super .setModel(model);
096: }
097:
098: /**
099: * @see wicket.markup.html.form.FormComponent#updateModel()
100: */
101: public void updateModel() {
102: // Only update the model if one was passed in
103: if (hasExplicitModel) {
104: setModelObject(getFileUpload());
105: }
106: }
107:
108: /**
109: * @see wicket.markup.html.form.FormComponent#getInputAsArray()
110: */
111: public String[] getInputAsArray() {
112: FileUpload fu = getFileUpload();
113: if (fu != null)
114: return new String[] { fu.getClientFileName() };
115: return null;
116: }
117:
118: /**
119: * @see wicket.markup.html.form.FormComponent#isMultiPart()
120: */
121: public boolean isMultiPart() {
122: return true;
123: }
124:
125: /**
126: * @see wicket.Component#onComponentTag(wicket.markup.ComponentTag)
127: */
128: protected void onComponentTag(ComponentTag tag) {
129: // Must be attached to an input tag
130: checkComponentTag(tag, "input");
131:
132: // Check for file type
133: checkComponentTagAttribute(tag, "type", "file");
134:
135: // Default handling for component tag
136: super .onComponentTag(tag);
137: }
138:
139: /**
140: * FileInputs cannot be persisted; returns false.
141: *
142: * @see wicket.markup.html.form.FormComponent#supportsPersistence()
143: */
144: protected boolean supportsPersistence() {
145: return false;
146: }
147:
148: /**
149: * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL OR
150: * OVERRIDE.
151: *
152: * Clean up at the end of the request. This means closing all inputstreams
153: * which might have been opened from the fileUpload.
154: *
155: * @see wicket.Component#internalOnDetach()
156: */
157: protected void internalOnDetach() {
158: super.internalOnDetach();
159:
160: if (fileUpload != null) {
161: fileUpload.closeStreams();
162: fileUpload = null;
163: }
164: }
165: }
|