1 package net.matuschek.http; 2 3 6 7 8 import java.io.BufferedOutputStream ; 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.net.URL ; 14 import java.util.StringTokenizer ; 15 16 import org.apache.log4j.Category; 17 18 24 public class HttpDocToFile extends AbstractHttpDocManager 25 { 26 29 private String baseDir; 30 31 32 35 private int minFileSize; 36 37 38 43 private boolean replaceAllSpecials = false; 44 45 46 51 private boolean storeCGI = true; 52 53 54 private Category log; 55 56 57 58 62 public HttpDocToFile(String baseDir) { 63 this.baseDir = baseDir; 64 log = Category.getInstance(getClass().getName()); 65 } 66 67 68 74 public void storeDocument(HttpDoc doc) 75 throws DocManagerException 76 { 77 if ((doc == null) || (doc.getContent() == null)) { 78 return; 79 } 80 81 85 if (doc.isCached()) { 86 return; 87 } 88 89 90 if ((! storeCGI) 91 && (doc.getURL().toString().indexOf('?') >= 0)) { 92 return; 95 } 96 97 98 String filename = url2Filename(doc.getURL()); 99 if (doc.getContent().length >= minFileSize) { 100 try { 101 createDirs(filename); 102 BufferedOutputStream os = 103 new BufferedOutputStream (new FileOutputStream (filename)); 104 os.write(doc.getContent()); 105 os.flush(); 106 os.close(); 107 } catch (IOException e) { 108 throw new DocManagerException(e.getMessage()); 109 } 110 } 111 } 112 113 114 119 protected File getCacheFile(URL url) { 120 File cacheFile = new File (url2Filename(url)); 122 if (! (cacheFile.exists() && (cacheFile.isFile()))) { 123 return null; 124 } 125 return cacheFile; 126 } 127 128 133 protected String getExtension(URL url) { 134 if ((url.toString().indexOf('?') >= 0) 136 || (url.toString().indexOf("cgi") >= 0)) { 137 return null; 138 } 139 140 String path = url.getPath(); 143 String ext = null; 144 145 if (path.indexOf(".") < 0) { 146 return null; 147 } 148 149 StringTokenizer st = new StringTokenizer (path,"."); 150 while (st.hasMoreTokens()) { 151 ext = st.nextToken(); 152 } 153 if (ext.indexOf("/") >= 0) { 155 return null; 156 } 157 158 return ext; 159 } 160 161 167 public void removeDocument(URL u) { 168 String ext = getExtension(u); 169 if (ext == null) return; 170 File cacheFile = getCacheFile(u); 171 if (cacheFile == null) return ; 172 173 cacheFile.delete(); 174 } 175 176 186 public HttpDoc retrieveFromCache(URL u) { 187 String ext = getExtension(u); 188 if (ext == null) return null; 189 File cacheFile = getCacheFile(u); 190 if (cacheFile == null) return null; 191 192 long size = cacheFile.length(); 194 if (size > Integer.MAX_VALUE) { 195 log.info("File too large"); 196 return null; 197 } 198 199 byte[] buff = new byte[(int) size]; 200 201 try { 203 FileInputStream fi = new FileInputStream (cacheFile); 204 fi.read(buff); 205 } catch (IOException e) { 206 log.info("Could not read cached document "+e.getMessage()); 207 return null; 208 } 209 210 HttpDoc doc = new HttpDoc(); 212 213 doc.setHttpCode("HTTP/1.0 200 OK"); 215 doc.setContent(buff); 216 217 218 String mimetype = null; 220 221 if (ext.equals("html") 222 || ext.equals("htm") 223 || ext.equals("shtml") 224 || ext.equals("asp") 225 || ext.equals("php") 226 || ext.equals("jsp")) { 227 mimetype="text/html"; 228 } else { 229 mimetype="application/unknown"; 230 } 231 232 doc.addHeader(new HttpHeader("Content-Type",mimetype)); 233 doc.setURL(u); 234 doc.setCached(true); 235 236 return doc; 237 } 238 239 240 244 public String getBaseDir() { 245 return baseDir; 246 } 247 248 249 253 public void setBaseDir(String baseDir) { 254 this.baseDir = baseDir; 255 } 256 257 258 264 protected String url2Filename(URL u) { 265 StringBuffer sb = new StringBuffer (); 266 267 sb.append(baseDir); 268 sb.append(File.separatorChar); 269 sb.append(u.getHost()); 270 sb.append(u.getFile()); 271 272 String query = u.getQuery(); 275 if ((query != null) && 276 (!query.equals(""))) { 277 sb.append(File.separatorChar); 278 sb.append(query); 279 } 280 281 if (sb.charAt(sb.length()-1) == '/') { 284 sb.append("index.html"); 285 } 286 287 for (int i=0; i<sb.length(); i++) { 289 char c=sb.charAt(i); 290 char newc=(char)0; 291 292 if (c == '/') { 294 newc = File.separatorChar; 295 } 296 297 if (replaceAllSpecials) { 299 if ((c == '?') 300 || (c == '=') 301 || (c == '&')) { 302 newc = '-'; 303 } 304 } 305 306 if ((newc != (char)0) 307 && (newc != c)) { 308 sb.setCharAt(i,newc); 309 } 310 } 311 312 return sb.toString(); 313 } 314 315 316 321 protected void createDirs(String filename) throws IOException { 322 int pos = -1; 323 for (int i = filename.length() - 1; i >= 0; i--) { 325 if (filename.charAt(i) == File.separatorChar) { 326 pos = i; 327 i = -1; 328 } 329 } 330 File dir = new File (filename.substring(0, pos)); 331 dir.mkdirs(); 332 } 333 334 335 340 public int getMinFileSize() { 341 return minFileSize; 342 } 343 344 345 350 public void setMinFileSize(int minFileSize) { 351 this.minFileSize = minFileSize; 352 } 353 354 355 365 public boolean isReplaceAllSpecials() { 366 return replaceAllSpecials; 367 } 368 369 370 380 public void setReplaceAllSpecials(boolean v) { 381 this.replaceAllSpecials = v; 382 } 383 384 385 392 public boolean getStoreCGI() { 393 return storeCGI; 394 } 395 396 397 406 public void setStoreCGI(boolean v) { 407 this.storeCGI = v; 408 } 409 410 } 411 412 413 | Popular Tags |