module RubyAem::Handlers
Response
handlers for file payload.
Response
handlers for HTML payload. AEM response body needs to be sanitized due to missing closing HTML tags scattered across many AEM web pages. The sanitisations are done manually using simple gsub call in order to avoid dependency to nokogiri which carries native compilation cost and security vulnerability on libxml dependency.
Response
handlers for JSON payload.
Response
handlers for no payload.
Response
handlers for XML payload.
Public Class Methods
Sanitise HTML string but only when the regex to be replaced actually exists. The intention for sanitising the HTML is to strip out unused invalid HTML tags, so that the remaining HTML is valid for rexml to parse. It's important to not assume that the regex exists due to the possibility of future AEM versions to produce a different response body that might / might not contain the invalid HTML tags on the older AEM versions.
@param html HTML response body string @param regex Ruby regular expression, all regex matches will be replaced @param replacement all existence of the regex will be replaced with this string
# File lib/ruby_aem/handlers/html.rb, line 111 def self._sanitise_html(html, regex, replacement) if regex.match?(html) html.gsub!(regex, replacement) else html end end
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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
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.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# File lib/ruby_aem/handlers/html.rb, line 77 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 sanitized_body = _sanitise_html(response.body, /<input.+>/, '') sanitized_body = _sanitise_html(sanitized_body, /< 0/, '< 0') html = Document.new(sanitized_body) user = XPath.first(html, '//body/div/table/tr/td/b/text()').to_s desc = XPath.first(html, '//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
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.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# File lib/ruby_aem/handlers/html.rb, line 54 def self.html_package_service_allow_error(response, response_spec, call_params) html = Document.new(response.body) title = XPath.first(html, '//title/text()').to_s desc = XPath.first(html, '//p/text()').to_s reason = XPath.first(html, '//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
Handle AEM Health Check Servlet JSON payload.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params additional call_params information @return RubyAem::Result
# 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
Extract a list of agent names from getAgents JSON payload.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params additional call_params information @return RubyAem::Result
# 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
Authorizable keystore payload handler, checks for the existence of certificate within AEM Truststore, identified by cert_alias call parameter.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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
Truststore payload handler, checks for the existence of certificate within AEM Truststore, identified by cert_alias call parameter.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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
Handle package filter JSON payload.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params additional call_params information @return RubyAem::Result
# 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
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.
@param response HTTP response containing status_code, body, and headers @param _response_spec response specification as configured in conf/spec.yaml @param _call_params additional call_params information @return RubyAem::Result
# 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
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.
@param response HTTP response containing status_code, body, and headers @param _response_spec response specification as configured in conf/spec.yaml @param _call_params additional call_params information @return RubyAem::Result
# 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
Truststore payload handler, checks for exists and aliases properties in order to identify existence.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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
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.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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 handler by returning result that contains status and message as configured in conf/spec.yaml AS-IS.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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 handler with response body as result data.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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 handler with raised error.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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 handler with boolean false result data.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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 handler with nil result data.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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 handler with boolean true result data.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# 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
Handle package list XML by removing non-packages data.
@param response HTTP response containing status_code, body, and headers @param response_spec response specification as configured in conf/spec.yaml @param call_params API call parameters @return RubyAem::Result
# File lib/ruby_aem/handlers/xml.rb, line 28 def self.xml_package_list(response, response_spec, call_params) xml = Document.new(response.body) status_code = XPath.first(xml, '//crx/response/status/@code').to_s status_text = XPath.first(xml, '//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 = XPath.first(xml, '//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