Module: RazorRisk::Cassini::Applications::RouteVerbAdaptors::Capabilities::GetHandlerMixin

Includes:
Pantheios, Util::ConversionUtil, RazorRisk::Core::Diagnostics::Logger, Razor::Connectivity::EntityConnectors::Exceptions, Razor::Connectivity::Razor3::EntityConnectors
Included in:
AddGroup, AddSubjectGroup, AddUser, BatchSummariesGet, CreateNewUser, DataSubjectGet, DataSubjectsGet, FiltersGet, GroupGet, GroupUsersGet, GroupsGet, ProcessedlogGet, SubjectGet, SubjectsGet, UpdateGroupPost, UserGet, UsersGet
Defined in:
lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/get_handler_mixin.rb

Overview

Note:

Methods in this module should not be used directly, only as methods

This ia a mixin module that provides the implementation for handling a get capabilities request.

on handler classes.

Constant Summary collapse

HTTP_VERB =
:get
HTTP_ACCEPTS =
%w{
    application/xml
    application/json
    text/xml
}
QUERY_PARAMETERS =
%w{
}
ErrorHeaders =
{
    'Content-Type' => 'text/plain',
}

Instance Method Summary collapse

Instance Method Details

#handle(env, params, request, response) {|ec| ... } ⇒ Object

Handles a GET capabilities request.

Parameters:

  • env (::Hash)

    The Rack request environment (@see Rack::Request#env).

  • params (::Hash)

    Validated query parameters (@see ValidateQueryParametersMixin#validate_query_parameters)

  • request (::Sinatra::Request)

    The request to be handled.

  • response (::Sinatra::Response)

    The response object that will be returned.

Yields:

  • (ec)

    Gives the entity connector to be used to perform the Razor Request.

Yield Parameters:

  • The (CapabilitiesConnector)

    entity connector.

Yield Returns:

  • (RazorRisk::Core::QualifiedResult)

    The qulified result of the request.



81
82
83
84
85
86
87
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
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/get_handler_mixin.rb', line 81

def handle env, params, request, response, &block

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

    request_options = settings.request_options

    cr = get_required_credentials
    rr = settings.razor_requester
    ec = CapabilitiesConnector.new(
        rr,
        credentials: cr,
        **request_options
    )

    # Send reqest
    begin
        qr = yield(ec)
    rescue ::ArgumentError, ::NameError, ::NoMethodError, ::TypeError => x
        log :failure, "exception (#{x.class}): #{x}"
        raise
    rescue UnexpectedServerResponseException => x
        log :failure, "exception (#{x.class}): #{x}"
        halt 500, ErrorHeaders, "Server Error"
    end

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

    r = qr.result

    # check result
    unless qr.succeeded?

        if qr.failure_qualifier.reasons.lookup?('RZ0011')
            r       = qr.failure_qualifier.reasons.lookup?('RZ0011')
            reasons = qr.failure_qualifier.reasons.join("\n")
            log :debug1, "failed to retrieve capabilities; #{reasons}"
            halt 404, ErrorHeaders, "#{r.message}: #{r.details}"
        elsif !qr.failure_qualifier.reasons.empty?
            reasons = qr.failure_qualifier.reasons.join("\n")
            log :warning, "Failed to retrieve capabilities; #{reasons}"
            halt 500, ErrorHeaders, 'Server Error'
        elsif r.nil?
            log :debug1, 'failed to retrieve capabilities'
            halt 404, ErrorHeaders, "Capabilities not found"
        else
            log :warning, "Failed to retrieve capabilities for an unknown reason #{r}"
            halt 500, ErrorHeaders, 'Server Error'
        end
    end

    # return result
    status 200
    if request.accept?('text/xml')
        log :debug1, 'text/xml'
        content_type 'text/xml'
        r.to_s
    elsif request.accept?('application/xml')
        log :debug1, 'application/xml'
        content_type 'application/xml'
        r.to_s
    elsif request.accept?('application/json')
        log :debug1, 'application/json'
        content_type 'application/json'
        convert_XML_to_JSON r, { scheme: :gdata }
    else
        log :violation, "unexpected failure to match given 'Accept' header '#{request.accept}'"
        halt 500, ErrorHeaders, 'No supported Accept types'
    end
end