Replaced the "Restarts" class with individual restart classes.
[jsvc.git] / src / dolda / jsvc / util / StaticContent.java
CommitLineData
7779099a
FT
1package dolda.jsvc.util;
2
3import dolda.jsvc.*;
4import java.io.*;
5import java.util.*;
6
7public 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
e899706d 13 public StaticContent(Class<?> base, String resname, String mimetype) {
7779099a 14 this.base = base;
e899706d 15 this.dir = ((this.resname = resname).charAt(resname.length() - 1) == '/');
7779099a
FT
16 this.mimetype = mimetype;
17 }
18
e899706d
FT
19 public StaticContent(String resname, String mimetype) {
20 this(null, resname, mimetype);
7779099a
FT
21 }
22
23 public void respond(Request req) {
24 String nm;
e899706d
FT
25 if(dir)
26 nm = resname + req.path();
27 else
7779099a 28 nm = resname;
7779099a
FT
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)
4126b9f4 36 throw(new StdResponse(404));
60ef2885 37 Cache.checkmtime(req, req.ctx().starttime());
7779099a
FT
38 try {
39 try {
40 req.outheaders().put("Content-Type", mimetype);
7779099a
FT
41 Misc.cpstream(in, req.output());
42 } finally {
43 in.close();
44 }
45 } catch(IOException e) {
46 throw(new Error(e));
47 }
48 }
49}