# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.

require_relative '../service'

module CIMI::Collections
  class Base < Sinatra::Base

    Sinatra::Rabbit.configure do
      enable :use_namespace
    end

    helpers Sinatra::Rabbit::URLHelper

    Sinatra::Rabbit::Collection.extend(CIMI::RabbitHelper)

    include Sinatra::Rabbit
    include Sinatra::Rabbit::Features
    include CIMI::Service

    helpers Deltacloud::Helpers::Drivers
    helpers Deltacloud::Helpers::Database
    helpers Sinatra::AuthHelper

    helpers Deltacloud::Helpers::Application
    helpers CIMI::Helper

    register Rack::RespondTo

    enable :method_override
    disable :show_exceptions
    disable :dump_errors

    set :config, Deltacloud[:cimi]
    set :root_url, config.root_url
    set :root_path, config.root_url
    set :version, config.version
    set :root, File.join(File.dirname(__FILE__), '..', '..', '..')
    set :views, File.join(root, 'views')
    set :public_folder, File.join(root, 'public')
    set :documentation_class, CIMI
    set :haml, :format => :xhtml

    error do
      report_error
    end

    error Deltacloud::Exceptions::ValidationFailure do
      report_error
    end

    error CIMI::Model::ValidationError do
      report_error
    end

    error CIMI::Model::NotFound do
      report_error(404, 'Not found')
    end

    error JSON::ParserError do
      report_error(400, "Bad request (#{request.env['sinatra.error'].message})")
    end

    error REXML::ParseException do
      report_error(400)
    end

    before do
      # Respond with 400, If we don't get a http Host header,
      halt 400, "Unable to find HTTP Host header" if @env['HTTP_HOST'] == nil
    end

  end
end
