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')
16 class MissingParameterError < Exception; end
20 def rescue_action_locally( exception )
21 rescue_action_in_public( exception )
24 def rescue_action_in_public(exception)
25 @message = exception.message
27 render :template => "error", :layout => false, :status => 400
29 render :template => 'error', :layout => "application", :status => 400
35 @lang = cookies[:lang]
37 @lang = params[:lang][0]
39 @lang.gsub!(/_/, '-') if @lang
40 if !@lang || !LANGUAGES.include?( @lang )
41 if !request.compatible_language_from(LANGUAGES).blank?
42 @lang = request.compatible_language_from(LANGUAGES).dup
48 GetText.locale = @lang
53 @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
59 # load available distributions
60 def load_distributions
61 @distributions = Array.new
63 response = get_from_api("distributions")
64 doc = REXML::Document.new response.body
65 doc.elements.each("distributions/distribution") { |element|
66 dist = [element.elements['name'].text, element.elements['project'].text]
67 @distributions << dist
69 @distributions << ["ALL Distributions", 'ALL']
71 logger.error "Error while loading distributions: " + e.to_s
77 # special version of render json with JSONP capabilities (only needed for rails < 3.0)
78 def render_json(json, options = {})
79 callback, variable = params[:callback], params[:variable]
81 if callback && variable
82 "var #{variable} = #{json};\n#{callback}(#{variable});"
84 "var #{variable} = #{json};"
86 "#{callback}(#{json});"
91 render({:content_type => :js, :text => response}.merge(options))
94 def required_parameters(*parameters)
95 parameters.each do |parameter|
96 unless params.include? parameter.to_s
97 raise MissingParameterError, "Required Parameter #{parameter} missing"
103 def valid_package_name? name
104 name =~ /^[[:alnum:]][-_+\w\.:]*$/
107 def valid_project_name? name
108 name =~ /^[[:alnum:]][-+\w.:]+$/
114 def get_from_api(path)
115 uri_str = "#{API_HOST}/#{path}".gsub(' ', '%20')
116 uri = URI.parse(uri_str)
117 logger.debug "Loading from api: #{uri_str}"
119 http = Net::HTTP.new(uri.host, uri.port)
120 if uri.scheme == 'https'
122 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
124 request = Net::HTTP::Get.new("#{uri.path}?#{uri.query}")
125 api_user = API_USERNAME if defined? API_USERNAME
126 api_pass = API_PASSWORD if defined? API_PASSWORD
127 request['x-username'] = api_user
128 request.basic_auth api_user, api_pass unless (api_user.blank? || api_pass.blank?)
129 http.read_timeout = 15
130 response = http.request(request)
132 when Net::HTTPSuccess then response;
134 raise "Response was: #{response} #{response.body}"
136 rescue Exception => e
137 logger.error "Error connecting to #{uri_str}: #{e.to_s}"
138 raise "Error connecting to OBS API: #{e.to_s}"