1 # Filters added to this controller apply to all controllers in the application.
2 # Likewise, all the methods added will be available for all controllers.
6 class ApplicationController < ActionController::Base
8 before_filter :set_language
9 before_filter :set_distributions
11 helper :all # include all helpers, all the time
12 require "rexml/document"
14 init_gettext('software')
18 def rescue_action_locally( exception )
19 rescue_action_in_public( exception )
22 def rescue_action_in_public(exception)
23 @message = exception.message
25 render :template => "error", :layout => false, :status => 400
27 render :template => 'error', :layout => "application", :status => 400
33 @lang = cookies[:lang]
35 @lang = params[:lang][0]
37 @lang.gsub!(/_/, '-') if @lang
38 if !@lang || !LANGUAGES.include?( @lang )
39 if !request.compatible_language_from(LANGUAGES).blank?
40 @lang = request.compatible_language_from(LANGUAGES).dup
46 GetText.locale = @lang
51 @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
57 # load available distributions
58 def load_distributions
59 @distributions = Array.new
61 response = get_from_api("distributions")
62 doc = REXML::Document.new response.body
63 doc.elements.each("distributions/distribution") { |element|
64 dist = [element.elements['name'].text, element.elements['project'].text]
65 @distributions << dist
67 @distributions << ["ALL Distributions", 'ALL']
69 logger.error "Error while loading distributions: " + e.to_s
75 # special version of render json with JSONP capabilities (only needed for rails < 3.0)
76 def render_json(json, options = {})
77 callback, variable = params[:callback], params[:variable]
79 if callback && variable
80 "var #{variable} = #{json};\n#{callback}(#{variable});"
82 "var #{variable} = #{json};"
84 "#{callback}(#{json});"
89 render({:content_type => :js, :text => response}.merge(options))
94 def get_from_api(path)
95 uri_str = "#{API_HOST}/#{path}".gsub(' ', '%20')
96 uri = URI.parse(uri_str)
97 logger.debug "Loading from api: #{uri_str}"
99 http = Net::HTTP.new(uri.host, uri.port)
100 if uri.scheme == 'https'
102 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
104 request = Net::HTTP::Get.new("#{uri.path}?#{uri.query}")
105 api_user = API_USERNAME if defined? API_USERNAME
106 api_pass = API_PASSWORD if defined? API_PASSWORD
107 request['x-username'] = api_user
108 request.basic_auth api_user, api_pass unless (api_user.blank? || api_pass.blank?)
109 http.read_timeout = 15
110 response = http.request(request)
112 when Net::HTTPSuccess then response;
114 raise "Response was: #{response} #{response.body}"
116 rescue Exception => e
117 raise "Error connecting to #{uri_str}: #{e.to_s}"