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_distributions
7 before_filter :set_language
8 helper :all # include all helpers, all the time
9 require "rexml/document"
11 init_gettext('software')
13 def rescue_action_in_public(exception)
14 @message = exception.message
16 render :template => "error", :layout => false, :status => 400
18 render :template => 'error', :layout => "application", :status => 400
26 @lang = cookies[:lang]
28 @lang = params[:lang][0]
30 @lang.gsub!(/_/, '-') if @lang
31 if !@lang || !LANGUAGES.include?( @lang )
32 if !request.compatible_language_from(LANGUAGES).blank?
33 @lang = request.compatible_language_from(LANGUAGES).dup
39 GetText.locale = @lang
44 @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
50 # load available distributions
51 def load_distributions
52 uri = URI.parse("http://#{API_HOST}/distributions")
53 request = Net::HTTP::Get.new(uri.path)
54 logger.debug "Loading distributions from #{uri}"
55 @distributions = Array.new
57 Net::HTTP.start(uri.host, uri.port) do |http|
58 http.read_timeout = 30
59 response = http.request(request)
60 unless( response.kind_of? Net::HTTPSuccess )
61 logger.error "Cannot load distributions: '#{response.code}', message: \n#{response.body}"
63 doc = REXML::Document.new response.body
64 doc.elements.each("distributions/distribution") { |element|
65 dist = [element.elements['name'].text, element.elements['project'].text]
66 @distributions << dist
70 @distributions << ["ALL Distributions", 'ALL']
73 logger.error "Error while loading distributions from '#{uri}': " + e.to_s