001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: /**
007: * Wrapper class to make com.oreilly.servlet.multipart.FilePart object
008: * available as a DataSource.
009: *
010: * We have to buffer the data stream since the MimePart class will try to
011: * read the stream several times (??) and we can't rewind the HttpRequest stream.
012: * <p>
013: * <b>Note</b>: The clients of this class must explictly call <code>dispose()</code>
014: * method to release temp files associated with this object.
015: * </p>
016: *
017: * @author George Lindholm, ITServices, UBC
018: * @version $Revision: 35756 $
019: */package org.jasig.portal;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.ByteArrayInputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.io.FileOutputStream;
027: import java.io.BufferedOutputStream;
028: import java.io.FileInputStream;
029: import java.io.BufferedInputStream;
030: import javax.activation.DataSource;
031: import com.oreilly.servlet.multipart.FilePart;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: public class MultipartDataSource implements DataSource {
037:
038: private static final Log log = LogFactory
039: .getLog(MultipartDataSource.class);
040:
041: java.io.File tempfile;
042: ByteArrayOutputStream buff = null;
043: String contentType = null;
044: String filename = null;
045: String errorMessage = null;
046: boolean isAvailable = false;
047:
048: public MultipartDataSource(FilePart filePart) throws IOException {
049: contentType = filePart.getContentType();
050: filename = filePart.getFileName();
051: try {
052: tempfile = java.io.File.createTempFile("uPdata", null);
053: tempfile.deleteOnExit();
054: OutputStream out = new BufferedOutputStream(
055: new FileOutputStream(tempfile));
056: filePart.writeTo(out);
057: out.close();
058: } catch (IOException ioe) {
059: log.error("MultipartDataSource unable to create temp file",
060: ioe);
061: if (tempfile != null) {
062: try {
063: tempfile.delete();
064: } catch (Exception e) {
065: }
066: tempfile = null;
067: }
068: buff = new ByteArrayOutputStream();
069: filePart.writeTo(buff);
070: }
071: this .isAvailable = true;
072: }
073:
074: public MultipartDataSource(String fileName, String errorMessage) {
075: this .filename = fileName;
076: this .errorMessage = errorMessage;
077: this .isAvailable = false;
078: }
079:
080: public boolean isAvailable() {
081: return this .isAvailable;
082: }
083:
084: /**
085: * Releases tempfile associated with this object any memory they consume
086: * will be returned to the OS.
087: * @since uPortal 2.5. Prior to uPortal 2.5, tempfile deletion was a side effect
088: * of the finalizer.
089: */
090: public void dispose() {
091: buff = null;
092: if (tempfile != null) {
093: boolean success = tempfile.delete();
094: if (!success) {
095: log.error("Unable to delete temp file ["
096: + tempfile.getPath() + "]");
097: }
098:
099: tempfile = null;
100: }
101: }
102:
103: public InputStream getInputStream() throws IOException {
104: if (!isAvailable())
105: throw new IOException(this .getErrorMessage());
106:
107: if (tempfile != null) {
108: return new BufferedInputStream(
109: new FileInputStream(tempfile));
110: } else {
111: return new ByteArrayInputStream(buff.toByteArray());
112: }
113: }
114:
115: public OutputStream getOutputStream() throws IOException {
116: throw new IOException("getOutputStream() not implemented");
117: }
118:
119: public String getContentType() {
120: return contentType;
121: }
122:
123: public String getName() {
124: return filename;
125: }
126:
127: public String getErrorMessage() {
128: return errorMessage;
129: }
130:
131: }
|