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