1 package dolda.jsvc.util;
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;
13 public StaticContent(Class<?> base, String resname, boolean dir, String mimetype) {
15 this.resname = resname;
17 this.mimetype = mimetype;
20 public StaticContent(String resname, boolean dir, String mimetype) {
21 this(null, resname, dir, mimetype);
24 public void respond(Request req) {
27 nm = resname + "/" + req.path();
33 in = StaticContent.class.getClassLoader().getResourceAsStream(nm);
35 in = base.getResourceAsStream(nm);
38 throw(Restarts.stdresponse(404));
39 String ims = req.inheaders().get("If-Modified-Since");
40 Date mtime = new Date((req.ctx().starttime() / 1000) * 1000);
44 d = Http.parsedate(ims);
45 } catch(java.text.ParseException e) {
46 throw(Restarts.stdresponse(400));
48 if(mtime.compareTo(d) <= 0) {
50 req.outheaders().put("Content-Length", "0");
56 req.outheaders().put("Content-Type", mimetype);
57 req.outheaders().put("Last-Modified", Http.fmtdate(mtime));
58 Misc.cpstream(in, req.output());
62 } catch(IOException e) {