1 4 5 9 10 package org.openlaszlo.data; 11 12 import java.io.*; 13 import java.net.URL ; 14 import java.net.MalformedURLException ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 import org.apache.log4j.Logger; 18 import org.openlaszlo.utils.ChainedException; 19 import org.openlaszlo.utils.LZHttpUtils; 20 import org.openlaszlo.utils.FileUtils; 21 import org.openlaszlo.media.MimeType; 22 23 26 public class FileDataSource extends DataSource 27 { 28 private static Logger mLogger = Logger.getLogger(FileDataSource.class); 29 30 33 public String name() { 34 return "file"; 35 } 36 37 46 public Data getData(String app, HttpServletRequest req, 47 HttpServletResponse res, long lastModifiedTime) 48 throws IOException, DataSourceException { 49 return getFileData(app, req, res, null, lastModifiedTime); 50 } 51 52 static public Data getFileData(String app, HttpServletRequest req, 53 HttpServletResponse res, String urlStr, 54 long lastModifiedTime) 55 throws IOException, DataSourceException { 56 57 if (urlStr == null) { 58 urlStr = DataSource.getURL(req); 59 } 60 61 URL url = new URL (urlStr); 62 63 String protocol = url.getProtocol(); 64 65 if ( protocol == null || ! protocol.equals("file")){ 66 mLogger.error( " bad protocol for " + url ); 67 throw new DataSourceException("protocol " + protocol + "is not 'file:' "); 68 } 69 70 if (true) { 72 throw new IOException("'file' data request type is not supported."); 73 } 74 75 String filename = url.getFile(); 76 mLogger.debug("filename " + filename); 77 78 if ( filename == null || filename.equals("") ) { 79 throw new DataSourceException("empty filename"); 80 } 81 82 if (filename.charAt(0) != '/') { 84 mLogger.debug("app " + app); 85 String appdir = app.substring(0, 86 app.lastIndexOf(File.separatorChar) + 1); 87 mLogger.debug("appdir " + appdir); 88 filename = appdir + filename; 89 mLogger.debug("filename " + filename); 90 } 91 92 if (File.separatorChar == '\\') { 94 while (filename.startsWith("/")) { 95 filename = filename.substring(1); 96 } 97 filename = filename.replace('/', '\\'); 98 } 99 100 FileData data = new FileData(filename, lastModifiedTime); 101 102 res.setDateHeader(LZHttpUtils.LAST_MODIFIED, data.lastModified()); 104 105 return data; 106 } 107 108 113 114 public static class FileData extends Data { 115 116 117 public FileInputStream str = null; 118 119 120 public final File file; 121 122 123 public final long lastModifiedTime; 124 125 128 public FileData(String filename, long lm) throws IOException { 129 mLogger.debug("filename " + filename); 130 File f = new File(filename); 131 if (f == null) { 132 throw new IOException("can't construct file"); 133 } else if (!f.exists()) { 134 throw new IOException(filename + " doesn't exist."); 135 } else if (!f.canRead()) { 136 throw new IOException("can't read " + filename); 137 } 138 lastModifiedTime = lm; 139 file = f; 140 } 141 142 145 public long size() { 146 try { 147 return FileUtils.fileSize(file); 148 } catch (Exception e) { 149 throw new ChainedException(e); 150 } 151 } 152 153 157 public boolean notModified() { 158 159 long l = lastModified(); 160 if (l == -1) { 161 return false; 162 } 163 164 return (l <= lastModifiedTime); 165 } 166 167 170 public long lastModified() { 171 172 long l = file.lastModified(); 173 if (l == 0) { 174 l = -1; 175 } 176 l = ((l)/1000L) * 1000L; 178 mLogger.debug("lm is " + l); 179 return l; 180 } 181 182 185 public void appendResponseHeadersAsXML(StringBuffer xmlResponse) { 186 xmlResponse.append("<header name=\"Last-Modified\" " 188 + "value=\"" + lastModified() + "\" />"); 189 } 190 191 194 public synchronized void release() { 195 try { 196 if (str != null) { 197 str.close(); 198 str = null; 199 } 200 } catch (Exception e) { 201 mLogger.warn("ignoring exception while closing stream: ", e); 202 } 203 } 204 205 208 public String getMimeType() { 209 return MimeType.fromExtension(file.getPath()); 210 } 211 212 215 public synchronized InputStream getInputStream() throws IOException { 216 if (str == null) { 217 str = new FileInputStream(file); 218 } 219 return str; 220 } 221 222 225 public String getAsString() throws IOException { 226 return FileUtils.readFileString(file); 227 } 228 } 229 } 230 | Popular Tags |