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