001 package railo.commons.net.http.httpclient3; 002 003 import java.io.FileNotFoundException; 004 import java.io.IOException; 005 import java.io.InputStream; 006 007 import org.apache.commons.httpclient.methods.multipart.PartSource; 008 009 import railo.commons.io.res.Resource; 010 011 public final class ResourcePartSource implements PartSource { 012 013 private final Resource res; 014 private String fileName = null; 015 016 /** 017 * Constructor of the class 018 * 019 * @param res the FilePart source File. 020 * 021 * @throws FileNotFoundException if the file does not exist or 022 * cannot be read 023 */ 024 public ResourcePartSource(Resource res) throws FileNotFoundException { 025 this.res = res; 026 if (res != null) { 027 if (!res.isFile()) { 028 throw new FileNotFoundException("File is not a normal file."); 029 } 030 if (!res.isReadable()) { 031 throw new FileNotFoundException("File is not readable."); 032 } 033 this.fileName = res.getName(); 034 } 035 } 036 037 /** 038 * Constructor for FilePartSource. 039 * 040 * @param fileName the file name of the FilePart 041 * @param file the source File for the FilePart 042 * 043 * @throws FileNotFoundException if the file does not exist or 044 * cannot be read 045 */ 046 public ResourcePartSource(String fileName, Resource file) 047 throws FileNotFoundException { 048 this(file); 049 if (fileName != null) { 050 this.fileName = fileName; 051 } 052 } 053 054 @Override 055 public long getLength() { 056 if (this.res != null) { 057 return this.res.length(); 058 } 059 return 0; 060 } 061 062 @Override 063 public String getFileName() { 064 return (fileName == null) ? "noname" : fileName; 065 } 066 067 @Override 068 public InputStream createInputStream() throws IOException { 069 return res.getInputStream(); 070 } 071 072 /** 073 * @return the res 074 */ 075 public Resource getResource() { 076 return res; 077 } 078 }