line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Action::SerializeBase; |
2
|
|
|
|
|
|
|
$Catalyst::Action::SerializeBase::VERSION = '1.20'; |
3
|
12
|
|
|
12
|
|
9318
|
use Moose; |
|
12
|
|
|
|
|
29
|
|
|
12
|
|
|
|
|
117
|
|
4
|
12
|
|
|
12
|
|
76635
|
use namespace::autoclean; |
|
12
|
|
|
|
|
30
|
|
|
12
|
|
|
|
|
113
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
extends 'Catalyst::Action'; |
7
|
12
|
|
|
12
|
|
857
|
use Module::Pluggable::Object; |
|
12
|
|
|
|
|
27
|
|
|
12
|
|
|
|
|
289
|
|
8
|
12
|
|
|
12
|
|
5988
|
use Catalyst::Request::REST; |
|
12
|
|
|
|
|
34
|
|
|
12
|
|
|
|
|
545
|
|
9
|
12
|
|
|
12
|
|
98
|
use Catalyst::Utils (); |
|
12
|
|
|
|
|
31
|
|
|
12
|
|
|
|
|
13162
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
after BUILDARGS => sub { |
12
|
|
|
|
|
|
|
my $class = shift; |
13
|
|
|
|
|
|
|
my $config = shift; |
14
|
|
|
|
|
|
|
Catalyst::Request::REST->_insert_self_into( $config->{class} ); |
15
|
|
|
|
|
|
|
}; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has [qw(_serialize_plugins _loaded_plugins)] => ( is => 'rw' ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _load_content_plugins { |
20
|
18
|
|
|
18
|
|
35
|
my $self = shift; |
21
|
18
|
|
|
|
|
46
|
my ( $search_path, $controller, $c ) = @_; |
22
|
|
|
|
|
|
|
|
23
|
18
|
100
|
|
|
|
790
|
unless ( defined( $self->_loaded_plugins ) ) { |
24
|
11
|
|
|
|
|
454
|
$self->_loaded_plugins( {} ); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Load the Serialize Classes |
28
|
18
|
100
|
|
|
|
782
|
unless ( defined( $self->_serialize_plugins ) ) { |
29
|
11
|
|
|
|
|
17
|
my @plugins; |
30
|
11
|
|
|
|
|
153
|
my $mpo = |
31
|
|
|
|
|
|
|
Module::Pluggable::Object->new( 'search_path' => [$search_path], ); |
32
|
11
|
|
|
|
|
136
|
@plugins = $mpo->plugins; |
33
|
11
|
|
|
|
|
88325
|
$self->_serialize_plugins( \@plugins ); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Finally, we load the class. If you have a default serializer, |
37
|
|
|
|
|
|
|
# and we still don't have a content-type that exists in the map, |
38
|
|
|
|
|
|
|
# we'll use it. |
39
|
18
|
|
|
|
|
64
|
my $sclass = $search_path . "::"; |
40
|
18
|
|
|
|
|
37
|
my $sarg; |
41
|
|
|
|
|
|
|
my $map; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
0
|
my $config; |
44
|
|
|
|
|
|
|
|
45
|
18
|
50
|
|
|
|
73
|
if ( exists $controller->{'serialize'} ) { |
46
|
0
|
|
|
|
|
0
|
$c->log->info("Catalyst::Action::REST - deprecated use of 'serialize' for configuration."); |
47
|
0
|
|
|
|
|
0
|
$c->log->info("Please see 'CONFIGURATION' in Catalyst::Controller::REST."); |
48
|
0
|
|
|
|
|
0
|
$config = $controller->{'serialize'}; |
49
|
|
|
|
|
|
|
# if they're using the deprecated config, they may be expecting a |
50
|
|
|
|
|
|
|
# default mapping too. |
51
|
0
|
|
0
|
|
|
0
|
$config->{map} ||= $controller->{map}; |
52
|
|
|
|
|
|
|
} else { |
53
|
18
|
|
|
|
|
33
|
$config = $controller; |
54
|
|
|
|
|
|
|
} |
55
|
18
|
|
|
|
|
50
|
$map = $config->{'map'}; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# pick preferred content type |
58
|
18
|
|
|
|
|
35
|
my @accepted_types; # priority order, best first |
59
|
|
|
|
|
|
|
# give top priority to content type specified by stash, if any |
60
|
18
|
|
|
|
|
46
|
my $content_type_stash_key = $config->{content_type_stash_key}; |
61
|
18
|
50
|
33
|
|
|
143
|
if ($content_type_stash_key |
62
|
|
|
|
|
|
|
and my $stashed = $c->stash->{$content_type_stash_key} |
63
|
|
|
|
|
|
|
) { |
64
|
|
|
|
|
|
|
# convert to array if not already a ref |
65
|
0
|
0
|
|
|
|
0
|
$stashed = [ $stashed ] if not ref $stashed; |
66
|
0
|
|
|
|
|
0
|
push @accepted_types, @$stashed; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
# then content types requested by caller |
69
|
18
|
|
|
|
|
36
|
push @accepted_types, @{ $c->request->accepted_content_types }; |
|
18
|
|
|
|
|
523
|
|
70
|
|
|
|
|
|
|
# then the default |
71
|
18
|
50
|
|
|
|
80
|
push @accepted_types, $config->{'default'} if $config->{'default'}; |
72
|
|
|
|
|
|
|
# pick the best match that we have a serializer mapping for |
73
|
18
|
|
|
|
|
40
|
my ($content_type) = grep { $map->{$_} } @accepted_types; |
|
19
|
|
|
|
|
70
|
|
74
|
|
|
|
|
|
|
|
75
|
18
|
100
|
|
|
|
59
|
return $self->unsupported_media_type($c, $content_type) |
76
|
|
|
|
|
|
|
if not $content_type; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# carp about old text/x-json |
79
|
17
|
100
|
|
|
|
55
|
if ($content_type eq 'text/x-json') { |
80
|
2
|
|
|
|
|
9
|
$c->log->info('Using deprecated text/x-json content-type.'); |
81
|
2
|
|
|
|
|
67
|
$c->log->info('Use application/json instead!'); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
17
|
50
|
|
|
|
88
|
if ( exists( $map->{$content_type} ) ) { |
85
|
17
|
|
|
|
|
24
|
my $mc; |
86
|
17
|
100
|
|
|
|
58
|
if ( ref( $map->{$content_type} ) eq "ARRAY" ) { |
87
|
6
|
|
|
|
|
18
|
$mc = $map->{$content_type}->[0]; |
88
|
6
|
|
|
|
|
14
|
$sarg = $map->{$content_type}->[1]; |
89
|
|
|
|
|
|
|
} else { |
90
|
11
|
|
|
|
|
21
|
$mc = $map->{$content_type}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
# TODO: Handle custom serializers more elegantly.. this is a start, |
93
|
|
|
|
|
|
|
# but how do we determine which is Serialize and Deserialize? |
94
|
|
|
|
|
|
|
#if ($mc =~ /^+/) { |
95
|
|
|
|
|
|
|
# $sclass = $mc; |
96
|
|
|
|
|
|
|
# $sclass =~ s/^+//g; |
97
|
|
|
|
|
|
|
#} else { |
98
|
17
|
|
|
|
|
46
|
$sclass .= $mc; |
99
|
|
|
|
|
|
|
#} |
100
|
17
|
50
|
|
|
|
27
|
if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) { |
|
17
|
|
|
|
|
767
|
|
101
|
0
|
|
|
|
|
0
|
return $self->unsupported_media_type($c, $content_type); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} else { |
104
|
0
|
|
|
|
|
0
|
return $self->unsupported_media_type($c, $content_type); |
105
|
|
|
|
|
|
|
} |
106
|
17
|
100
|
|
|
|
756
|
unless ( exists( $self->_loaded_plugins->{$sclass} ) ) { |
107
|
11
|
|
|
|
|
23
|
my $load_class = $sclass; |
108
|
11
|
|
|
|
|
57
|
$load_class =~ s/::/\//g; |
109
|
11
|
|
|
|
|
56
|
$load_class =~ s/$/.pm/g; |
110
|
11
|
|
|
|
|
21
|
eval { require $load_class; }; |
|
11
|
|
|
|
|
5710
|
|
111
|
11
|
50
|
|
|
|
57
|
if ($@) { |
112
|
0
|
|
|
|
|
0
|
$c->log->error( |
113
|
|
|
|
|
|
|
"Error loading $sclass for " . $content_type . ": $!" ); |
114
|
0
|
|
|
|
|
0
|
return $self->unsupported_media_type($c, $content_type); |
115
|
|
|
|
|
|
|
} else { |
116
|
11
|
|
|
|
|
510
|
$self->_loaded_plugins->{$sclass} = 1; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
17
|
100
|
|
|
|
81
|
if ($search_path eq "Catalyst::Action::Serialize") { |
121
|
8
|
50
|
|
|
|
235
|
unless( $c->response->header( 'Vary' ) ) { |
122
|
8
|
50
|
|
|
|
2549
|
if ($content_type) { |
|
|
0
|
|
|
|
|
|
123
|
8
|
|
|
|
|
237
|
$c->response->header( 'Vary' => 'Content-Type' ); |
124
|
|
|
|
|
|
|
} elsif ($c->request->accept_only) { |
125
|
0
|
|
|
|
|
0
|
$c->response->header( 'Vary' => 'Accept' ); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
8
|
|
|
|
|
2142
|
$c->response->content_type($content_type); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
17
|
|
|
|
|
1823
|
return $sclass, $sarg, $content_type; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub unsupported_media_type { |
135
|
1
|
|
|
1
|
0
|
3
|
my ( $self, $c, $content_type ) = @_; |
136
|
1
|
|
|
|
|
5
|
$c->res->content_type('text/plain'); |
137
|
1
|
|
|
|
|
311
|
$c->res->status(415); |
138
|
1
|
50
|
33
|
|
|
152
|
if (defined($content_type) && $content_type ne "") { |
139
|
0
|
|
|
|
|
0
|
$c->res->body( |
140
|
|
|
|
|
|
|
"Content-Type " . $content_type . " is not supported.\r\n" ); |
141
|
|
|
|
|
|
|
} else { |
142
|
1
|
|
|
|
|
4
|
$c->res->body( |
143
|
|
|
|
|
|
|
"Cannot find a Content-Type supported by your client.\r\n" ); |
144
|
|
|
|
|
|
|
} |
145
|
1
|
|
|
|
|
69
|
return undef; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub serialize_bad_request { |
149
|
3
|
|
|
3
|
0
|
11
|
my ( $self, $c, $content_type, $error ) = @_; |
150
|
3
|
|
|
|
|
14
|
$c->res->content_type('text/plain'); |
151
|
3
|
|
|
|
|
860
|
$c->res->status(400); |
152
|
3
|
|
|
|
|
437
|
$c->res->body( |
153
|
|
|
|
|
|
|
"Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" ); |
154
|
3
|
|
|
|
|
206
|
return undef; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
1; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 NAME |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Catalyst::Action::SerializeBase - Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 DESCRIPTION |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This module implements the plugin loading and content-type negotiating |
168
|
|
|
|
|
|
|
code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 SEE ALSO |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>, |
173
|
|
|
|
|
|
|
L<Catalyst::Controller::REST>, |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 AUTHORS |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
See L<Catalyst::Action::REST> for authors. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 LICENSE |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
You may distribute this code under the same terms as Perl itself. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |