001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.net.http.httpclient3; 020 021import java.io.FileNotFoundException; 022import java.io.IOException; 023import java.io.InputStream; 024 025import lucee.commons.io.res.Resource; 026 027import org.apache.commons.httpclient.methods.multipart.PartSource; 028 029public final class ResourcePartSource implements PartSource { 030 031 private final Resource res; 032 private String fileName = null; 033 034 /** 035 * Constructor of the class 036 * 037 * @param res the FilePart source File. 038 * 039 * @throws FileNotFoundException if the file does not exist or 040 * cannot be read 041 */ 042 public ResourcePartSource(Resource res) throws FileNotFoundException { 043 this.res = res; 044 if (res != null) { 045 if (!res.isFile()) { 046 throw new FileNotFoundException("File is not a normal file."); 047 } 048 if (!res.isReadable()) { 049 throw new FileNotFoundException("File is not readable."); 050 } 051 this.fileName = res.getName(); 052 } 053 } 054 055 /** 056 * Constructor for FilePartSource. 057 * 058 * @param fileName the file name of the FilePart 059 * @param file the source File for the FilePart 060 * 061 * @throws FileNotFoundException if the file does not exist or 062 * cannot be read 063 */ 064 public ResourcePartSource(String fileName, Resource file) 065 throws FileNotFoundException { 066 this(file); 067 if (fileName != null) { 068 this.fileName = fileName; 069 } 070 } 071 072 @Override 073 public long getLength() { 074 if (this.res != null) { 075 return this.res.length(); 076 } 077 return 0; 078 } 079 080 @Override 081 public String getFileName() { 082 return (fileName == null) ? "noname" : fileName; 083 } 084 085 @Override 086 public InputStream createInputStream() throws IOException { 087 return res.getInputStream(); 088 } 089 090 /** 091 * @return the res 092 */ 093 public Resource getResource() { 094 return res; 095 } 096}