Class: RazorRisk::Cassini::Applications::RouteVerbAdaptors::RiskPointData::Domains::CollectionGet

Inherits:
RESTFramework::VerbHandler
  • Object
show all
Includes:
Pantheios, Utilities::CollectionGetHelper, Utilities::RiskPointData::Domains, Util::ConversionUtil, RazorRisk::Core::Diagnostics::Logger, Razor::Connectivity::Razor3::EntityConnectors::RiskPointData, Razor::Connectivity::Search::Parsers
Defined in:
lib/razor_risk/cassini/applications/route_verb_adaptors/risk_point_data/domains/collection_get.rb

Overview

########################################################################## classes

Constant Summary collapse

HTTP_VERB =
:get
HTTP_ACCEPTS =
%w{ application/xml application/json text/xml }
ROUTE_VARIABLES =
%w{

    domain
}
QUERY_PARAMETERS =
%w{

    field-search

    filter-name

    page-base
    page-extent

    sort-field
    sort-direction
    isModified
    oper-type
}

Instance Method Summary collapse

Methods included from Utilities::RiskPointData::Domains

#infer_domain, #infer_id, #infer_route_element

Instance Method Details

#handle(env, params, request, response) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/risk_point_data/domains/collection_get.rb', line 88

def handle env, params, request, response

    trace ParamNames[ :env, :params, :request, :response ], env, params, request, response

    # params


    domain      =   infer_domain(params)

    validate_exclusive_search_parameters params, 'filter-name', 'field-search', message_scope: 'invalid search specifier'

    field_srch  =   params['field-search'].to_s.strip
    field_srch  =   nil if field_srch.empty?
    filter_name =   params['filter-name'].to_s.strip
    filter_name =   nil if filter_name.empty?
    page_base   =   ::Xqsr3::Conversion::IntegerParser.to_integer(params['page-base'] || 0) { |x, arg| halt 422, {}, "invalid page specifier: 'page-base'=#{arg}" }
    page_extent =   ::Xqsr3::Conversion::IntegerParser.to_integer(params['page-extent'] || 1000) { |x, arg| halt 422, {}, "invalid page specifier: 'page-extent'=#{arg}" }
    sort_field  =   params['sort-field'].to_s.strip
    sort_field  =   nil if sort_field.empty?
    sort_direction  =   params['sort-direction'].to_s.strip
    sort_direction  =   nil if sort_direction.empty?
    isModified  =   params['isModified']
    isModified ? isModified = true : isModified = false
    operType  =   params['oper-type']
    operType  =   operType === "OR"? "OR" : "AND"

    if page_base < 0 || page_extent < 1

        halt 416, {}, "invalid page specifier: 'page-base'=#{page_base}; 'page-extent'=#{page_extent}"
    end

    unless sort_direction.nil? or sort_direction =~ /^ascending|descending$/

        halt 400, {'Content-Type' => 'text/plain'}, "invalid sort direction, must be 'ascending' or 'descending'; was #{sort_direction}"
    end

    if field_srch
        begin
            cp = MultiValueConjunctionParser.new
            field_srch = cp.parse_conjunction_list field_srch
        rescue MultiValueConjunctionParser::SyntaxError => x
            halt 422, {'Content-Type' => 'text/plain'}, x.message
        end
    end

    rq_opts     =   {

        :field_search => field_srch,
        :filter_name => filter_name,
        :page_base => page_base,
        :page_extent => page_extent,
        :sort_field => sort_field,
        :sort_direction => sort_direction,
        :operType => operType,
    }

    # get credentials


    cr  =   get_required_credentials

    # get requester


    rr  =   settings.razor_requester

    # get connector


    ec  =   DomainsConnector.new(rr, credentials: cr, **rq_opts)

    # issue request


    qr  =   ec.get_records domain, indicate_result_by: :qualified_result, result_form: :body

    log :debug1, "qr(#{qr.class})='#{qr}'"

    unless qr.succeeded?

        if false

            ;
        elsif :no_such_filter == qr.result

            log :debug1, "failed to retrieve risk point data for domain '#{domain}': no such filter '#{filter_name}'"

            halt 422, {}, "no such filter '#{filter_name}'"
        elsif r = qr.failure_qualifier.reasons.lookup?('RZ0011')

            log :debug1, "failed to retrieve risk point data for domain '#{domain}'"

            halt 404, {}, "#{r.message}: #{r.details}"
        else

            log :warning, "failed to retrieve risk point data for domain '#{domain}'"

            halt 500, {}, qr.failure_qualifier.reasons.join("\n")
        end
    end

    r   =   qr.result.to_s

    # return result


    status 200

    if false
    elsif request.accept?('application/xml')

        log :debug1, 'application/xml'
        content_type 'application/xml'
        r
    elsif request.accept?('application/json')

        log :debug1, 'application/json'

        content_type 'application/json'

        # TODO: this switch should be removed. This is a temporary fix

        # for continuation of UI development only. This code is repeated

        # in item_get.rb where it must also be removed.

        unless ENV['RR_TOOL_WEBAPI_RISKPOINTDATA_DOMAINS_JSON_OUTPUT_FORMAT_SWITCH']

            xml = ::Nokogiri::XML(r)

            if isModified
    xml.xpath('//field').each do |e|
                    if e['id']
                        e.name = e['id']
                        e['text'] = e.text
                        e.content = ""
                    end
                end
                resp_hash   =   XmlHasher.parse(xml.to_xml)
            else
                xml.xpath('//field').each { |e| e.name = e['id'] }
                resp_hash   =   Hash.from_xml xml.to_xml
            end
            
            resp_json   =   resp_hash

            JSON.generate resp_json
        else

            convert_XML_to_JSON r, { scheme: :gdata }
        end
    elsif request.accept?('text/xml')

        log :debug1, 'text/xml'

        content_type 'text/xml'

        r
    else

        log :violation, "unexpected failure to match given 'Accept' header '#{request.accept}'"

        status 500
    end
end