| 1 | # ************************************************************************* | ||||||||||||||
| 2 | # Copyright (c) 2014-2015-2015, 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 | package Web::MREST::InitRouter; | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 22 | ▶ | use 5.012; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 22 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 22 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | 22 | ▶ | use App::CELL qw( $log $meta $site ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 22 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 22 | ▶ | use Path::Router; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | 22 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | |||||||||||||||
| 44 | |||||||||||||||
| 45 | =head1 NAME | ||||||||||||||
| 46 | |||||||||||||||
| 47 | Web::MREST::InitRouter - Routines for initializing our Path::Router instance | ||||||||||||||
| 48 | |||||||||||||||
| 49 | |||||||||||||||
| 50 | |||||||||||||||
| 51 | =head1 SYNOPSIS | ||||||||||||||
| 52 | |||||||||||||||
| 53 | L<Web::MREST> uses L<Path::Router> to match URIs to resources. All resources | ||||||||||||||
| 54 | are packed into a single object. The singleton is exported as C<$router> from | ||||||||||||||
| 55 | this module and can be initialized by calling C<init_router>, which is also | ||||||||||||||
| 56 | exported, with no arguments. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | use Web::MREST::InitRouter qw( $router ); | ||||||||||||||
| 59 | |||||||||||||||
| 60 | ... | ||||||||||||||
| 61 | |||||||||||||||
| 62 | Web::MREST::InitRouter::init_router() unless defined $router and $router->can( 'match' ); | ||||||||||||||
| 63 | |||||||||||||||
| 64 | |||||||||||||||
| 65 | |||||||||||||||
| 66 | |||||||||||||||
| 67 | =head1 PACKAGE VARIABLES | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =cut | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 22 | our $router; | |||||||||||||
| 72 | 22 | our $resources = {}; | |||||||||||||
| 73 | 22 | our @non_expandable_properties = qw( parent validations documentation resource_name children ); | |||||||||||||
| 74 | 22 | our %no_expand_map = map { ( $_ => '' ) } @non_expandable_properties; | |||||||||||||
| 75 | |||||||||||||||
| 76 | |||||||||||||||
| 77 | =head1 EXPORTS | ||||||||||||||
| 78 | |||||||||||||||
| 79 | This module provides the following exports: | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =over | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =item C<$router> (Path::Router singleton) | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =item C<$resources> (expanded resource definitions) | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =back | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =cut | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 22 | ▶ | use Exporter qw( import ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 92 | 22 | our @EXPORT_OK = qw( $router $resources ); | |||||||||||||
| 93 | |||||||||||||||
| 94 | |||||||||||||||
| 95 | =head1 FUNCTIONS | ||||||||||||||
| 96 | |||||||||||||||
| 97 | =cut | ||||||||||||||
| 98 | |||||||||||||||
| 99 | |||||||||||||||
| 100 | # | ||||||||||||||
| 101 | # read in multiple resource definitions from a hash | ||||||||||||||
| 102 | # | ||||||||||||||
| 103 | sub load_resource_defs { | ||||||||||||||
| 104 | 20 | ▶ | my $defs = shift; | ||||||||||||
|
Subroutine
load_resource_defs: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||
| 105 | #$log->debug("Entering " . __PACKAGE__. "::_load_resource_defs with argument " . Dumper( $defs )); | ||||||||||||||
| 106 | |||||||||||||||
| 107 | # first pass -> expand resource defs and add them to $resources | ||||||||||||||
| 108 | 20 | foreach my $resource ( keys( %$defs ) ) { | |||||||||||||
| 109 | # each resource definition is a hash. | ||||||||||||||
| 110 | 240 | ▶ | if ( ref( $defs->{$resource} ) eq 'HASH' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 240 | _process_resource_def( $resource, $defs->{$resource} ); | |||||||||||||
| 112 | } else { | ||||||||||||||
| 113 | 0 | die "AAAAAAAHHHHHHH! Definition of resource $resource is not a hashref!"; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | 240 | _add_route( $resource ); | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | |||||||||||||||
| 120 | # processes an individual resource definition hash and adds Path::Router route | ||||||||||||||
| 121 | # for it | ||||||||||||||
| 122 | sub _process_resource_def { | ||||||||||||||
| 123 | 240 | ▶ | my ( $resource, $resource_def ) = @_; | ||||||||||||
|
Subroutine
_process_resource_def: called CC=7 SCAR=19.5
| |||||||||||||||
| 124 | #$log->debug("Entering " . __PACKAGE__. "::_process_resource_def with:" ); | ||||||||||||||
| 125 | 240 | $log->info("Initializing \$resource ->$resource<-"); | |||||||||||||
| 126 | #$log->debug("\$resource_def " . Dumper( $resource_def ) ); | ||||||||||||||
| 127 | |||||||||||||||
| 128 | # expand all properties except those in %no_expand_map | ||||||||||||||
| 129 | 240 | foreach my $prop ( keys %$resource_def ) { | |||||||||||||
| 130 | 1160 | ▶ | next if exists $no_expand_map{ $prop }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 700 | _expand_property( $resource, $resource_def, $prop ); | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | # handle non-expandable properties | ||||||||||||||
| 135 | # | ||||||||||||||
| 136 | # - validations | ||||||||||||||
| 137 | 240 | my $validations = $resource_def->{'validations'}; | |||||||||||||
| 138 | 240 | ▶ | $resources->{$resource}->{'validations'} = $validations if $resource_def->{'validations'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | # | ||||||||||||||
| 140 | # - documentation | ||||||||||||||
| 141 | 240 | my $documentation = $resource_def->{'documentation'}; | |||||||||||||
| 142 | 240 | ▶ | $resources->{$resource}->{'documentation'} = $documentation if $resource_def->{'documentation'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | # | ||||||||||||||
| 144 | # - parent | ||||||||||||||
| 145 | 240 | ▶ | if ( $resource ne '/' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | 220 | ▶ | my $parent = $resource_def->{'parent'} || '/'; | ||||||||||||
|
Condition50%1/2
$resource_def->{'parent'} || '/'
MC/DC0%0/1
$resource_def->{'parent'} || '/'
$resource_def->{'parent'}
Truth table50%1/2
A
A= $resource_def->{'parent'}
| |||||||||||||||
| 147 | 220 | push( @{ $resources->{$parent}->{'children'} }, $resource ); | |||||||||||||
| 148 | 220 | $resources->{$resource}->{'parent'} = $parent; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 240 | return; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | |||||||||||||||
| 154 | |||||||||||||||
| 155 | sub _add_route { | ||||||||||||||
| 156 | 240 | ▶ | my $resource = shift; | ||||||||||||
|
Subroutine
_add_route: called CC=3 SCAR=11.0
| |||||||||||||||
| 157 | 240 | my %validations; | |||||||||||||
| 158 | 240 | ▶ | if ( ref( $resources->{$resource}->{'validations'} ) eq 'HASH' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 20 | %validations = %{ $resources->{$resource}->{'validations'} }; | |||||||||||||
| 160 | 20 | delete $resources->{$resource}->{'validations'}; | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | my $ARGS = { | ||||||||||||||
| 163 | 240 | target => $resources->{$resource}, | |||||||||||||
| 164 | }; | ||||||||||||||
| 165 | 240 | ▶ | $ARGS->{'validations'} = \%validations if %validations; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | |||||||||||||||
| 167 | try { | ||||||||||||||
| 168 | 240 | ▶ | $router->add_route( $resource, %$ARGS ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 169 | } catch { | ||||||||||||||
| 170 | 0 | ▶ | $log->crit( $_ ); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 171 | 240 | }; | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | |||||||||||||||
| 174 | |||||||||||||||
| 175 | # takes an individual resource definition property, expands it and puts it in | ||||||||||||||
| 176 | # $resources package variable | ||||||||||||||
| 177 | sub _expand_property { | ||||||||||||||
| 178 | 700 | ▶ | my ( $resource, $resource_def, $prop ) = @_; | ||||||||||||
|
Subroutine
_expand_property: called CC=9 SCAR=23.1
| |||||||||||||||
| 179 | #$log->debug("Entering " . __PACKAGE__. "::_expand_property with " . | ||||||||||||||
| 180 | # "resource \"$resource\" and property \"$prop\"" ); | ||||||||||||||
| 181 | |||||||||||||||
| 182 | # set the resource_name property | ||||||||||||||
| 183 | 700 | $resources->{$resource}->{'resource_name'} = $resource; | |||||||||||||
| 184 | |||||||||||||||
| 185 | my @supported_methods = ( ref( $resource_def->{'handler'} ) eq 'HASH' ) | ||||||||||||||
| 186 | 480 | ? keys( %{ $resource_def->{'handler'} } ) | |||||||||||||
| 187 | 700 | ▶ | : @{ $site->MREST_SUPPORTED_HTTP_METHODS || [ qw( GET POST PUT DELETE ) ] }; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 188 | 700 | foreach my $method ( @supported_methods ) { | |||||||||||||
| 189 | #$log->debug( "Considering the \"$method\" method" ); | ||||||||||||||
| 190 | 2140 | ▶ | if ( exists $resource_def->{$prop} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 191 | 2140 | my $prop_def = $resource_def->{$prop}; | |||||||||||||
| 192 | 2140 | ▶ | my $refv = ref( $prop_def ) || 'SCALAR'; | ||||||||||||
|
Condition100%2/2
ref $prop_def || 'SCALAR'
MC/DC100%1/1
ref $prop_def || 'SCALAR'
ref $prop_def
Truth table100%2/2
A
A= ref $prop_def
| |||||||||||||||
| 193 | #$log->debug( "The definition of this property is a $refv" ); | ||||||||||||||
| 194 | 2140 | ▶ | if ( $refv eq 'HASH' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 195 | 320 | ▶ | if ( $prop_def->{$method} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 196 | 320 | $resources->{$resource}->{$method}->{$prop} = $prop_def->{$method}; | |||||||||||||
| 197 | } else { | ||||||||||||||
| 198 | 0 | $log->crit( "No $prop defined for $method method in $resource!" ); | |||||||||||||
| 199 | } | ||||||||||||||
| 200 | } elsif ( $refv eq 'SCALAR' ) { | ||||||||||||||
| 201 | 1820 | $resources->{$resource}->{$method}->{$prop} = $prop_def; | |||||||||||||
| 202 | } else { | ||||||||||||||
| 203 | 0 | die "AAAAAGAAAGAAAAAA! in " . __FILE__ . ", _populate_resources"; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | } else { | ||||||||||||||
| 206 | # resource with no def_part: suspicious | ||||||||||||||
| 207 | 0 | $log->notice( "While walking resource definition tree, " . | |||||||||||||
| 208 | "encountered resource $resource with missing $prop in its definition" ); | ||||||||||||||
| 209 | } | ||||||||||||||
| 210 | } | ||||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | 22 | 1; | |||||||||||||