Added a responder to serve static content from the classloader.
[jsvc.git] / src / dolda / jsvc / util / Multiplexer.java
CommitLineData
79d2dd64
FT
1package dolda.jsvc.util;
2
3import dolda.jsvc.*;
4import java.util.*;
5
6public class Multiplexer implements Responder {
7 private Responder def;
8 private Collection<Sub> subs = new LinkedList<Sub>();
9
10 private static interface Sub {
11 boolean match(Request req);
12 }
13
14 public Multiplexer(Responder def) {
15 this.def = def;
16 }
17
18 public Multiplexer() {
7779099a
FT
19 this(new Responder() {
20 public void respond(Request req) {
21 throw(Restarts.stdresponse(404, "Resource not found", "The resource you requested could not be found on this server."));
79d2dd64
FT
22 }
23 });
24 }
25
26 public void file(final String path, final Responder responder) {
27 subs.add(new Sub() {
28 public boolean match(Request req) {
29 if(req.path().equals(path)) {
30 responder.respond(req);
31 return(true);
32 }
33 return(false);
34 }
35 });
36 }
37
38 public void dir(String path, final Responder responder) {
39 final String fp = Misc.stripslashes(path, true, true);
40 subs.add(new Sub() {
41 public boolean match(Request req) {
42 if(req.path().equals(fp)) {
43 throw(Restarts.redirect(fp + "/"));
44 } else if(req.path().startsWith(fp + "/")) {
45 responder.respond(RequestWrap.chpath(req, req.path().substring(fp.length() + 1)));
46 return(true);
47 }
48 return(false);
49 }
50 });
51 }
52
53 public void respond(Request req) {
54 for(Sub s : subs) {
55 if(s.match(req))
56 return;
57 }
58 def.respond(req);
59 }
60}