restful-riskpointdata-domains 0.6.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,258 @@
1
+ # encoding: UTF-8
2
+
3
+ # ######################################################################## #
4
+ # File: razor_risk/cassini/applications/route_verb_adaptors/risk_point_data/domains/collection_get.rb
5
+ #
6
+ # Purpose: Adaptor for RPD microservice's collection GET verb
7
+ #
8
+ # Author: Matthew Wilson
9
+ #
10
+ # Copyright (c) 2018, Razor Risk Technologies Pty Ltd
11
+ # All rights reserved.
12
+ #
13
+ # ######################################################################## #
14
+
15
+
16
+ # ##########################################################################
17
+ # requires
18
+
19
+ require 'razor_risk/cassini/applications/route_verb_adaptors/utilities/collection_get_helper'
20
+ require 'razor_risk/cassini/applications/route_verb_adaptors/utilities/risk_point_data/domains'
21
+ require 'razor_risk/cassini/applications/rest_framework/verb_handler'
22
+
23
+ require 'razor_risk/cassini/util/conversion_util'
24
+
25
+ require 'razor_risk/razor/connectivity/razor_3/entity_connectors/risk_point_data/domains_connector'
26
+ require 'razor_risk/razor/connectivity/search/parsers/multi_value_conjunction_parser'
27
+
28
+ require 'razor_risk/core/diagnostics/logger'
29
+
30
+ require 'active_support/core_ext/hash'
31
+ require 'json'
32
+ require 'nokogiri'
33
+ require 'pantheios'
34
+ require 'xqsr3/conversion/integer_parser'
35
+ require 'fastxml'
36
+ require 'xmlhasher'
37
+
38
+ # ##########################################################################
39
+ # module
40
+
41
+ module RazorRisk
42
+ module Cassini
43
+ module Applications
44
+ module RouteVerbAdaptors
45
+ module RiskPointData
46
+ module Domains
47
+
48
+ # ##########################################################################
49
+ # classes
50
+
51
+ class CollectionGet < RESTFramework::VerbHandler
52
+
53
+ include ::RazorRisk::Cassini::Applications::RouteVerbAdaptors::Utilities::RiskPointData::Domains
54
+ include ::RazorRisk::Cassini::Applications::RouteVerbAdaptors::Utilities::CollectionGetHelper
55
+ include ::RazorRisk::Cassini::Util::ConversionUtil
56
+
57
+ include ::RazorRisk::Razor::Connectivity::Razor3::EntityConnectors::RiskPointData
58
+ include ::RazorRisk::Razor::Connectivity::Search::Parsers
59
+
60
+ include ::RazorRisk::Core::Diagnostics::Logger
61
+
62
+ include ::Pantheios
63
+
64
+ HTTP_VERB = :get
65
+
66
+ HTTP_ACCEPTS = %w{ application/xml application/json text/xml }
67
+
68
+ ROUTE_VARIABLES = %w{
69
+
70
+ domain
71
+ }
72
+
73
+ QUERY_PARAMETERS = %w{
74
+
75
+ field-search
76
+
77
+ filter-name
78
+
79
+ page-base
80
+ page-extent
81
+
82
+ sort-field
83
+ sort-direction
84
+ isModified
85
+ oper-type
86
+ }
87
+
88
+ def handle env, params, request, response
89
+
90
+ trace ParamNames[ :env, :params, :request, :response ], env, params, request, response
91
+
92
+ # params
93
+
94
+ domain = infer_domain(params)
95
+
96
+ validate_exclusive_search_parameters params, 'filter-name', 'field-search', message_scope: 'invalid search specifier'
97
+
98
+ field_srch = params['field-search'].to_s.strip
99
+ field_srch = nil if field_srch.empty?
100
+ filter_name = params['filter-name'].to_s.strip
101
+ filter_name = nil if filter_name.empty?
102
+ page_base = ::Xqsr3::Conversion::IntegerParser.to_integer(params['page-base'] || 0) { |x, arg| halt 422, {}, "invalid page specifier: 'page-base'=#{arg}" }
103
+ page_extent = ::Xqsr3::Conversion::IntegerParser.to_integer(params['page-extent'] || 1000) { |x, arg| halt 422, {}, "invalid page specifier: 'page-extent'=#{arg}" }
104
+ sort_field = params['sort-field'].to_s.strip
105
+ sort_field = nil if sort_field.empty?
106
+ sort_direction = params['sort-direction'].to_s.strip
107
+ sort_direction = nil if sort_direction.empty?
108
+ isModified = params['isModified']
109
+ isModified ? isModified = true : isModified = false
110
+ operType = params['oper-type']
111
+ operType = operType === "OR"? "OR" : "AND"
112
+
113
+ if page_base < 0 || page_extent < 1
114
+
115
+ halt 416, {}, "invalid page specifier: 'page-base'=#{page_base}; 'page-extent'=#{page_extent}"
116
+ end
117
+
118
+ unless sort_direction.nil? or sort_direction =~ /^ascending|descending$/
119
+
120
+ halt 400, {'Content-Type' => 'text/plain'}, "invalid sort direction, must be 'ascending' or 'descending'; was #{sort_direction}"
121
+ end
122
+
123
+ if field_srch
124
+ begin
125
+ cp = MultiValueConjunctionParser.new
126
+ field_srch = cp.parse_conjunction_list field_srch
127
+ rescue MultiValueConjunctionParser::SyntaxError => x
128
+ halt 422, {'Content-Type' => 'text/plain'}, x.message
129
+ end
130
+ end
131
+
132
+ rq_opts = {
133
+
134
+ :field_search => field_srch,
135
+ :filter_name => filter_name,
136
+ :page_base => page_base,
137
+ :page_extent => page_extent,
138
+ :sort_field => sort_field,
139
+ :sort_direction => sort_direction,
140
+ :operType => operType,
141
+ }
142
+
143
+ # get credentials
144
+
145
+ cr = get_required_credentials
146
+
147
+ # get requester
148
+
149
+ rr = settings.razor_requester
150
+
151
+ # get connector
152
+
153
+ ec = DomainsConnector.new(rr, credentials: cr, **rq_opts)
154
+
155
+ # issue request
156
+
157
+ qr = ec.get_records domain, indicate_result_by: :qualified_result, result_form: :body
158
+
159
+ log :debug1, "qr(#{qr.class})='#{qr}'"
160
+
161
+ unless qr.succeeded?
162
+
163
+ if false
164
+
165
+ ;
166
+ elsif :no_such_filter == qr.result
167
+
168
+ log :debug1, "failed to retrieve risk point data for domain '#{domain}': no such filter '#{filter_name}'"
169
+
170
+ halt 422, {}, "no such filter '#{filter_name}'"
171
+ elsif r = qr.failure_qualifier.reasons.lookup?('RZ0011')
172
+
173
+ log :debug1, "failed to retrieve risk point data for domain '#{domain}'"
174
+
175
+ halt 404, {}, "#{r.message}: #{r.details}"
176
+ else
177
+
178
+ log :warning, "failed to retrieve risk point data for domain '#{domain}'"
179
+
180
+ halt 500, {}, qr.failure_qualifier.reasons.join("\n")
181
+ end
182
+ end
183
+
184
+ r = qr.result.to_s
185
+
186
+ # return result
187
+
188
+ status 200
189
+
190
+ if false
191
+ elsif request.accept?('application/xml')
192
+
193
+ log :debug1, 'application/xml'
194
+ content_type 'application/xml'
195
+ r
196
+ elsif request.accept?('application/json')
197
+
198
+ log :debug1, 'application/json'
199
+
200
+ content_type 'application/json'
201
+
202
+ # TODO: this switch should be removed. This is a temporary fix
203
+ # for continuation of UI development only. This code is repeated
204
+ # in item_get.rb where it must also be removed.
205
+ unless ENV['RR_TOOL_WEBAPI_RISKPOINTDATA_DOMAINS_JSON_OUTPUT_FORMAT_SWITCH']
206
+
207
+ xml = ::Nokogiri::XML(r)
208
+
209
+ if isModified
210
+ xml.xpath('//field').each do |e|
211
+ if e['id']
212
+ e.name = e['id']
213
+ e['text'] = e.text
214
+ e.content = ""
215
+ end
216
+ end
217
+ resp_hash = XmlHasher.parse(xml.to_xml)
218
+ else
219
+ xml.xpath('//field').each { |e| e.name = e['id'] }
220
+ resp_hash = Hash.from_xml xml.to_xml
221
+ end
222
+
223
+ resp_json = resp_hash
224
+
225
+ JSON.generate resp_json
226
+ else
227
+
228
+ convert_XML_to_JSON r, { scheme: :gdata }
229
+ end
230
+ elsif request.accept?('text/xml')
231
+
232
+ log :debug1, 'text/xml'
233
+
234
+ content_type 'text/xml'
235
+
236
+ r
237
+ else
238
+
239
+ log :violation, "unexpected failure to match given 'Accept' header '#{request.accept}'"
240
+
241
+ status 500
242
+ end
243
+ end
244
+ end # class CollectionGet
245
+
246
+ # ##########################################################################
247
+ # module
248
+
249
+ end # module Domains
250
+ end # module RiskPointData
251
+ end # module RouteVerbAdaptors
252
+ end # module Applications
253
+ end # module Cassini
254
+ end # module RazorRisk
255
+
256
+ # ############################## end of file ############################# #
257
+
258
+
@@ -0,0 +1,193 @@
1
+ # encoding: UTF-8
2
+
3
+ # ######################################################################## #
4
+ # File: razor_risk/cassini/applications/route_verb_adaptors/risk_point_data/domains/item_get.rb
5
+ #
6
+ # Purpose: Adaptor for RPD microservice's item GET verb
7
+ #
8
+ # Author: Matthew Wilson
9
+ #
10
+ # Copyright (c) 2018, Razor Risk Technologies Pty Ltd
11
+ # All rights reserved.
12
+ #
13
+ # ######################################################################## #
14
+
15
+
16
+ # ##########################################################################
17
+ # requires
18
+
19
+ require 'razor_risk/cassini/applications/route_verb_adaptors/utilities/risk_point_data/domains'
20
+ require 'razor_risk/cassini/applications/rest_framework/verb_handler'
21
+
22
+ require 'razor_risk/cassini/util/conversion_util'
23
+
24
+ require 'razor_risk/razor/connectivity/razor_3/entity_connectors/risk_point_data/domains_connector'
25
+
26
+ require 'razor_risk/core/diagnostics/logger'
27
+
28
+ require 'active_support/core_ext/hash'
29
+ require 'json'
30
+ require 'nokogiri'
31
+ require 'pantheios'
32
+
33
+ # ##########################################################################
34
+ # module
35
+
36
+ module RazorRisk
37
+ module Cassini
38
+ module Applications
39
+ module RouteVerbAdaptors
40
+ module RiskPointData
41
+ module Domains
42
+
43
+ # ##########################################################################
44
+ # classes
45
+
46
+ class RiskPointGet < RESTFramework::VerbHandler
47
+
48
+ include ::RazorRisk::Cassini::Applications::RouteVerbAdaptors::Utilities::RiskPointData::Domains
49
+
50
+ include ::RazorRisk::Cassini::Util::ConversionUtil
51
+
52
+ include ::RazorRisk::Razor::Connectivity::Razor3::EntityConnectors::RiskPointData
53
+
54
+ include ::RazorRisk::Core::Diagnostics::Logger
55
+
56
+ include ::Pantheios
57
+
58
+ HTTP_VERB = :get
59
+
60
+ HTTP_ACCEPTS = %w{ application/xml application/json text/xml }
61
+
62
+ ROUTE_VARIABLES = %w{
63
+ }
64
+
65
+ QUERY_PARAMETERS = %w{
66
+
67
+ result-form
68
+ }
69
+
70
+ def handle env, params, request, response
71
+
72
+ trace ParamNames[ :env, :params, :request, :response ], env, params, request, response
73
+
74
+ # params
75
+
76
+ rf = case rf = (params['result-form'] || '').to_s
77
+ when '', 'default'
78
+ :body
79
+ when 'summary'
80
+ :rpd_summary
81
+ when 'rpd'
82
+ :rpd
83
+ else
84
+ log(:warning) { "unrecognised result-form '#{rf}'" }
85
+ :body
86
+ end
87
+
88
+ # get credentials
89
+
90
+ cr = get_required_credentials
91
+
92
+ # get requester
93
+
94
+ rr = settings.razor_requester
95
+
96
+ # get connector
97
+
98
+ ec = DomainsConnector.new(rr, credentials: cr)
99
+
100
+ # issue request
101
+
102
+ qr = ec.get_riskpoint_domian indicate_result_by: :qualified_result, result_form: rf
103
+
104
+ log :debug1, "qr(#{qr.class})='#{qr}'"
105
+
106
+ unless qr.succeeded?
107
+
108
+ if false
109
+
110
+ ;
111
+ elsif qr.result.nil?
112
+
113
+ log :debug1, "failed to retrieve risk point data with domain '#{dom}' and id '#{id}'"
114
+
115
+ halt *[ 404, {}, "risk-point-data domain with '#{dom}' and id '#{id}' does not exist" ]
116
+ elsif r = qr.failure_qualifier.reasons.lookup?('RZ0011')
117
+
118
+ log :debug1, "failed to retrieve risk point data with domain '#{dom}' and id '#{id}'"
119
+
120
+ halt *[ 404, {}, "#{r.message}: #{r.details}" ]
121
+ else
122
+
123
+ log :warning, "failed to retrieve risk point data with domain '#{dom}' and id '#{id}'"
124
+
125
+ halt *[ 500, {}, qr.failure_qualifier.reasons.join("\n") ]
126
+ end
127
+ end
128
+
129
+ r = qr.result.to_s
130
+
131
+ # return result
132
+
133
+ status 200
134
+
135
+ if false
136
+ elsif request.accept?('application/xml')
137
+
138
+ log :debug1, 'application/xml'
139
+
140
+ content_type 'application/xml'
141
+
142
+ r
143
+ elsif request.accept?('application/json')
144
+
145
+ log :debug1, 'application/json'
146
+
147
+ content_type 'application/json'
148
+
149
+ # TODO: this switch should be removed. This is a temporary fix
150
+ # for continuation of UI development only. This code is repeated
151
+ # in item_get.rb where it must also be removed.
152
+ unless ENV['RR_TOOL_WEBAPI_RISKPOINTDATA_DOMAINS_JSON_OUTPUT_FORMAT_SWITCH']
153
+
154
+ xml = ::Nokogiri::XML(r)
155
+ xml.xpath('//field').each { |e| e.name = e['id'] }
156
+ resp_hash = Hash.from_xml xml.to_xml
157
+
158
+ resp_json = resp_hash
159
+
160
+ JSON.generate resp_json
161
+ else
162
+
163
+ convert_XML_to_JSON r, { scheme: :gdata }
164
+ end
165
+ elsif request.accept?('text/xml')
166
+
167
+ log :debug1, 'text/xml'
168
+
169
+ content_type 'text/xml'
170
+
171
+ r
172
+ else
173
+
174
+ log :violation, "unexpected failure to match given 'Accept' header '#{request.accept}'"
175
+
176
+ status 500
177
+ end
178
+ end
179
+ end # class RiskPointGet
180
+
181
+ # ##########################################################################
182
+ # module
183
+
184
+ end # module Domains
185
+ end # module RiskPointData
186
+ end # module RouteVerbAdaptors
187
+ end # module Applications
188
+ end # module Cassini
189
+ end # module RazorRisk
190
+
191
+ # ############################## end of file ############################# #
192
+
193
+
@@ -0,0 +1,212 @@
1
+ # encoding: UTF-8
2
+
3
+ # ######################################################################## #
4
+ #
5
+ # Adaptor for RPD microservice's item GET verb
6
+ #
7
+ # Copyright (c) 2018 Razor Risk Technologies Pty Limited. All rights reserved.
8
+ #
9
+ # ######################################################################## #
10
+
11
+
12
+ # ##########################################################################
13
+ # requires
14
+
15
+ require 'razor_risk/cassini/applications/route_verb_adaptors/utilities/risk_point_data/domains'
16
+
17
+ require 'razor_risk/cassini/applications/rest_framework/verb_handler'
18
+ require 'razor_risk/cassini/util/conversion_util'
19
+
20
+ require 'razor_risk/razor/connectivity/razor_3/entity_connectors/risk_point_data/domains_connector'
21
+
22
+ require 'pantheios'
23
+
24
+ # ##########################################################################
25
+ # module
26
+
27
+ module RazorRisk
28
+ module Cassini
29
+ module Applications
30
+ module RouteVerbAdaptors
31
+ module RiskPointData
32
+ module Domains
33
+
34
+ # ##########################################################################
35
+ # classes
36
+
37
+ class HierarchyItemGet < RESTFramework::VerbHandler
38
+
39
+ include ::RazorRisk::Core::Diagnostics::Exceptions
40
+
41
+ include ::RazorRisk::Cassini::Applications::RouteVerbAdaptors::Utilities::RiskPointData::Domains
42
+ include ::RazorRisk::Cassini::Util::ConversionUtil
43
+
44
+ include ::RazorRisk::Razor::Connectivity::Razor3::EntityConnectors::RiskPointData
45
+
46
+ include ::Pantheios
47
+
48
+ HTTP_VERB = :get
49
+
50
+ HTTP_ACCEPTS = %w{ application/xml application/json text/xml }
51
+
52
+ ROUTE_VARIABLES = %w{
53
+
54
+ domain
55
+ id
56
+ }
57
+
58
+ QUERY_PARAMETERS = %w{
59
+
60
+ result-form
61
+ complete-tree
62
+ group-by
63
+ }
64
+
65
+ def handle env, params, request, response
66
+
67
+ trace ParamNames[ :env, :params, :request, :response ], env, params, request, response
68
+
69
+ # params
70
+
71
+ dom = infer_domain(params)
72
+ id = infer_id(params)
73
+
74
+ complete_tree = params['complete-tree'].to_s.strip
75
+ complete_tree = nil if complete_tree.empty?
76
+ halt 422, {}, "'complete-tree' may only be 't[rue]' or 'f[alse]'" unless complete_tree.nil? or complete_tree =~ /^t(rue)?$|^f(alse)?$/i
77
+ complete_tree = complete_tree =~ /^t(rue)?$/i ? true : false
78
+ group_by = params['group-by'].to_s.strip
79
+ group_by = nil if group_by.empty?
80
+
81
+ rf = case rf = (params['result-form'] || '').to_s
82
+ when 'body', ''
83
+ :body
84
+ when 'tree'
85
+ :tree
86
+ else
87
+ log(:failure) { "unrecognised result-form '#{rf}'" }
88
+ halt 422, {}, "unrecognised result-form '#{rf}'"
89
+ end
90
+
91
+ rq_opts = {
92
+ complete_tree: complete_tree,
93
+ group_by: group_by,
94
+ }
95
+
96
+ # get credentials
97
+
98
+ cr = get_required_credentials
99
+
100
+ # get requester
101
+
102
+ rr = settings.razor_requester
103
+
104
+ # get connector
105
+
106
+ ec = DomainsConnector.new(rr, credentials: cr, **rq_opts)
107
+
108
+ # issue request
109
+
110
+ begin
111
+
112
+ qr = ec.get_hierarchy dom, id, indicate_result_by: :qualified_result, result_form: rf
113
+
114
+ rescue ::ArgumentError, ::NameError, ::NoMethodError, ::TypeError => x
115
+
116
+ log(:violation) { "(#{x.class}): #{x.message}" }
117
+
118
+ raise
119
+ rescue RazorRequester::InvalidCredentialsException => x
120
+
121
+ log(:failure) { "exception (#{x.class}): #{x}" }
122
+
123
+ headers = {
124
+
125
+ 'Content-Type' => 'text/plain',
126
+ }
127
+
128
+ halt 401, headers, 'Invalid credentials'
129
+ rescue RRCSBaseException => x
130
+
131
+ log(:failure) { "exception (#{x.class}): #{x}" }
132
+
133
+ headers = {
134
+
135
+ 'Content-Type' => 'text/plain',
136
+ }
137
+
138
+ halt 500, headers, x.message
139
+ end
140
+
141
+ log(:debug1) { "qr(#{qr.class})='#{qr}'" }
142
+
143
+ unless qr.succeeded?
144
+
145
+ if false
146
+ elsif qr.result.nil?
147
+
148
+ log(:debug1) { "failed to retrieve risk point data tree with domain '#{dom}' and id '#{id}'" }
149
+
150
+ halt 404, {}, "risk-point-data domain '#{dom}' with id '#{id}' does not exist"
151
+ elsif r = qr.failure_qualifier.reasons.lookup?('RZ0011')
152
+
153
+ log(:debug1) { "failed to retrieve risk point data tree with domain '#{dom}' and id '#{id}'; #{r.message}: #{r.details}" }
154
+
155
+ halt 422, {}, "#{r.message}: #{r.details}"
156
+ else
157
+
158
+ log(:warning) { "failed to retrieve risk point data tree with domain '#{dom}' and id '#{id}'" }
159
+
160
+ halt 500, {}, qr.failure_qualifier.reasons.join("\n")
161
+ end
162
+ end
163
+
164
+ r = qr.result.to_s
165
+
166
+ # return result
167
+
168
+ status 200
169
+
170
+ if request.accept?('application/xml')
171
+
172
+ log(:debug1) { 'application/xml' }
173
+
174
+ content_type 'application/xml'
175
+
176
+ r
177
+ elsif request.accept?('text/xml')
178
+
179
+ log(:debug1) { 'text/xml' }
180
+
181
+ content_type 'text/xml'
182
+
183
+ r
184
+ elsif request.accept?('application/json')
185
+
186
+ log(:debug1) { 'application/json' }
187
+
188
+ content_type 'application/json'
189
+
190
+ convert_XML_to_JSON r, { scheme: :gdata }
191
+ else
192
+
193
+ log(:violation) { "unexpected failure to match given 'Accept' header '#{request.accept}'" }
194
+
195
+ status 500
196
+ end
197
+ end
198
+ end # class HierarchyItemGet
199
+
200
+ # ##########################################################################
201
+ # module
202
+
203
+ end # module Domains
204
+ end # module RiskPointData
205
+ end # module RouteVerbAdaptors
206
+ end # module Applications
207
+ end # module Cassini
208
+ end # module RazorRisk
209
+
210
+ # ############################## end of file ############################# #
211
+
212
+