001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: UploadFileSpec.java,v 1.1 2002/01/14 18:13:45 russgold Exp $
005: *
006: * Copyright (c) 2001-2002, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.io.File;
024: import java.io.InputStream;
025: import java.io.IOException;
026: import java.io.FileInputStream;
027:
028: /**
029: * A description of a file to be uploaded as part of a form submission.
030: *
031: * @author <a href="mailto:russgold@acm.org">Russell Gold</a>
032: **/
033: public class UploadFileSpec {
034:
035: /**
036: * Creates a specification based on a File object. The content type will be guessed from the file extension.
037: */
038: public UploadFileSpec(File file) {
039: _file = file;
040: guessContentType();
041: }
042:
043: /**
044: * Creates a specification based on a File object and with a specified content type.
045: */
046: public UploadFileSpec(File file, String contentType) {
047: _file = file;
048: _contentType = contentType;
049: }
050:
051: /**
052: * Creates a specification for an upload from an input stream. The file name and content type must be specified.
053: */
054: public UploadFileSpec(String fileName, InputStream inputStream,
055: String contentType) {
056: _fileName = fileName;
057: _inputStream = inputStream;
058: _contentType = contentType;
059: }
060:
061: InputStream getInputStream() throws IOException {
062: if (_inputStream == null) {
063: _inputStream = new FileInputStream(_file);
064: }
065: return _inputStream;
066: }
067:
068: String getFileName() {
069: if (_fileName == null) {
070: _fileName = _file.getAbsolutePath();
071: }
072: return _fileName;
073: }
074:
075: /**
076: * Returns the content type associated with this file upload specification.
077: */
078: public String getContentType() {
079: return _contentType;
080: }
081:
082: private File _file;
083:
084: private InputStream _inputStream;
085:
086: private String _fileName;
087:
088: private String _contentType = "text/plain";
089:
090: private static String[][] CONTENT_EXTENSIONS = {
091: { "text/plain", "txt", "text" },
092: { "text/html", "htm", "html" }, { "image/gif", "gif" },
093: { "image/jpeg", "jpg", "jpeg" }, { "image/png", "png" },
094: { "application/octet-stream", "zip" } };
095:
096: private void guessContentType() {
097: String extension = getExtension(_file.getName());
098: for (int i = 0; i < CONTENT_EXTENSIONS.length; i++) {
099: for (int j = 1; j < CONTENT_EXTENSIONS[i].length; j++) {
100: if (extension
101: .equalsIgnoreCase(CONTENT_EXTENSIONS[i][j])) {
102: _contentType = CONTENT_EXTENSIONS[i][0];
103: return;
104: }
105: }
106: }
107: }
108:
109: private String getExtension(String fileName) {
110: return fileName.substring(fileName.lastIndexOf('.') + 1);
111: }
112: }
|