1 2 3 4 package net.nutch.protocol.file; 5 6 import javax.activation.MimetypesFileTypeMap ; 7 10 import net.nutch.protocol.Content; 11 12 import java.net.URL ; 13 14 import java.util.TreeMap ; 15 import java.util.Properties ; 16 17 import java.util.logging.Level ; 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 22 48 public class FileResponse { 49 private String orig; 50 private String base; 51 private byte[] content; 52 private int code; 53 private Properties headers = new Properties (); 54 55 private final File file; 56 57 58 public int getCode() { return code; } 59 60 61 public String getHeader(String name) { 62 return (String )headers.get(name); 63 } 64 65 public byte[] getContent() { return content; } 66 67 public Content toContent() { 68 return new Content(orig, base, content, 69 getHeader("Content-Type"), 70 headers); 71 } 72 73 public FileResponse(URL url, File file) 74 throws FileException, IOException { 75 this(url.toString(), url, file); 76 } 77 78 public FileResponse(String orig, URL url, File file) 79 throws FileException, IOException { 80 81 this.orig = orig; 82 this.base = url.toString(); 83 this.file = file; 84 85 if (!"file".equals(url.getProtocol())) 86 throw new FileException("Not a file url:" + url); 87 88 if (File.LOG.isLoggable(Level.FINE)) 89 File.LOG.fine("fetching " + url); 90 91 if (url.getPath() != url.getFile()) 92 File.LOG.warning("url.getPath() != url.getFile(): " + url); 93 94 String path = "".equals(url.getPath()) ? "/" : url.getPath(); 95 96 try { 97 98 this.content = null; 99 100 java.io.File f = new java.io.File (path); 103 104 if (!f.exists()) { 105 this.code = 404; return; 107 } 108 109 if (!f.canRead()) { 110 this.code = 401; return; 112 } 113 114 if (!f.equals(f.getCanonicalFile())) { 118 TreeMap hdrs = new TreeMap (String.CASE_INSENSITIVE_ORDER); 120 hdrs.put("Location", f.getCanonicalFile().toURL().toString()); 122 this.headers.putAll(hdrs); 123 124 this.code = 300; return; 126 } 127 128 if (f.isDirectory()) { 129 getDirAsHttpResponse(f); 130 } else if (f.isFile()) { 131 getFileAsHttpResponse(f); 132 } else { 133 this.code = 500; return; 135 } 136 137 } catch (IOException e) { 138 throw e; 139 } 140 141 } 142 143 private void getFileAsHttpResponse(java.io.File f) 145 throws FileException, IOException { 146 147 long size = f.length(); 150 if (size > Integer.MAX_VALUE) { 151 throw new FileException("file is too large, size: "+size); 152 } 156 157 int len = (int) size; 159 160 if (this.file.maxContentLength > 0 && len > this.file.maxContentLength) 161 len = this.file.maxContentLength; 162 163 this.content = new byte[len]; 164 165 java.io.InputStream is = new java.io.FileInputStream (f); 166 int offset = 0; int n = 0; 167 while (offset < len 168 && (n = is.read(this.content, offset, len-offset)) >= 0) { 169 offset += n; 170 } 171 if (offset < len) File.LOG.warning("not enough bytes read from file: "+f.getPath()); 173 is.close(); 174 175 TreeMap hdrs = new TreeMap (String.CASE_INSENSITIVE_ORDER); 177 178 hdrs.put("Content-Length", new Long (size).toString()); 179 180 hdrs.put("Last-Modified", 181 this.file.httpDateFormat.toString(f.lastModified())); 182 183 String contentType = null; 184 if (contentType == null && this.file.TYPE_MAP != null) 188 contentType = this.file.TYPE_MAP.getContentType(f.getName()); 189 if (contentType != null) 190 hdrs.put("Content-Type", contentType); 191 192 this.headers.putAll(hdrs); 193 194 this.code = 200; } 197 198 private void getDirAsHttpResponse(java.io.File f) 200 throws IOException { 201 202 String path = f.toString(); 203 this.content = list2html(f.listFiles(), path, "/".equals(path) ? false : true); 204 205 TreeMap hdrs = new TreeMap (String.CASE_INSENSITIVE_ORDER); 207 208 hdrs.put("Content-Length", 209 new Integer (this.content.length).toString()); 210 211 hdrs.put("Content-Type", "text/html"); 212 213 hdrs.put("Last-Modified", 214 this.file.httpDateFormat.toString(f.lastModified())); 215 216 this.headers.putAll(hdrs); 217 218 this.code = 200; } 221 222 private byte[] list2html(java.io.File [] list, 224 String path, boolean includeDotDot) { 225 226 StringBuffer x = new StringBuffer ("<html><head>"); 227 x.append("<title>Index of "+path+"</title></head>\n"); 228 x.append("<body><h1>Index of "+path+"</h1><pre>\n"); 229 230 if (includeDotDot) { 231 x.append("<a HREF='../'>../</a>\t-\t-\t-\n"); 232 } 233 234 236 java.io.File f; 237 for (int i=0; i<list.length; i++) { 238 f = list[i]; 239 String name = f.getName(); 240 String time = this.file.httpDateFormat.toString(f.lastModified()); 241 if (f.isDirectory()) { 242 x.append("<a HREF='"+name+"/"+"'>"+name+"/</a>\t"); 247 x.append(time+"\t-\n"); 248 } else if (f.isFile()) { 249 x.append("<a HREF='"+name+ "'>"+name+"</a>\t"); 250 x.append(time+"\t"+f.length()+"\n"); 251 } else { 252 } 254 } 255 256 x.append("</pre></body></html>\n"); 257 258 return new String (x).getBytes(); 259 } 260 261 } 262 | Popular Tags |