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.
4 class ApplicationController < ActionController::Base
6 before_filter :set_language
7 before_filter :set_distributions
9 helper :all # include all helpers, all the time
10 require "rexml/document"
12 init_gettext('software')
16 def rescue_action_locally( exception )
17 rescue_action_in_public( exception )
20 def rescue_action_in_public(exception)
21 @message = exception.message
23 render :template => "error", :layout => false, :status => 400
25 render :template => 'error', :layout => "application", :status => 400
31 @lang = cookies[:lang]
33 @lang = params[:lang][0]
35 @lang.gsub!(/_/, '-') if @lang
36 if !@lang || !LANGUAGES.include?( @lang )
37 if !request.compatible_language_from(LANGUAGES).blank?
38 @lang = request.compatible_language_from(LANGUAGES).dup
44 GetText.locale = @lang
49 @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
55 # load available distributions
56 def load_distributions
57 uri = URI.parse("http://#{API_HOST}/distributions")
58 request = Net::HTTP::Get.new(uri.path)
59 logger.debug "Loading distributions from #{uri}"
60 @distributions = Array.new
62 Net::HTTP.start(uri.host, uri.port) do |http|
63 http.read_timeout = 30
64 response = http.request(request)
65 unless( response.kind_of? Net::HTTPSuccess )
66 logger.error "Cannot load distributions: '#{response.code}', message: \n#{response.body}"
68 doc = REXML::Document.new response.body
69 doc.elements.each("distributions/distribution") { |element|
70 dist = [element.elements['name'].text, element.elements['project'].text]
71 @distributions << dist
75 @distributions << ["ALL Distributions", 'ALL']
78 logger.error "Error while loading distributions from '#{uri}': " + e.to_s