Sha256: 347b402a0fc98561dceb5258091be7e6364c75bb60a664daaec966767181b47f

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents


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
    if (extension.casecmp('.html') == 0)
      begin
        serve_generated_file(path[0, path.length - 5], req, res)
      rescue Exception
        @logger.error("error serving the file #{path}")
        @logger.error($!)
        raise WEBrick::HTTPStatus::InternalServerError, 
          "#{$!}", caller
      end
    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
BuildMaster-0.7.0 lib/buildmaster/source_file_handler.rb
BuildMaster-0.6.0 lib/buildmaster/source_file_handler.rb