require 'jsender' require 'soap4juddi' module SoarSr class Validator def present?(field) not (field.nil? or (field.is_a?(String) and field.strip == "")) end def provided?(field, label) raise ValidationError, "no #{label} provided" if field.nil? raise ValidationError, "invalid #{label} provided" if (field.is_a?(String) and field.strip == "") true end def key_provided?(field, key, label) msg_no = "no #{label} provided" msg_invalid = "invalid #{label} provided" raise ValidationError, msg_no if field.nil? raise ValidationError, msg_invalid if (not field.is_a? Hash) raise ValidationError, msg_no if field[key].nil? raise ValidationError, msg_invalid if (not field[key].is_a? String) or (field[key].strip == "") true end def length_at_least?(field, min, label) raise ValidationError, "invalid #{label} provided" if (field.size < min) true end def credentials?(credentials) provided?(credentials['username'], 'username') and provided?(credentials['password'], 'password') end def authorized?(result) raise ValidationError, 'not authorized' if (notifications_include?(result, 'E_authTokenRequired')) or (notifications_include?(result, 'E_keyUnavailable')) or (notifications_include?(result, 'E_userMismatch')) or (notifications_include?(result, 'not authorized')) true end def identifier?(result, label) raise ValidationError, "invalid #{element} identifier provided" if notifications_include?(result, 'E_invalidKeyPassed') true end def uri?(uri) raise ValidationError, 'invalid URI' if not (uri =~ URI::DEFAULT_PARSER.regexp[:UNSAFE]).nil? true end def meta?(meta) raise ValidationError, 'invalid meta' if not meta.is_a?(Hash) true end def type?(type) ['domains', 'services', 'teams', 'service_components'].include?(type) end def wadl?(definition) raise ValidationError, 'invalid service definition provided' if not definition.include?("wadl") true end def contact?(contact) error = 'invalid contact details provided' raise ValidationError, error if not contact.is_a?(Hash) raise ValidationError, error if contact['name'].nil? or contact['name'].strip == "" true end def type_registered?(element, type) return is_registered?(@registry.domain_perspectives.domain_perspective_registered?(element)) if (type == 'domains') return is_registered?(@registry.service_components.service_component_registered?(element)) if (type == 'service components') return is_registered?(@registry.services.service_registered?(element)) if (type == 'services') return is_registered?(@registry.teams.team_registered?(element)) if (type == 'teams') false end def not_registered?(element, type) raise ValidationError, "#{one_of(type)} already exists" if type_registered?(element, type) true end def any_registered?(domain_perspective) raise ValidationError, "unknown domain perspective provided" if (not type_registered?(domain_perspective, 'domains')) and (not type_registered?(domain_perspective, 'teams')) end def registered?(element, type) raise ValidationError, "unknown #{one_of(type)} provided" if not type_registered?(element, type) true end def one_of(type) result = type[0..-2] result = 'domain perspective' if result == 'domain' result end end end