Module: RubyAem::Handlers

Defined in:
lib/ruby_aem/handlers/xml.rb,
lib/ruby_aem/handlers/file.rb,
lib/ruby_aem/handlers/html.rb,
lib/ruby_aem/handlers/json.rb,
lib/ruby_aem/handlers/simple.rb

Overview

Response handlers for no payload.

Class Method Summary collapse

Class Method Details

.file_download(response, response_spec, call_params) ⇒ Object

Returns RubyAem::Result.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



39
40
41
42
43
44
45
46
# File 'lib/ruby_aem/handlers/file.rb', line 39

def self.file_download(response, response_spec, call_params)
  FileUtils.cp(response.body.path, call_params[:file_path].to_s)
  response.body.delete

  message = response_spec['message'] % call_params

  RubyAem::Result.new(message, response)
end

.html_authorizable_id(response, response_spec, call_params) ⇒ Object

Parse authorizable ID from response body data. This is used to get the authorizable ID of a newly created user/group.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_aem/handlers/html.rb', line 27

def self.html_authorizable_id(response, response_spec, call_params)
  html = Nokogiri::HTML(response.body)
  authorizable_id = html.xpath('//title/text()').to_s
  authorizable_id.slice! "Content created #{call_params[:path]}"
  call_params[:authorizable_id] = authorizable_id.sub(%r{^/}, '')

  message = response_spec['message'] % call_params

  RubyAem::Result.new(message, response)
end

.html_change_password(response, response_spec, call_params) ⇒ Object

Check response body for indicator of change password failure. If response body is empty, that indicates that there’s an error due to inexisting user. Successful action produces a HTML page as response body.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby_aem/handlers/html.rb', line 70

def self.html_change_password(response, response_spec, call_params)
  if response.body.to_s.empty?
    message = 'Failed to change password: Response body is empty, user likely does not exist.'
    result = RubyAem::Result.new(message, response)
    raise RubyAem::Error.new(message, result)
  end

  html = Nokogiri::HTML(response.body)
  user = html.xpath('//body/div/table/tr/td/b/text()').to_s
  desc = html.xpath('//body/div/table/tr/td/font/text()').to_s

  if desc == 'Password successfully changed.'
    call_params[:user] = user
    message = response_spec['message'] % call_params
    RubyAem::Result.new(message, response)
  else
    message = desc
    result = RubyAem::Result.new(message, response)
    raise RubyAem::Error.new(message, result)
  end
end

.html_package_service_allow_error(response, response_spec, call_params) ⇒ Object

Parse error message from response body data. This is to handle AEM hotfix, service pack, and feature pack package installation which might cause AEM to respond with error 500 but it’s still processing behind the scene.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_aem/handlers/html.rb', line 47

def self.html_package_service_allow_error(response, response_spec, call_params)
  html = Nokogiri::HTML(response.body)
  title = html.xpath('//title/text()').to_s
  desc = html.xpath('//p/text()').to_s
  reason = html.xpath('//pre/text()').to_s

  call_params[:title] = title
  call_params[:desc] = desc
  call_params[:reason] = reason

  message = response_spec['message'] % call_params

  RubyAem::Result.new(message, response)
end

.json_aem_health_check(response, response_spec, call_params) ⇒ Object

Handle AEM Health Check Servlet JSON payload.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    additional call_params information

Returns:

  • RubyAem::Result



89
90
91
92
93
94
95
96
97
# File 'lib/ruby_aem/handlers/json.rb', line 89

def self.json_aem_health_check(response, response_spec, call_params)
  json = JSON.parse(response.body)

  message = response_spec['message'] % call_params

  result = RubyAem::Result.new(message, response)
  result.data = json['results']
  result
end

.json_agents(response, response_spec, call_params) ⇒ Object

Extract a list of agent names from getAgents JSON payload.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    additional call_params information

Returns:

  • RubyAem::Result



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_aem/handlers/json.rb', line 105

def self.json_agents(response, response_spec, call_params)
  json = JSON.parse(response.body)

  agent_names = []
  json.each_key do |key|
    agent_names.push(key) if (!key.start_with? 'jcr:') && (!key.start_with? 'rep:')
  end

  message = response_spec['message'] % call_params

  result = RubyAem::Result.new(message, response)
  result.data = agent_names
  result
end

.json_authorizable_id(response, response_spec, call_params) ⇒ Object

Handle JSON response payload containing authorizable ID.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_aem/handlers/json.rb', line 27

def self.json_authorizable_id(response, response_spec, call_params)
  json = JSON.parse(response.body)
  authorizable_id = nil
  if json['success'] == true && json['hits'].length == 1
    authorizable_id = json['hits'][0]['name']
    call_params[:authorizable_id] = authorizable_id
    message = response_spec['message'] % call_params
  else
    message = "User/Group #{call_params[:name]} authorizable ID not found"
  end

  result = RubyAem::Result.new(message, response)
  result.data = authorizable_id
  result
end

.json_authorizable_keystore_exists(response, response_spec, call_params) ⇒ Object

Authorizable keystore payload handler, checks for exists and aliases properties in order to identify existence.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ruby_aem/handlers/json.rb', line 203

def self.json_authorizable_keystore_exists(response, response_spec, call_params)
  keystore_info = response.body

  result = Handlers.simple(response, response_spec, call_params)

  if keystore_info.exists == false
    result.data = false
    result.message = 'Authorizable keystore not found'
  elsif keystore_info.aliases.is_a?(Array)
    result.data = true
  end

  result
end

.json_certificate_chain_exists(response, response_spec, call_params) ⇒ Object

Authorizable keystore payload handler, checks for the existence of certificate within AEM Truststore, identified by cert_alias call parameter.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ruby_aem/handlers/json.rb', line 154

def self.json_certificate_chain_exists(response, response_spec, call_params)
  authorizable_keystore_info = response.body

  result = Handlers.simple(response, response_spec, call_params)

  certificate_chain_exists = false
  authorizable_keystore_info.aliases.each { |certificate_chain_alias|
    certificate_chain_exists = true if certificate_chain_alias._alias.to_s == call_params[:private_key_alias].to_s
  }
  if certificate_chain_exists == false
    result.data = false
    result.message = 'Certificate chain not found'
  else
    result.data = true
    result.message = 'Certificate chain exists'
  end

  result
end

.json_certificate_exists(response, response_spec, call_params) ⇒ Object

Truststore payload handler, checks for the existence of certificate within AEM Truststore, identified by cert_alias call parameter.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ruby_aem/handlers/json.rb', line 127

def self.json_certificate_exists(response, response_spec, call_params)
  truststore_info = response.body

  result = Handlers.simple(response, response_spec, call_params)

  certificate_exists = false
  truststore_info.aliases.each { |certificate_alias|
    certificate_exists = true if certificate_alias.serial_number.to_s == call_params[:serial_number].to_s
  }
  if certificate_exists == false
    result.data = false
    result.message = 'Certificate not found'
  else
    result.data = true
    result.message = 'Certificate exists'
  end

  result
end

.json_package_filter(response, response_spec, call_params) ⇒ Object

Handle package filter JSON payload.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    additional call_params information

Returns:

  • RubyAem::Result



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_aem/handlers/json.rb', line 68

def self.json_package_filter(response, response_spec, call_params)
  json = JSON.parse(response.body)

  filter = []
  json.each_key do |key|
    filter.push(json[key]['root']) unless json[key]['root'].nil?
  end

  message = response_spec['message'] % call_params

  result = RubyAem::Result.new(message, response)
  result.data = filter
  result
end

.json_package_service(response, _response_spec, _call_params) ⇒ Object

Handle package JSON payload. Result status is determined directly by success field. NOTE: _response_spec and _call_params are not used in the implementation of this method, but they are needed for the handler signature.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • _response_spec

    response specification as configured in conf/spec.yaml

  • _call_params

    additional call_params information

Returns:

  • RubyAem::Result

Raises:



51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_aem/handlers/json.rb', line 51

def self.json_package_service(response, _response_spec, _call_params)
  json = JSON.parse(response.body)

  message = json['msg']
  result = RubyAem::Result.new(message, response)

  return result if json['success'] == true

  raise RubyAem::Error.new(message, result)
end

.json_product_info(response, _response_spec, _call_params) ⇒ Object

Handle product info JSON payload. Result status is determined directly by success field. NOTE: _response_spec and _call_params are not used in the implementation of this method, but they are needed for the handler signature.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • _response_spec

    response specification as configured in conf/spec.yaml

  • _call_params

    additional call_params information

Returns:

  • RubyAem::Result



226
227
228
229
230
231
232
# File 'lib/ruby_aem/handlers/json.rb', line 226

def self.json_product_info(response, _response_spec, _call_params)
  message = 'AEM Product informations found'
  result = RubyAem::Result.new(message, response)
  result.data = response.body

  result
end

.json_truststore_exists(response, response_spec, call_params) ⇒ Object

Truststore payload handler, checks for exists and aliases properties in order to identify existence.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ruby_aem/handlers/json.rb', line 181

def self.json_truststore_exists(response, response_spec, call_params)
  truststore_info = response.body

  result = Handlers.simple(response, response_spec, call_params)

  if truststore_info.exists == false
    result.data = false
    result.message = 'Truststore not found'
  elsif truststore_info.aliases.is_a?(Array)
    result.data = true
  end

  result
end

.package_file_download(response, response_spec, call_params) ⇒ Object

Handle downloaded file by copying from temporary location to file_path call param. The downloaded file in temporary location will then be deleted. data, status_code, and headers are all returned from RubyAem::Client call.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



26
27
28
29
30
31
32
33
# File 'lib/ruby_aem/handlers/file.rb', line 26

def self.package_file_download(response, response_spec, call_params)
  FileUtils.cp(response.body.path, "#{call_params[:file_path]}/#{call_params[:package_name]}-#{call_params[:package_version]}.zip")
  response.body.delete

  message = response_spec['message'] % call_params

  RubyAem::Result.new(message, response)
end

.simple(response, response_spec, call_params) ⇒ Object

Simple handler by returning result that contains status and message as configured in conf/spec.yaml AS-IS.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



28
29
30
31
# File 'lib/ruby_aem/handlers/simple.rb', line 28

def self.simple(response, response_spec, call_params)
  message = response_spec['message'] % call_params
  RubyAem::Result.new(message, response)
end

.simple_body(response, response_spec, call_params) ⇒ Object

Simple handler with response body as result data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



86
87
88
89
90
# File 'lib/ruby_aem/handlers/simple.rb', line 86

def self.simple_body(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  result.data = response.body
  result
end

.simple_error(response, response_spec, call_params) ⇒ Object

Simple handler with raised error.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result

Raises:



75
76
77
78
# File 'lib/ruby_aem/handlers/simple.rb', line 75

def self.simple_error(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  raise RubyAem::Error.new(result.message, result)
end

.simple_false(response, response_spec, call_params) ⇒ Object

Simple handler with boolean false result data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



51
52
53
54
55
# File 'lib/ruby_aem/handlers/simple.rb', line 51

def self.simple_false(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  result.data = false
  result
end

.simple_nil(response, response_spec, call_params) ⇒ Object

Simple handler with nil result data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



63
64
65
66
67
# File 'lib/ruby_aem/handlers/simple.rb', line 63

def self.simple_nil(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  result.data = nil
  result
end

.simple_true(response, response_spec, call_params) ⇒ Object

Simple handler with boolean true result data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



39
40
41
42
43
# File 'lib/ruby_aem/handlers/simple.rb', line 39

def self.simple_true(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  result.data = true
  result
end

.xml_package_list(response, response_spec, call_params) ⇒ Object

Handle package list XML by removing non-packages data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_aem/handlers/xml.rb', line 26

def self.xml_package_list(response, response_spec, call_params)
  xml = Nokogiri::XML(response.body)

  status_code = xml.xpath('//crx/response/status/@code').to_s
  status_text = xml.xpath('//crx/response/status/text()').to_s

  if status_code == '200' && status_text == 'ok'
    message = response_spec['message'] % call_params
    result = RubyAem::Result.new(message, response)
    result.data = xml.xpath('//crx/response/data/packages')
  else
    result = RubyAem::Result.new("Unable to retrieve package list, getting status code #{status_code} and status text #{status_text}", response)
  end

  result
end