1 28 29 package com.caucho.jsp; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.vfs.Depend; 33 import com.caucho.vfs.Path; 34 import com.caucho.vfs.PersistentDependency; 35 import com.caucho.vfs.ReadStream; 36 import com.caucho.vfs.TempCharBuffer; 37 import com.caucho.vfs.WriteStream; 38 39 import javax.servlet.ServletException ; 40 import javax.servlet.ServletRequest ; 41 import javax.servlet.ServletResponse ; 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpServletResponse ; 44 import java.io.IOException ; 45 import java.io.PrintWriter ; 46 import java.util.ArrayList ; 47 48 51 public class StaticPage extends Page { 52 private Path _cacheEntry; 53 private long _lastModified; 54 private int _contentLength; 55 private boolean _hasSession; 56 57 63 StaticPage(Path path, boolean hasSession) 64 throws IOException 65 { 66 _cacheEntry = path; 67 _contentLength = (int) _cacheEntry.getLength(); 68 _hasSession = hasSession; 69 70 _caucho_setCacheable(); 71 } 72 73 public void init(Path path) 74 throws ServletException 75 { 76 } 77 78 81 public boolean _caucho_isModified() 82 { 83 return ! _cacheEntry.exists() || super._caucho_isModified(); 84 } 85 86 void _caucho_setUncacheable() 87 { 88 _lastModified = 0; 89 } 90 91 public long getLastModified(HttpServletRequest request) 92 { 93 return _caucho_lastModified(); 94 } 95 96 99 public void service(ServletRequest request, ServletResponse response) 100 throws IOException , ServletException 101 { 102 HttpServletRequest req = (HttpServletRequest ) request; 103 HttpServletResponse res = (HttpServletResponse ) response; 104 105 _caucho_init(req, res); 106 107 if (_hasSession) { 108 req.getSession(); 109 res.setHeader("Cache-Control", "private"); 110 } 111 112 114 TempCharBuffer buf = TempCharBuffer.allocate(); 115 char []cBuf = buf.getBuffer(); 116 int len; 117 118 PrintWriter out = response.getWriter(); 119 120 ReadStream rs = _cacheEntry.openRead(); 121 rs.setEncoding("UTF-8"); 122 try { 123 while ((len = rs.read(cBuf, 0, cBuf.length)) > 0) { 124 out.write(cBuf, 0, len); 125 } 126 } finally { 127 rs.close(); 128 } 129 130 TempCharBuffer.free(buf); 131 } 132 133 public boolean disableLog() 134 { 135 return true; 136 } 137 138 public static void writeDepend(Path dependPath, 139 ArrayList <PersistentDependency> dependList) 140 throws IOException 141 { 142 WriteStream os = dependPath.openWrite(); 143 try { 144 for (int i = 0; i < dependList.size(); i++) { 145 PersistentDependency dependency = dependList.get(i); 146 147 if (dependency instanceof Depend) { 148 Depend depend = (Depend) dependency; 149 150 os.print('"'); 151 os.print(depend.getPath().getNativePath()); 152 os.print("\" \""); 153 os.print(depend.getDigest()); 154 os.println("\""); 155 } 156 } 157 } finally { 158 os.close(); 159 } 160 } 161 162 static ArrayList <Depend> parseDepend(Path dependPath) 163 throws IOException 164 { 165 ReadStream is = dependPath.openRead(); 166 try { 167 ArrayList <Depend> dependList = new ArrayList <Depend>(); 168 169 String name; 170 171 while ((name = parseName(is)) != null) { 172 long digest = Long.parseLong(parseName(is)); 173 174 Depend depend = new Depend(dependPath.lookup(name), digest); 175 176 dependList.add(depend); 177 } 178 179 return dependList; 180 } finally { 181 is.close(); 182 } 183 } 184 185 private static String parseName(ReadStream is) 186 throws IOException 187 { 188 int ch; 189 190 for (ch = is.read(); ch > 0 && ch != '"'; ch = is.read()) { 191 } 192 193 if (ch < 0) 194 return null; 195 196 CharBuffer cb = new CharBuffer(); 197 198 for (ch = is.read(); ch > 0 && ch != '"'; ch = is.read()) { 199 cb.append((char) ch); 200 } 201 202 if (ch < 0) 203 return null; 204 205 return cb.toString(); 206 } 207 208 public void destroy() 209 { 210 216 } 217 218 221 public String toString() 222 { 223 return "StaticPage[" + _cacheEntry + "]"; 224 } 225 } 226 | Popular Tags |