001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: MockFileUpload.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.test;
009:
010: import com.uwyn.rife.config.RifeConfig;
011: import java.io.File;
012: import java.io.FileInputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015:
016: /**
017: * An instance of this class provides all the data that is needed to simulate
018: * a file upload.
019: *
020: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
021: * @version $Revision: 3634 $
022: * @since 1.1
023: */
024: public class MockFileUpload {
025: private File mFile;
026: private InputStream mInputStream;
027: private String mFileName;
028: private String mContentType = "text/plain";
029:
030: /**
031: * Creates a new file upload simulation based on a <code>File</code>
032: * object.
033: * <p>The content type will be guessed from the file extension. The
034: * extension to mime-type mapping is retrieved from {@link
035: * com.uwyn.rife.config.RifeConfig.Mime}.
036: *
037: * @param file the file that will be uploaded
038: * @since 1.1
039: */
040: public MockFileUpload(File file) {
041: if (null == file)
042: throw new IllegalArgumentException("file can't be null.");
043:
044: mFile = file;
045: guessContentType();
046: }
047:
048: /**
049: * Creates a new file upload simulation based on a <code>File</code>
050: * object.
051: *
052: * @param file the file that will be uploaded
053: * @param contentType the content type of the file
054: * @since 1.1
055: */
056: public MockFileUpload(File file, String contentType) {
057: if (null == file)
058: throw new IllegalArgumentException("file can't be null.");
059:
060: mFile = file;
061: if (null == contentType) {
062: guessContentType();
063: } else {
064: mContentType = contentType;
065: }
066: }
067:
068: /**
069: * Creates a new file upload simulation based on an
070: * <code>InputStream</code>.
071: *
072: * @param fileName the name of file that will be uploaded
073: * @param inputStream the input stream that will be read to provide the
074: * content of the uploaded file
075: * @param contentType the content type of the uploaded file
076: * @since 1.1
077: */
078: public MockFileUpload(String fileName, InputStream inputStream,
079: String contentType) {
080: if (null == fileName)
081: throw new IllegalArgumentException(
082: "fileName can't be null.");
083: if (null == inputStream)
084: throw new IllegalArgumentException(
085: "inputStream can't be null.");
086:
087: mFileName = fileName;
088: mInputStream = inputStream;
089: if (null == contentType) {
090: guessContentType();
091: } else {
092: mContentType = contentType;
093: }
094: }
095:
096: InputStream getInputStream() throws IOException {
097: if (null == mInputStream) {
098: mInputStream = new FileInputStream(mFile);
099: }
100: return mInputStream;
101: }
102:
103: String getFileName() {
104: if (null == mFileName) {
105: mFileName = mFile.getAbsolutePath();
106: }
107: return mFileName;
108: }
109:
110: /**
111: * Returns the content type associated with this file upload simulation.
112: * <p>If no content type has been provided, and it could not be detected
113: * automatically, then it defaults to <code>text/plain</code>.
114: *
115: * @return the content type
116: * @since 1.1
117: */
118: public String getContentType() {
119: return mContentType;
120: }
121:
122: private void guessContentType() {
123: String extension = getExtension(getFileName());
124: if (null == extension) {
125: return;
126: }
127:
128: String content_type = RifeConfig.Mime.getMimeType(extension);
129: if (content_type != null) {
130: mContentType = content_type;
131: }
132: }
133:
134: private String getExtension(String fileName) {
135: int last_dot_index = fileName.lastIndexOf('.');
136: if (-1 == last_dot_index) {
137: return null;
138: }
139: return fileName.substring(last_dot_index + 1);
140: }
141: }
|