Sha256: 8d0446a12713a824699138b3700e82c419b28fe4498e5e857f3bb06c604dce33

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

$:.unshift File.dirname(__FILE__)

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.path, true)
  end
  
  def service(req, res)
    path = req.path_info
    file_processor = for_request_path(path)
    if (file_processor)
      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 for_request_path(request_path)
    template = @spec.load_template
    if (request_path[0,1] == '/')
      request_path = request_path[1, request_path.length - 1]
    end
    file = @spec.content_dir.file(request_path)
    processor = nil
    if (@spec.content_engines.supports?(file.extname))
      source, content_engine = @spec.content_engines.for_candidate(file.parent, file.basename)
      processor = FileProcessor.new(template, source, @spec) if source #todo test this if condition
    end
    return processor
  end
end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
BuildMaster-1.0.6 lib/buildmaster/site/source_file_handler.rb
BuildMaster-1.0.9 lib/buildmaster/site/source_file_handler.rb
BuildMaster-1.1.9 lib/buildmaster/site/source_file_handler.rb