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 java.io.File;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.wicket.IClusterable;
027: import org.apache.wicket.Session;
028: import org.apache.wicket.util.file.Files;
029: import org.apache.wicket.util.upload.FileItem;
030:
031: /**
032: * Model for file uploads.
033: *
034: * @author Jonathan Locke
035: */
036: public class FileUpload implements IClusterable {
037: private static final long serialVersionUID = 1L;
038:
039: private final FileItem item;
040:
041: private transient List/* <InputStream> */inputStreamsToClose;
042:
043: /**
044: * Constructor
045: *
046: * @param item
047: * The uploaded file item
048: */
049: public FileUpload(final FileItem item) {
050: this .item = item;
051: }
052:
053: /**
054: * Close the streams which has been opened when getting the InputStream
055: * using {@link #getInputStream()}. All the input streams are closed at the
056: * end of the request. This is done when the FileUploadField, which is
057: * associated with this FileUpload is detached.
058: * <p>
059: * If an exception is thrown when closing the input streams, we ignore it,
060: * because the stream might have been closed already.
061: */
062: public final void closeStreams() {
063: if (inputStreamsToClose != null) {
064: for (Iterator inputStreamsIterator = inputStreamsToClose
065: .iterator(); inputStreamsIterator.hasNext();) {
066: InputStream inputStream = (InputStream) inputStreamsIterator
067: .next();
068:
069: try {
070: inputStream.close();
071: } catch (IOException e) {
072: // We don't care aobut the exceptions thrown here.
073: }
074: }
075:
076: // Reset the list
077: inputStreamsToClose = null;
078: }
079: }
080:
081: /**
082: * Deletes temp file from disk
083: */
084: public void delete() {
085: item.delete();
086: }
087:
088: /**
089: * @return Uploaded file as an array of bytes
090: */
091: public byte[] getBytes() {
092: return item.get();
093: }
094:
095: /**
096: * @since 1.2
097: * @return name of uploaded client side file
098: */
099: public String getClientFileName() {
100: return item.getName();
101: }
102:
103: /**
104: * @return Content type for upload
105: */
106: public String getContentType() {
107: return item.getContentType();
108: }
109:
110: /**
111: * Get an input stream for the file uploaded. Use this input stream if you
112: * can't use {@link #writeTo(File)} for persisting the uploaded file. This
113: * can be if you need to react upon the content of the file or need to
114: * persist it elsewhere, i.e. a database or external filesystem.
115: * <p>
116: * <b>PLEASE NOTE!</b><br>
117: * The InputStream return will be closed be Wicket at the end of the
118: * request. If you need it across a request you need to hold on to this
119: * FileUpload instead.
120: *
121: * @return Input stream with file contents.
122: * @throws IOException
123: */
124: public InputStream getInputStream() throws IOException {
125: if (inputStreamsToClose == null) {
126: inputStreamsToClose = new ArrayList/* <InputStream> */();
127: }
128:
129: InputStream is = item.getInputStream();
130: inputStreamsToClose.add(is);
131:
132: return is;
133: }
134:
135: /**
136: * @return The upload's size
137: */
138: public long getSize() {
139: return item.getSize();
140: }
141:
142: /**
143: * Saves this file upload to a given file on the server side.
144: *
145: * @param file
146: * The file
147: * @throws IOException
148: */
149: public void writeTo(final File file) throws IOException {
150: InputStream is = getInputStream();
151: try {
152: Files.writeTo(file, is);
153: } finally {
154: is.close();
155: }
156: }
157:
158: /**
159: * Convinience method that copies the input stream returned by
160: * {@link #getInputStream()} into a temporary file.
161: * <p>
162: * Only use this if you actually need a {@link File} to work with, in all
163: * other cases use {@link #getInputStream()} or {@link #getBytes()}
164: *
165: * @since 1.2
166: *
167: * @return temporary file containing the contents of the uploaded file
168: * @throws IOException
169: */
170: public final File writeToTempFile() throws IOException {
171: File temp = File.createTempFile(Session.get().getId(), item
172: .getFieldName());
173: writeTo(temp);
174: return temp;
175: }
176: }
|