Replaced the "Restarts" class with individual restart classes.
[jsvc.git] / src / dolda / jsvc / util / StaticContent.java
1 package dolda.jsvc.util;
2
3 import dolda.jsvc.*;
4 import java.io.*;
5 import java.util.*;
6
7 public class StaticContent implements Responder {
8     private final Class<?> base;
9     private final String resname;
10     private final boolean dir;
11     private final String mimetype;
12     
13     public StaticContent(Class<?> base, String resname, String mimetype) {
14         this.base = base;
15         this.dir = ((this.resname = resname).charAt(resname.length() - 1) == '/');
16         this.mimetype = mimetype;
17     }
18     
19     public StaticContent(String resname, String mimetype) {
20         this(null, resname, mimetype);
21     }
22     
23     public void respond(Request req) {
24         String nm;
25         if(dir)
26             nm = resname + req.path();
27         else
28             nm = resname;
29         InputStream in;
30         if(base == null) {
31             in = StaticContent.class.getClassLoader().getResourceAsStream(nm);
32         } else {
33             in = base.getResourceAsStream(nm);
34         }
35         if(in == null)
36             throw(new StdResponse(404));
37         Cache.checkmtime(req, req.ctx().starttime());
38         try {
39             try {
40                 req.outheaders().put("Content-Type", mimetype);
41                 Misc.cpstream(in, req.output());
42             } finally {
43                 in.close();
44             }
45         } catch(IOException e) {
46             throw(new Error(e));
47         }
48     }
49 }