01: package pygmy.core;
02:
03: import java.io.IOException;
04: import java.util.logging.Logger;
05: import java.util.logging.Level;
06:
07: public abstract class AbstractHandler implements Handler {
08:
09: private static final Logger log = Logger
10: .getLogger(AbstractHandler.class.getName());
11:
12: protected Server server;
13: protected String handlerName;
14: protected String urlPrefix;
15:
16: public static final ConfigOption URL_PREFIX_OPTION = new ConfigOption(
17: "url-prefix",
18: "/",
19: "URL prefix path for this handler. Anything that matches starts with this prefix will be handled by this handler.");
20:
21: public AbstractHandler() {
22: }
23:
24: public boolean initialize(String handlerName, Server server) {
25: this .server = server;
26: this .handlerName = handlerName;
27: this .urlPrefix = URL_PREFIX_OPTION.getProperty(server,
28: handlerName);
29: return true;
30: }
31:
32: public String getName() {
33: return handlerName;
34: }
35:
36: public boolean handle(Request aRequest, Response aResponse)
37: throws IOException {
38: if (aRequest instanceof HttpRequest) {
39: HttpRequest request = (HttpRequest) aRequest;
40: HttpResponse response = (HttpResponse) aResponse;
41: if (isRequestdForHandler(request)) {
42: return handleBody(request, response);
43: }
44: if (log.isLoggable(Level.INFO)) {
45: log.info("'" + request.getUrl()
46: + "' does not start with prefix '"
47: + getUrlPrefix() + "'");
48: }
49: }
50: return false;
51: }
52:
53: protected boolean isRequestdForHandler(HttpRequest request) {
54: return request.getUrl().startsWith(getUrlPrefix());
55: }
56:
57: protected boolean handleBody(HttpRequest request,
58: HttpResponse response) throws IOException {
59: return false;
60: }
61:
62: public boolean shutdown(Server server) {
63: return true;
64: }
65:
66: public String getUrlPrefix() {
67: return urlPrefix;
68: }
69:
70: protected String getMimeType(String filename) {
71: int index = filename.lastIndexOf(".");
72: String mimeType = null;
73: if (index > 0) {
74: mimeType = server.getProperty("mime"
75: + filename.substring(index).toLowerCase());
76: }
77:
78: return mimeType;
79: }
80:
81: }
|