public HttpServer.Response serveRequest(HttpServer.Request request) {
String path = request.getUrl();
if (!path.startsWith(basePath_)) {
return null;
}
final ServerResource resource =
resourceMap_.get(path);
if (resource == EXPIRED) {
return HttpServer.createErrorResponse(410, "Gone");
} else if (resource != null) {
Map<String,String> hdrMap = new HashMap<String,String>();
hdrMap.put("Content-Type", resource.getContentType());
long contentLength = resource.getContentLength();
if (contentLength >= 0) {
hdrMap.put("Content-Length",
Long.toString(contentLength));
}
String method = request.getMethod();
if (method.equals("HEAD")) {
return new HttpServer.Response(200, "OK", hdrMap) {
public void writeBody(OutputStream out) {
}
};
} else if (method.equals("GET")) {
return new HttpServer.Response(200, "OK", hdrMap) {
public void writeBody(OutputStream out)
throws IOException {
resource.writeBody(out);
}
};
} else {
return HttpServer
.createErrorResponse(405, "Unsupported method");