package.path = package.path .. ";/opt/nevisproxy/webapp/WEB-INF/lib/lua/Utils.lua"
local Utils = require "Utils"

validLanguages = {}
validLanguages["DE"]=true
validLanguages["FR"]=true
validLanguages["IT"]=true
validLanguages["EN"]=true
validLanguages["RS"]=true

function inputHeader(req, resp)
  local trace = req:getTracer()

  local queryParams = Utils.getQueryParameters(req)
  local pathInfo = req:getPathInfo()

  -- only allow calls to the countries service ('api/v1/countries')
  if pathInfo ~= 'api/v1/countries' then
    trace:info("utility service called with invalid path " .. pathInfo)
    resp:send(404)
    return
  end

  -- only allow one query-parameter 'lang' with the values DE, FR, IT, EN, RS
  for param, values in pairs(queryParams) do
    if (param ~= 'lang') then
      trace:info("utility service called with invalid query param " .. param)
      resp:send(404)
      return
    end
    local count = 0
    if values ~= nil then
      for i, value in pairs(values) do
        count = count + 1
        local lang = string.upper(value)
        if not validLanguages[lang] then
          trace:info("utility service called with invalid value for query param " .. param .. "=" .. value)
          resp:send(404)
          return
        end
      end
    end 
    if count ~= 1 then
      trace:info("utility service called with invalid values for query param " .. param)
      resp:send(404)
      return
    end
  end

end
