Class: Upman::Server::BaseServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Includes:
Utils::Helper
Defined in:
lib/upman/server/base_servlet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Helper

#fail, #info, #success, #warn

Constructor Details

#initialize(server, *options) ⇒ BaseServlet

Returns a new instance of BaseServlet.



9
10
11
# File 'lib/upman/server/base_servlet.rb', line 9

def initialize(server, *options)
  super(server, *options)
end

Instance Attribute Details

#error_msgObject

Returns the value of attribute error_msg.



7
8
9
# File 'lib/upman/server/base_servlet.rb', line 7

def error_msg
  @error_msg
end

#queryObject

Returns the value of attribute query.



7
8
9
# File 'lib/upman/server/base_servlet.rb', line 7

def query
  @query
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/upman/server/base_servlet.rb', line 7

def response
  @response
end

Instance Method Details

#bad_request(response, body) ⇒ Object



49
50
51
52
53
54
# File 'lib/upman/server/base_servlet.rb', line 49

def bad_request(response, body)
  response.status = 400
  response['Content-Type'] = 'application/json'
  response.body = "{\"status\": \"bad_request\", \"message\": \"#{body}\"}"
  response
end

#do_GET(_request, response) ⇒ Object

rubocop:disable Naming/MethodName



14
15
16
17
18
19
20
21
# File 'lib/upman/server/base_servlet.rb', line 14

def do_GET(_request, response)
  # rubocop:enable Naming/MethodName

  info "Process API request #{_request.path}"

  @response = response
  @query = _request.query
end

#get_param(key, validate, require = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/upman/server/base_servlet.rb', line 56

def get_param(key, validate, require = false)
  if @query.nil?
    return ''
  end

  unless @query.include? key
    unless require
      return ''
    else
      return nil
    end
  end
  unless validate.nil?
    if validate == 'date'
      begin
        @query[key] = DateTime.parse(@query[key])
      rescue
        msg = "Invalid content for param #{key}, expected type: date - found #{@query[key]}"
        bad_request(@response, msg)
        fail msg
        return false
      end
    end
  end
  @query[key]
end

#is_authenticated(request) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/upman/server/base_servlet.rb', line 23

def is_authenticated(request)
  node_service = Upman::Service::Node.new
  node_uuid = node_service.get_node_uuuid

  info "Checking Authentication"
  return true if request["x-upman-token"] == node_uuid

  fail "Access denied"

  false
end

#not_authorized(response, body) ⇒ Object



42
43
44
45
46
47
# File 'lib/upman/server/base_servlet.rb', line 42

def not_authorized(response, body)
  response.status = 401
  response['Content-Type'] = 'application/json'
  response.body = "{\"status\": \"not_authorized\", \"message\": \"#{body}\"}"
  response
end

#ok(response, body) ⇒ Object



35
36
37
38
39
40
# File 'lib/upman/server/base_servlet.rb', line 35

def ok(response, body)
  response.status = 200
  response['Content-Type'] = 'application/json'
  response.body = body
  response
end