Added a responder to serve static content from the classloader.
[jsvc.git] / src / dolda / jsvc / util / Misc.java
1 package dolda.jsvc.util;
2
3 import dolda.jsvc.*;
4 import java.util.*;
5 import java.io.*;
6
7 public class Misc {
8     private static Map<Integer, String> stext = new HashMap<Integer, String>();
9     
10     static {
11         stext.put(200, "OK");
12         stext.put(300, "Multiple Choices");
13         stext.put(301, "Permanently Moved");
14         stext.put(302, "Temporarily Moved");
15         stext.put(303, "See Other");
16         stext.put(304, "Not Modified");
17         stext.put(400, "Bad Request");
18         stext.put(401, "Authentication Required");
19         stext.put(403, "Access Forbidden");
20         stext.put(404, "Resource Not Found");
21         stext.put(500, "Server Error");
22     }
23     
24     public static String statustext(int status) {
25         String text;
26         if((text = stext.get(status)) != null)
27             return(text);
28         return("Server Flimsiness");
29     }
30     
31     public static String stripslashes(String p, boolean beg, boolean end) {
32         while(end && (p.length() > 0) && (p.charAt(p.length() - 1) == '/'))
33             p = p.substring(0, p.length() - 1);
34         while(beg && (p.length() > 0) && (p.charAt(0) == '/'))
35             p = p.substring(1);
36         return(p);
37     }
38
39     public static void cpstream(InputStream in, OutputStream out) throws IOException {
40         byte[] buf = new byte[4096];
41         while(true) {
42             int ret = in.read(buf, 0, buf.length);
43             if(ret < 0)
44                 return;
45             out.write(buf, 0, ret);
46         }
47     }
48     
49     public static Responder stdroot(Responder root) {
50         Responder ret = root;
51         ret = new Rehandler(ret);
52         ret = new ErrorHandler(ret);
53         return(ret);
54     }
55 }