| 1 | package Catalyst::ActionRole::ProvidesMedia; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Plack::MIME; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | requires 'dispatch'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | has 'media_actions' => ( | |||||||||||||||||||||||||||
| 9 | is=>'ro', | ||||||||||||||||||||||||||||
| 10 | required=>1, | ||||||||||||||||||||||||||||
| 11 | lazy=>1, | ||||||||||||||||||||||||||||
| 12 | builder=>'_build_media_actions'); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub _build_media_actions { | ||||||||||||||||||||||||||
|
Subroutine
_build_media_actions: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 15 | 1 | my $name = (my $self = shift)->name; | |||||||||||||||||||||||||||
| 16 | 1 | my %media_actions = (); | |||||||||||||||||||||||||||
| 17 | 1 | foreach my $action($self->class->meta->get_all_method_names) { | |||||||||||||||||||||||||||
| 18 | 100 | my ($media_proto) = ($action=~m/^$name\_(.+)$/); | |||||||||||||||||||||||||||
| 19 | 100 | ▶ | next unless $media_proto; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | my $media = Plack::MIME->mime_type('.' . lc($media_proto)) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
'Plack::MIME'->mime_type('.' . lc($media_proto)) || ($media_proto eq 'no_match' ? $media_proto : die("$media_proto not a MIME type"))
MC/DC0%0/2
'Plack::MIME'->mime_type('.' . lc($media_proto)) || ($media_proto eq 'no_match' ? $media_proto : die("$media_proto not a MIME type"))
'Plack::MIME'->mime_type('.' . lc($media_proto))
($media_proto eq 'no_match' ? $media_proto : die("$media_proto not a MIME type"))
Truth table66%2/3
A || B
A= 'Plack::MIME'->mime_type('.' . lc($media_proto))B= ($media_proto eq 'no_match' ? $media_proto : die("$media_proto not a MIME type"))
| |||||||||||||||||||||||||||||
| 21 | ( $media_proto eq 'no_match' ? $media_proto : die "$media_proto not a MIME type" ); | ||||||||||||||||||||||||||||
| 22 | 3 | $media_actions{$media} = $action; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | return \%media_actions; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | sub forwards { | ||||||||||||||||||||||||||
|
Subroutine
forwards: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 3 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | Scalar::Util::weaken($ctx); | |||||||||||||||||||||||||||
| 32 | 3 | my %media_actions = %{$self->media_actions}; | |||||||||||||||||||||||||||
| 33 | 3 | ▶ | unless(exists $media_actions{no_match}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | $media_actions{no_match} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | 0 | my ($req, %callbacks) = @_; | |||||||||||||||||||||||||||
| 36 | 0 | $ctx->res->status(406); | |||||||||||||||||||||||||||
| 37 | 0 | $ctx->res->content_type('text/plain'); | |||||||||||||||||||||||||||
| 38 | 0 | my $allowed = join(',',(keys %callbacks)); | |||||||||||||||||||||||||||
| 39 | 0 | $ctx->res->body("You requested a media type we don't support. Acceptable types are: $allowed"); | |||||||||||||||||||||||||||
| 40 | 0 | }; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my @forwards = map { | ||||||||||||||||||||||||||||
| 44 | 3 | my $action = $media_actions{$_}; | |||||||||||||||||||||||||||
| 45 | $_ => $_ eq 'no_match' ? | ||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub { $ctx->forward($action, \%media_actions) } : | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 9 | ▶ | sub { $ctx->forward($action) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } (keys %media_actions); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 3 | return @forwards; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | around 'dispatch', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 3 | my ($orig, $self, $ctx, @rest) = @_; | |||||||||||||||||||||||||||
| 55 | 3 | my $return = $self->$orig($ctx, @rest); | |||||||||||||||||||||||||||
| 56 | 3 | $ctx->req->on_best_media_type($self->forwards($ctx)); | |||||||||||||||||||||||||||
| 57 | 3 | return $return; | |||||||||||||||||||||||||||
| 58 | 1 | }; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | 1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 NAME | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Catalyst::ActionRole::ProvidesMedia - Delegate actions based on best mediatype match | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | package MyApp; | ||||||||||||||||||||||||||||
| 69 | use Catalyst; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | MyApp->request_class_traits(['Catalyst::TraitFor::Request::ContentNegotiationHelpers']); | ||||||||||||||||||||||||||||
| 72 | MyApp->setup; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | package MyApp::Controller::Example; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | use Moose; | ||||||||||||||||||||||||||||
| 77 | use MooseX::MethodAttributes; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | extends 'Catalyst::Controller'; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub myaction :Chained(/) Does('ProvidesMedia') CaptureArgs(0) { | ||||||||||||||||||||||||||||
| 82 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Executed if the request accept header like prefers and accepts | ||||||||||||||||||||||||||||
| 86 | # type 'application/json'. | ||||||||||||||||||||||||||||
| 87 | sub myaction_JSON :Action { } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Executed if the request accept header like prefers and accepts | ||||||||||||||||||||||||||||
| 90 | # type 'text/html'. | ||||||||||||||||||||||||||||
| 91 | sub myaction_HTML :Action { } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Executed if none of the above types are accepted by the incoming | ||||||||||||||||||||||||||||
| 94 | # request accept header. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub myaction_no_match :Action { | ||||||||||||||||||||||||||||
| 97 | my ($self, $c, $matches) = @_; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # There's a sane default for this, but you can override as needed. | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub next_action_in_chain_1 :Chained(myaction) Args(0) { ... } | ||||||||||||||||||||||||||||
| 103 | sub next_action_in_chain_2 :Chained(myaction) Args(0) { ... } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | In server side content negotiation you seek the provide response data based on | ||||||||||||||||||||||||||||
| 110 | what a client says it can accept (typically ranked in order of what is most | ||||||||||||||||||||||||||||
| 111 | acceptable to which is least.) This way a client can tell the server what type | ||||||||||||||||||||||||||||
| 112 | of representation of a resource it knows how to handle. A Server can either | ||||||||||||||||||||||||||||
| 113 | provide that represention or return an error state explaining that it cannot ( | ||||||||||||||||||||||||||||
| 114 | with some optional information about what it can provide). | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Classic REST over HTTP allows for server side content negotiation over representation | ||||||||||||||||||||||||||||
| 117 | media type, language, encoding and character set. The L<Catalyst> request trait | ||||||||||||||||||||||||||||
| 118 | L<Catalyst::TraitFor::Request::ContentNegotiationHelpers> provides methods on the request | ||||||||||||||||||||||||||||
| 119 | object for helping a programmer correctly make choices based on what a client is | ||||||||||||||||||||||||||||
| 120 | requesting. This Actionrole provides additional sugar by allowing one to delegate | ||||||||||||||||||||||||||||
| 121 | to an action based on the matched media type. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | B<NOTE>: This actionrole only provides feaures over mediatypes NOT other catagories | ||||||||||||||||||||||||||||
| 124 | of content negotiation. However in general practice content negotiation over | ||||||||||||||||||||||||||||
| 125 | media types is probably the most common use case. It would be easy to add action | ||||||||||||||||||||||||||||
| 126 | roles that clone this one to do the same for encoding, language, etc. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 USAGE | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Apply the action role to an action, for example: | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub myaction :Chained(/) Does('ProvidesMedia') CaptureArgs(1) { | ||||||||||||||||||||||||||||
| 133 | my ($self, $c, $id) = @_; | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | When the action body completes (as long as it does not detach or generate | ||||||||||||||||||||||||||||
| 137 | an error) we then delegate (via $c->forward) to an action that matches the | ||||||||||||||||||||||||||||
| 138 | first action's method name, followed by an extension that can be matched to | ||||||||||||||||||||||||||||
| 139 | a media type: | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Executed if the request accept header like prefers and accepts | ||||||||||||||||||||||||||||
| 142 | # type 'application/json'. | ||||||||||||||||||||||||||||
| 143 | sub myaction_JSON :Action { } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Executed if the request accept header like prefers and accepts | ||||||||||||||||||||||||||||
| 146 | # type 'text/html'. | ||||||||||||||||||||||||||||
| 147 | sub myaction_HTML :Action { } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | When several possible matches exist, as in the above example, we inspect the | ||||||||||||||||||||||||||||
| 150 | HTTP request ACCEPT header and determine the best match. Should no match | ||||||||||||||||||||||||||||
| 151 | exist, we return a default 'no match' response which looks like this: | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub myaction_no_match :Action { | ||||||||||||||||||||||||||||
| 154 | my ($self, $c, $matches) = @_; | ||||||||||||||||||||||||||||
| 155 | my $allowed = join(',', keys %{$matches||+{}}); | ||||||||||||||||||||||||||||
| 156 | $c->response->status(406); | ||||||||||||||||||||||||||||
| 157 | $c->response->content_type('text/plain'); | ||||||||||||||||||||||||||||
| 158 | $c->response->body("No match for Accept header. Media types we support are: $allowed"); | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | You may override this default action by providing your own. The argument | ||||||||||||||||||||||||||||
| 162 | "$matches" is a hashref where the keys are matches allowed and the values | ||||||||||||||||||||||||||||
| 163 | are references to the matched action, that way you could for example | ||||||||||||||||||||||||||||
| 164 | redelegate to default. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 VERSUS Catalyst::Action::REST | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | L<Catalyst::TraitFor::Request::REST> (which comes with the L<Catalyst::Action::REST> | ||||||||||||||||||||||||||||
| 169 | distribution) defines a method 'accepted_content_types' which returns an array of | ||||||||||||||||||||||||||||
| 170 | content types that the client accepts, sorted in order by inspecting the ACCEPT | ||||||||||||||||||||||||||||
| 171 | header. However for GET requests this is overridden and instead we return the | ||||||||||||||||||||||||||||
| 172 | request content-type if one exists. I'm not sure this is exactly correct. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 METHODS | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | This role contains the following methods. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 ALSO SEE | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | L<Catalyst::TraitFor::Request::ContentNegotiationHelpers>, L<Catalyst> | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | John Napiorkowski <jnapiork@cpan.org> | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Copyright (c) 2015 the above named AUTHOR | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | You may distribute this code under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||||||||