1 package com.quadcap.http.servlets.file; 2 3 40 41 import java.io.File ; 42 import java.io.FileInputStream ; 43 import java.io.FileNotFoundException ; 44 import java.io.InputStream ; 45 import java.io.IOException ; 46 import java.io.OutputStream ; 47 48 import java.net.URL ; 49 import java.net.URLConnection ; 50 51 import javax.servlet.ServletContext ; 52 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletResponse ; 55 56 import com.quadcap.util.collections.Cacheable; 57 58 import com.quadcap.io.IO; 59 60 import com.quadcap.io.dir.Directory; 61 import com.quadcap.io.dir.Entry; 62 63 import com.quadcap.util.Debug; 64 import com.quadcap.util.Util; 65 66 71 public class HttpFile extends Cacheable { 72 ServletContext context; 73 String contentType = "text/plain"; 74 File file; 75 URL url; 76 long lastMod; 77 long lastCheck = -1; 78 byte[] buf = null; 79 80 static int MAX_CACHED = 16 * 1024; 81 82 85 public HttpFile() {} 86 87 public void init(Object store, Object key) throws IOException { 88 super.init(store, key); 89 FileServlet fs = (FileServlet)store; 90 this.context = fs.getServletContext(); 91 String path = key.toString(); 92 this.contentType = context.getMimeType(path); 93 this.file = new File (path); 94 if (!file.exists()) { 95 this.file = null; 96 this.url = fs.getURL(path); 97 if (url == null) { 98 throw new FileNotFoundException ("Not found: " + path); 99 } 100 } 101 compile(); 102 } 103 104 public synchronized void compile() throws IOException { 105 if (file != null) { 106 if (!file.exists()) { 107 throw new FileNotFoundException ("Not found: " + file); 108 } 109 if (file.isDirectory()) { 110 File g = new File (file, "index.html"); 111 if (g.canRead()) { 112 file = g; 113 contentType = "text/html"; 114 } else { 115 throw new FileNotFoundException ("Not found: " + file); 116 } 117 } 118 if (file.canRead() && file.length() < MAX_CACHED) { 119 buf = new byte[(int)file.length()]; 120 FileInputStream is = new FileInputStream (file); 121 try { 122 IO.readFully(is, buf); 123 } finally { 124 is.close(); 125 } 126 this.lastMod = file.lastModified(); 127 } else { 128 buf = null; 129 } 130 } else { 131 URLConnection conn = url.openConnection(); 132 long len = conn.getContentLength(); 133 if (len < MAX_CACHED) { 134 buf = new byte[(int)len]; 135 try { 136 this.lastMod = conn.getLastModified(); 137 InputStream is = conn.getInputStream(); 138 try { 139 IO.readFully(is, buf); 140 } finally { 141 is.close(); 142 } 143 } catch (IOException e) { 144 buf = null; 145 throw e; 146 } 147 } else { 148 buf = null; 149 } 150 } 151 } 152 153 public synchronized void service(HttpServletRequest req, 154 HttpServletResponse res) 155 throws IOException 156 { 157 res.setContentType(contentType); 159 OutputStream os = res.getOutputStream(); 161 if (buf != null) { 163 res.setContentLength(buf.length); 164 os.write(buf); 166 } else if (file != null) { 168 res.setContentLength((int)file.length()); 169 try { 170 FileInputStream is = new FileInputStream (file); 171 try { 172 IO.copyStream(is, os); 173 } finally { 174 is.close(); 175 } 176 } catch (FileNotFoundException fe) { 177 res.sendError(res.SC_NOT_FOUND, "Not found: " + 178 file.getName()); 179 } 180 } else { 181 URLConnection conn = url.openConnection(); 182 res.setContentLength(conn.getContentLength()); 183 InputStream is = conn.getInputStream(); 184 try { 185 IO.copyStream(is, os); 186 } finally { 187 is.close(); 188 } 189 } 190 } 192 193 public synchronized void checkModified() throws Exception { 194 long now = System.currentTimeMillis(); 195 if (now - lastCheck > 2000) { 196 lastCheck = now; 197 if (file != null) { 198 if (file.lastModified() > lastMod) { 199 compile(); 200 } 201 } else { 202 URLConnection conn = url.openConnection(); 203 if (conn.getLastModified() > lastMod) { 204 compile(); 205 } 206 } 207 } 208 } 209 210 public Object getData() { 211 return this; 212 } 213 214 public void setData(Object obj) { 215 throw new RuntimeException ("not implemented"); 216 } 217 } 218 | Popular Tags |