| 1 | # ************************************************************************* | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2014-2022, SUSE LLC | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # All rights reserved. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Redistribution and use in source and binary forms, with or without | ||||||||||||||||||||||||||||
| 7 | # modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||||||||||||||||||||||||||||
| 10 | # this list of conditions and the following disclaimer. | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||||||||||||||||||||||||||||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||||||||||||||||||||||||||||
| 14 | # documentation and/or other materials provided with the distribution. | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # 3. Neither the name of SUSE LLC nor the names of its contributors may be | ||||||||||||||||||||||||||||
| 17 | # used to endorse or promote products derived from this software without | ||||||||||||||||||||||||||||
| 18 | # specific prior written permission. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||||||||||||||||||||||||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||||||||||||||||||||||||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||||||||||||||||||||||||||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||||||||||||||||||||||||||||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||||||||||||||||||||||||||||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||||||||||||||||||||||||||||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||||||||||||||||||||||||||||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||||||||||||||||||||||||||||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||||||||||||||||||||||||||||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||||||||||||||||||||||||||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 31 | # ************************************************************************* | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # ------------------------ | ||||||||||||||||||||||||||||
| 34 | # This package contains methods for dealing with request and response | ||||||||||||||||||||||||||||
| 35 | # entities (parts two and four of the FSM as described in the L<Web::MREST> | ||||||||||||||||||||||||||||
| 36 | # documentation | ||||||||||||||||||||||||||||
| 37 | # ------------------------ | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | package Web::MREST::Entity; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 21 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 21 | ▶ | use App::CELL qw( $CELL $log $meta $site ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 21 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 21 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 21 | ▶ | use Web::Machine::FSM::States; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 21 | ▶ | use Web::MREST::Util qw( $JSON ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 21 | ▶ | use parent 'Web::MREST::Resource'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 NAME | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Web::MREST::Entity - Methods for dealing with request, response entities | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Methods for dealing with request, response entities | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 METHODS | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 get_acceptable_content_type_handler | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | The method to use to process the request entity (i.e, the "acceptable content | ||||||||||||||||||||||||||||
| 75 | type handler") is set in content_types_accepted. Web::Machine only calls the | ||||||||||||||||||||||||||||
| 76 | method on PUT requests and those POST requests for which post_is_create is | ||||||||||||||||||||||||||||
| 77 | true. On POST requests where post_is_create is false, we have to call it | ||||||||||||||||||||||||||||
| 78 | ourselves, and for that we need a way to get to it. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 9 | ▶ | sub get_acceptable_content_type_handler { | ||||||||||||||||||||||||||
|
Subroutine
get_acceptable_content_type_handler: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 9 | Web::Machine::FSM::States::_get_acceptable_content_type_handler( $self, $self->request ); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 content_types_provided | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | L<Web::Machine> calls this routine to determine how to generate the response | ||||||||||||||||||||||||||||
| 91 | body GET requests. (It is not called for PUT, POST, or DELETE requests.) | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | The return value has the following format: | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | [ | ||||||||||||||||||||||||||||
| 96 | { 'text/html' => 'method_for_html' }, | ||||||||||||||||||||||||||||
| 97 | { 'application/json' => 'method_for_json' }, | ||||||||||||||||||||||||||||
| 98 | { 'other/mime' => 'method_for_other_mime' }, | ||||||||||||||||||||||||||||
| 99 | ] | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | As you can see, this is a list of tuples. The key is a media type and the | ||||||||||||||||||||||||||||
| 102 | value is the name of a method. The first tuple is taken as the default. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 100 | ▶ | sub content_types_provided { | ||||||||||||||||||||||||||
|
Subroutine
content_types_provided: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 100 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 100 | my @caller = caller; | |||||||||||||||||||||||||||
| 109 | 100 | $log->debug( "Entering " . __PACKAGE__ . "::content_types_provided, caller is " . Dumper( \@caller ) ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | return [ | ||||||||||||||||||||||||||||
| 112 | 100 | { 'text/html' => 'mrest_generate_response_html' }, | |||||||||||||||||||||||||||
| 113 | { 'application/json' => 'mrest_generate_response_json' }, | ||||||||||||||||||||||||||||
| 114 | ]; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 mrest_generate_response_html | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Normally, clients will communicate with the server via | ||||||||||||||||||||||||||||
| 121 | '_render_response_json', but humans need HTML. This method takes the | ||||||||||||||||||||||||||||
| 122 | server's JSON response and wraps it up in a nice package. | ||||||||||||||||||||||||||||
| 123 | The return value from this method becomes the response entity. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | sub mrest_generate_response_html { | ||||||||||||||||||||||||||
|
Subroutine
mrest_generate_response_html: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 129 | 1 | $log->debug( "Entering " . __PACKAGE__ . "::_render_response_html (response generator)" ); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | my $json = $self->mrest_generate_response_json; | |||||||||||||||||||||||||||
| 132 | 1 | ▶ | return $json if ref( $json ) eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | my $msgobj = $CELL->msg( | |||||||||||||||||||||||||||
| 135 | 'MREST_RESPONSE_HTML', | ||||||||||||||||||||||||||||
| 136 | $site->MREST_APPLICATION_MODULE, | ||||||||||||||||||||||||||||
| 137 | $json, | ||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||
| 139 | 1 | ▶ | my $entity = $msgobj | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | ? $msgobj->text | ||||||||||||||||||||||||||||
| 141 | : '<html><body><h1>Internal Error</h1><p>See Resource.pm->_render_response_html</p></body></html>'; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | $self->response->header('Content-Type' => 'text/html' ); | |||||||||||||||||||||||||||
| 144 | 1 | $self->response->content( $entity ); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | return $entity; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 content_types_accepted | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | L<Web::Machine> calls this routine to determine how to handle the request | ||||||||||||||||||||||||||||
| 153 | body (e.g. in PUT requests). | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 13 | ▶ | sub content_types_accepted { | ||||||||||||||||||||||||||
|
Subroutine
content_types_accepted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 159 | 13 | my @caller = caller; | |||||||||||||||||||||||||||
| 160 | 13 | $log->debug("Entering " . __PACKAGE__ . "::content_types_accepted, caller is " . Dumper( \@caller ) ); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 13 | return [ { 'application/json' => 'mrest_process_request_json' }, ] | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 mrest_process_request_json | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | PUT and POST requests may contain a request body. This is the "handler | ||||||||||||||||||||||||||||
| 169 | function" where we process those requests. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | We associate this function with 'application/json' via | ||||||||||||||||||||||||||||
| 172 | C<content_types_accepted>. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 13 | ▶ | sub mrest_process_request_json { | ||||||||||||||||||||||||||
|
Subroutine
mrest_process_request_json: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 178 | 13 | my @caller = caller; | |||||||||||||||||||||||||||
| 179 | 13 | $log->debug("Entering " . __PACKAGE__ . "::mrest_process_request_json, caller is " . Dumper( \@caller ) ); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # convert body to JSON | ||||||||||||||||||||||||||||
| 182 | 13 | my ( $from_json, $status ); | |||||||||||||||||||||||||||
| 183 | 13 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 184 | 13 | my $content = $self->request->content; | |||||||||||||||||||||||||||
| 185 | 13 | ▶ | if ( ! defined $content or $content eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $content or $content eq ""
MC/DC50%1/2
not defined $content or $content eq ""
not defined $content
$content eq ""
Truth table66%2/3
not(A) or B
A= not defined $contentB= $content eq ""
| |||||||||||||||||||||||||||||
| 186 | 4 | $log->debug( "There is no request body, assuming JSON null" ); | |||||||||||||||||||||||||||
| 187 | 4 | $content = 'null'; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 13 | $log->debug( "Attempting to decode JSON request entity $content" ); | |||||||||||||||||||||||||||
| 190 | 13 | $from_json = $JSON->decode( $content ); | |||||||||||||||||||||||||||
| 191 | 12 | $log->debug( "Success" ); | |||||||||||||||||||||||||||
| 192 | 1 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | 1 | $status = \400; | |||||||||||||||||||||||||||
| 194 | 1 | $log->error( "Caught JSON decode error; response code should be " . $$status ); | |||||||||||||||||||||||||||
| 195 | 1 | $self->mrest_declare_status( 'code' => $$status, explanation => $_ ); | |||||||||||||||||||||||||||
| 196 | 13 | }; | |||||||||||||||||||||||||||
| 197 | 13 | ▶ | return $status if ref( $status ) eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 12 | $self->push_onto_context( { 'request_entity' => $from_json } ); | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 12 | return $self->mrest_generate_response; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 mrest_process_request | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Used to call the request handler manually in cases when L<Web::Machine> does | ||||||||||||||||||||||||||||
| 207 | not call it for us. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 9 | ▶ | sub mrest_process_request { | ||||||||||||||||||||||||||
|
Subroutine
mrest_process_request: called CC=2 SCAR=7.5
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 213 | 9 | $log->debug( "Entering " . __PACKAGE__ . "::mrest_process_request" ); | |||||||||||||||||||||||||||
| 214 | 9 | my $handler = $self->get_acceptable_content_type_handler; | |||||||||||||||||||||||||||
| 215 | 9 | ▶ | if ( ref( $handler ) eq 'SCALAR' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | $self->mrest_declare_status( code => $$handler, | |||||||||||||||||||||||||||
| 217 | explanation => 'Could not get acceptable content type handler' ); | ||||||||||||||||||||||||||||
| 218 | 0 | return $CELL->status_not_ok; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 9 | $log->debug( "acceptable request handler is: " . Dumper( $handler ) ); | |||||||||||||||||||||||||||
| 221 | 9 | return $self->$handler; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head2 mrest_generate_response_json | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | First, run pass 2 of the resource handler, which is expected to return an | ||||||||||||||||||||||||||||
| 228 | App::CELL::Status object. Second, push that object onto the context. Third, | ||||||||||||||||||||||||||||
| 229 | convert that object into JSON and push the JSON onto the context, too. Return | ||||||||||||||||||||||||||||
| 230 | the JSON representation of the App::CELL::Status object - this becomes the | ||||||||||||||||||||||||||||
| 231 | HTTP response entity. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 34 | ▶ | sub mrest_generate_response_json { | ||||||||||||||||||||||||||
|
Subroutine
mrest_generate_response_json: called CC=4 SCAR=15.1
Pod
documented
| |||||||||||||||||||||||||||||
| 236 | 34 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 237 | 34 | my ( $d, %h, $before, $after, $after_utf8 ); | |||||||||||||||||||||||||||
| 238 | 34 | my @caller = caller; | |||||||||||||||||||||||||||
| 239 | 34 | $log->debug( "Entering " . __PACKAGE__ . "::mrest_generate_response_json, caller is " . | |||||||||||||||||||||||||||
| 240 | Dumper( \@caller ) ); | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # run the handler | ||||||||||||||||||||||||||||
| 243 | 34 | my $handler = $self->context->{'handler'}; # WWWW | |||||||||||||||||||||||||||
| 244 | 34 | $log->debug( "mrest_generate_response_json: Calling resource handler $handler for pass two" ); | |||||||||||||||||||||||||||
| 245 | 34 | my ( $status, $response_obj, $entity ); | |||||||||||||||||||||||||||
| 246 | 34 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=17.3
| |||||||||||||||||||||||||||||
| 247 | 34 | $status = $self->$handler(2); | |||||||||||||||||||||||||||
| 248 | 34 | ▶ | if ( ( my $reftype = ref( $status ) ) ne 'App::CELL::Status' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 0 | die "AAAAHAGGHG! Handler $handler, pass two, returned a ->$reftype<-, " . | |||||||||||||||||||||||||||
| 250 | "which is not an App::CELL::Status object!"; | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | 34 | ▶ | if ( $status->not_ok and ! $self->status_declared ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$status->not_ok and not $self->status_declared
MC/DC0%0/2
$status->not_ok and not $self->status_declared
$status->not_ok
not $self->status_declared
Truth table33%1/3
A and not(B)
A= $status->not_okB= not $self->status_declared
| |||||||||||||||||||||||||||||
| 253 | 0 | $status->{'http_code'} = 500; | |||||||||||||||||||||||||||
| 254 | 0 | $self->mrest_declare_status( $status ); | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | 34 | $response_obj = $status->expurgate; | |||||||||||||||||||||||||||
| 257 | 34 | $entity = $JSON->encode( $response_obj ); | |||||||||||||||||||||||||||
| 258 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | if ( ! $self->status_declared ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 0 | $self->mrest_declare_status( code => 500, explanation => $_ ); | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | 0 | my $code = $self->mrest_declared_status_code; | |||||||||||||||||||||||||||
| 263 | 0 | $code += 0; | |||||||||||||||||||||||||||
| 264 | 0 | $status = \$code; | |||||||||||||||||||||||||||
| 265 | 34 | }; | |||||||||||||||||||||||||||
| 266 | 34 | $log->debug( "response generator returned " . Dumper( $status ) ); | |||||||||||||||||||||||||||
| 267 | 34 | ▶ | return $status if ref( $status ) eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | # for PUT requests, we need a Location header if a new resource was created | ||||||||||||||||||||||||||||
| 270 | 34 | ▶ | if ( $self->context->{'method'} eq 'PUT' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 3 | my $headers = $self->response->headers; | |||||||||||||||||||||||||||
| 272 | 3 | my $uri_path = $self->context->{'uri_path'}; | |||||||||||||||||||||||||||
| 273 | 3 | ▶ | $headers->header( 'Location' => $uri_path ) unless $self->context->{'resource_exists'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # stage the status object to become the response entity | ||||||||||||||||||||||||||||
| 277 | $self->push_onto_context( { | ||||||||||||||||||||||||||||
| 278 | 34 | 'handler_status' => $status, | |||||||||||||||||||||||||||
| 279 | 'response_object' => $response_obj, | ||||||||||||||||||||||||||||
| 280 | 'response_entity' => $entity, | ||||||||||||||||||||||||||||
| 281 | } ); | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # put the entity into the response | ||||||||||||||||||||||||||||
| 284 | 34 | $self->response->header('Content-Type' => 'application/json' ); | |||||||||||||||||||||||||||
| 285 | 34 | $self->response->content( $entity ); | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 34 | $log->debug( "Response will be: " . $self->response->content ); | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 34 | return $entity; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =head2 mrest_generate_response | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | This should somehow get the response handler and run it. | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =cut | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 13 | ▶ | sub mrest_generate_response { | ||||||||||||||||||||||||||
|
Subroutine
mrest_generate_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 300 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 301 | 13 | return $self->mrest_generate_response_json; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 21 | 1; | |||||||||||||||||||||||||||