require 'webrick' require 'file_processor' module BuildMaster class SourceFileHandler < WEBrick::HTTPServlet::AbstractServlet # uncomment the following for automatic servlet reloading # def SourceFileHandler.get_instance config, *options # load __FILE__ # SourceFileHandler.new config, *options # end def initialize(server, spec) super @config = server.config @logger = @config[:Logger] @spec = spec @delegate = WEBrick::HTTPServlet::FileHandler.new(server, spec.content_dir, true) end def service(req, res) path = req.path_info extension = File.extname(path) if (extension == '') path = "#{path}index.html" extension = '.html' end file_processor = FileProcessor.for_request_path(path, @spec) if (file_processor.is_html?) begin content = file_processor.generate_document.to_s rescue Exception @logger.error("error serving the file #{path}") @logger.error($!) raise WEBrick::HTTPStatus::InternalServerError, "#{$!}", caller end res['content-type'] = 'text/html' res['content-length'] = content.length res['last-modified'] = DateTime.new res.body = content else @delegate.service(req, res) end end private def serve_generated_file(path, req, res) file_path = @spec.content_dir + path textile_file = "#{file_path}.textile" html_file = "#{file_path}.html" if File.file? textile_file # stats = File::stat(textile_file) document = FileProcessor.new(@spec.load_template, textile_file, @spec).process_textile() elsif File.file? html_file # stats = File::stat(html_file) document = FileProcessor.new(@spec.load_template, html_file, @spec).process_html() end if (document) content = document.to_s res['content-type'] = 'text/html' res['content-length'] = content.length # res['last-modified'] = stats.mtime.httpdate res['last-modified'] = DateTime.now res.body = content else @delegate.service(req, res) end end end end