Module: Nexpose::AJAX

Defined in:
lib/nexpose/ajax.rb

Overview

Accessor to the Nexpose AJAX API. These core methods should allow direct access to underlying controllers in order to test functionality that is not currently exposed through the XML API.

Class Method Summary collapse

Class Method Details

._headers(nsc, request) ⇒ Object

Attach necessary header fields.



107
108
109
110
# File 'lib/nexpose/ajax.rb', line 107

def _headers(nsc, request)
  request.add_field('nexposeCCSessionID', nsc.session_id)
  request.add_field('Cookie', "nexposeCCSessionID=#{nsc.session_id}")
end

._https(nsc) ⇒ Object

Use the Nexpose::Connection to establish a correct HTTPS object.



99
100
101
102
103
104
# File 'lib/nexpose/ajax.rb', line 99

def _https(nsc)
  http = Net::HTTP.new(nsc.host, nsc.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http
end

._request(nsc, request) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nexpose/ajax.rb', line 112

def _request(nsc, request)
  http = _https(nsc)
  _headers(nsc, request)

  # Return response body if request is successful. Brittle.
  response = http.request(request)
  case response
  when Net::HTTPOK
    response.body
  else
    req_type = request.class.name.split('::').last
    raise Nexpose::APIError.new(response, "#{req_type} request to #{request.path} failed. #{request.body}")
  end
end

.delete(nsc, uri, content_type = 'text/xml') ⇒ Object

DELETE call to a Nexpose controller.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • uri (String)

    Controller address relative to host:port

  • content_type (String) (defaults to: 'text/xml')

    Content type to use when issuing the DELETE.



78
79
80
81
82
# File 'lib/nexpose/ajax.rb', line 78

def delete(nsc, uri, content_type = 'text/xml')
  delete = Net::HTTP::Delete.new(uri)
  delete.set_content_type(content_type)
  _request(nsc, delete)
end

.form_post(nsc, uri, parameters, content_type = 'application/x-www-form-urlencoded; charset=UTF-8') ⇒ Hash

POST call to a Nexpose controller that uses a form-post model. This is here to support legacy use of POST in old controllers.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • uri (String)

    Controller address relative to host:port

  • parameters (Hash)

    Hash of attributes that need to be sent to the controller.

  • content_type (String) (defaults to: 'application/x-www-form-urlencoded; charset=UTF-8')

    Content type to use when issuing the POST.

Returns:

  • (Hash)

    The parsed JSON response from the call.



66
67
68
69
70
71
# File 'lib/nexpose/ajax.rb', line 66

def form_post(nsc, uri, parameters, content_type = 'application/x-www-form-urlencoded; charset=UTF-8')
  post = Net::HTTP::Post.new(uri)
  post.set_content_type(content_type)
  post.set_form_data(parameters)
  _request(nsc, post)
end

.get(nsc, uri, content_type = 'text/xml; charset=UTF-8') ⇒ String|REXML::Document|Hash

GET call to a Nexpose controller.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • uri (String)

    Controller address relative to host:port

  • content_type (String) (defaults to: 'text/xml; charset=UTF-8')

    Content type to use when issuing the GET.

Returns:

  • (String|REXML::Document|Hash)

    The response from the call.



20
21
22
23
24
# File 'lib/nexpose/ajax.rb', line 20

def get(nsc, uri, content_type = 'text/xml; charset=UTF-8')
  get = Net::HTTP::Get.new(uri)
  get.set_content_type(content_type)
  _request(nsc, get)
end

.parametrize_uri(uri, parameters) ⇒ Hash

Append the query parameters to given URI.

Parameters:

  • uri (String)

    Controller address relative to host:port

  • parameters (Hash)

    Hash of attributes that need to be sent to the controller.

Returns:

  • (Hash)

    The parametrized URI.



91
92
93
# File 'lib/nexpose/ajax.rb', line 91

def parametrize_uri(uri, parameters)
  uri = uri.concat(('?').concat(parameters.map { |k, v| "#{k}=#{CGI.escape(v[0].to_s)}" }.join('&'))) if parameters
end

.post(nsc, uri, payload = nil, content_type = 'text/xml') ⇒ String|REXML::Document|Hash

POST call to a Nexpose controller.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • uri (String)

    Controller address relative to host:port

  • payload (String|REXML::Document) (defaults to: nil)

    XML document required by the call.

  • content_type (String) (defaults to: 'text/xml')

    Content type to use when issuing the POST.

Returns:

  • (String|REXML::Document|Hash)

    The response from the call.



49
50
51
52
53
54
# File 'lib/nexpose/ajax.rb', line 49

def post(nsc, uri, payload = nil, content_type = 'text/xml')
  post = Net::HTTP::Post.new(uri)
  post.set_content_type(content_type)
  post.body = payload.to_s if payload
  _request(nsc, post)
end

.put(nsc, uri, payload = nil, content_type = 'text/xml; charset=UTF-8') ⇒ String

PUT call to a Nexpose controller.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • uri (String)

    Controller address relative to host:port

  • payload (String|REXML::Document) (defaults to: nil)

    XML document required by the call.

  • content_type (String) (defaults to: 'text/xml; charset=UTF-8')

    Content type to use when issuing the PUT.

Returns:

  • (String)

    The response from the call.



34
35
36
37
38
39
# File 'lib/nexpose/ajax.rb', line 34

def put(nsc, uri, payload = nil, content_type = 'text/xml; charset=UTF-8')
  put = Net::HTTP::Put.new(uri)
  put.set_content_type(content_type)
  put.body = payload.to_s if payload
  _request(nsc, put)
end