Commit | Line | Data |
e601adda |
1 | # |
2 | # Catlyst::Action::SerializeBase.pm |
be3c588a |
3 | # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com> |
e601adda |
4 | # |
5 | # $Id$ |
6 | |
7 | package Catalyst::Action::SerializeBase; |
8 | |
9 | use strict; |
10 | use warnings; |
11 | |
12 | use base 'Catalyst::Action'; |
13 | use Module::Pluggable::Object; |
14 | use Data::Dump qw(dump); |
9a76221e |
15 | use Catalyst::Request::REST; |
16 | |
17 | Catalyst->request_class('Catalyst::Request::REST') |
18 | unless Catalyst->request_class->isa('Catalyst::Request::REST'); |
e601adda |
19 | |
20 | __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins)); |
21 | |
22 | sub _load_content_plugins { |
23 | my $self = shift; |
24 | my ( $search_path, $controller, $c ) = @_; |
25 | |
26 | unless ( defined( $self->_loaded_plugins ) ) { |
27 | $self->_loaded_plugins( {} ); |
28 | } |
29 | |
30 | # Load the Serialize Classes |
31 | unless ( defined( $self->_serialize_plugins ) ) { |
32 | my @plugins; |
33 | my $mpo = |
34 | Module::Pluggable::Object->new( 'search_path' => [$search_path], ); |
35 | @plugins = $mpo->plugins; |
36 | $self->_serialize_plugins( \@plugins ); |
37 | } |
38 | |
e601adda |
39 | # Finally, we load the class. If you have a default serializer, |
40 | # and we still don't have a content-type that exists in the map, |
41 | # we'll use it. |
42 | my $sclass = $search_path . "::"; |
43 | my $sarg; |
faf5c20b |
44 | my $map; |
367b3ff4 |
45 | |
faf5c20b |
46 | my $config; |
47 | |
48 | if ( exists $controller->config->{'serialize'} ) { |
49 | $c->log->info("Using deprecated configuration for Catalyst::Action::REST!"); |
50 | $c->log->info("Please see perldoc Catalyst::Action::REST for the update guide"); |
51 | $config = $controller->config->{'serialize'}; |
52 | } else { |
53 | $config = $controller->config; |
54 | } |
55 | $map = $config->{'map'}; |
367b3ff4 |
56 | # If we don't have a handler for our preferred content type, try |
57 | # the default |
c0aef9cd |
58 | |
59 | my ($content_type) = grep { $map->{$_} } @{$c->request->accepted_content_types}; |
60 | |
61 | unless ( defined $content_type ) { |
faf5c20b |
62 | if( exists $config->{'default'} ) { |
63 | $content_type = $config->{'default'} ; |
367b3ff4 |
64 | } else { |
65 | return $self->_unsupported_media_type($c, $content_type); |
66 | } |
67 | } |
68 | |
c0aef9cd |
69 | # carp about old text/x-json |
70 | if ($content_type eq 'text/x-json') { |
71 | $c->log->info('Using deprecated text/x-json content-type.'); |
72 | $c->log->info('Use application/json instead!'); |
73 | } |
74 | |
e601adda |
75 | if ( exists( $map->{$content_type} ) ) { |
76 | my $mc; |
77 | if ( ref( $map->{$content_type} ) eq "ARRAY" ) { |
78 | $mc = $map->{$content_type}->[0]; |
79 | $sarg = $map->{$content_type}->[1]; |
80 | } else { |
81 | $mc = $map->{$content_type}; |
82 | } |
83 | # TODO: Handle custom serializers more elegantly.. this is a start, |
84 | # but how do we determine which is Serialize and Deserialize? |
85 | #if ($mc =~ /^+/) { |
86 | # $sclass = $mc; |
87 | # $sclass =~ s/^+//g; |
88 | #} else { |
89 | $sclass .= $mc; |
90 | #} |
91 | if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) { |
92 | return $self->_unsupported_media_type($c, $content_type); |
93 | } |
94 | } else { |
367b3ff4 |
95 | return $self->_unsupported_media_type($c, $content_type); |
e601adda |
96 | } |
97 | unless ( exists( $self->_loaded_plugins->{$sclass} ) ) { |
98 | my $load_class = $sclass; |
99 | $load_class =~ s/::/\//g; |
100 | $load_class =~ s/$/.pm/g; |
101 | eval { require $load_class; }; |
102 | if ($@) { |
103 | $c->log->error( |
d4611771 |
104 | "Error loading $sclass for " . $content_type . ": $!" ); |
e601adda |
105 | return $self->_unsupported_media_type($c, $content_type); |
106 | } else { |
107 | $self->_loaded_plugins->{$sclass} = 1; |
108 | } |
109 | } |
110 | |
111 | if ($search_path eq "Catalyst::Action::Serialize") { |
112 | if ($content_type) { |
113 | $c->response->header( 'Vary' => 'Content-Type' ); |
9a76221e |
114 | } elsif ($c->request->accept_only) { |
e601adda |
115 | $c->response->header( 'Vary' => 'Accept' ); |
116 | } |
117 | $c->response->content_type($content_type); |
118 | } |
119 | |
120 | return $sclass, $sarg, $content_type; |
121 | } |
122 | |
123 | sub _unsupported_media_type { |
124 | my ( $self, $c, $content_type ) = @_; |
125 | $c->res->content_type('text/plain'); |
126 | $c->res->status(415); |
2224bad1 |
127 | if (defined($content_type) && $content_type ne "") { |
e601adda |
128 | $c->res->body( |
129 | "Content-Type " . $content_type . " is not supported.\r\n" ); |
130 | } else { |
131 | $c->res->body( |
132 | "Cannot find a Content-Type supported by your client.\r\n" ); |
133 | } |
134 | return undef; |
135 | } |
136 | |
137 | sub _serialize_bad_request { |
138 | my ( $self, $c, $content_type, $error ) = @_; |
139 | $c->res->content_type('text/plain'); |
140 | $c->res->status(400); |
141 | $c->res->body( |
142 | "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" ); |
143 | return undef; |
144 | } |
145 | |
146 | 1; |
147 | |
148 | =head1 NAME |
149 | |
150 | B<Catalyst::Action::SerializeBase> |
151 | |
152 | Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize. |
153 | |
154 | =head1 DESCRIPTION |
155 | |
156 | This module implements the plugin loading and content-type negotiating |
157 | code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>. |
158 | |
159 | =head1 SEE ALSO |
160 | |
161 | L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>, |
162 | L<Catalyst::Controller::REST>, |
163 | |
164 | =head1 AUTHOR |
165 | |
166 | Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway. |
167 | |
168 | Marchex, Inc. paid me while I developed this module. (http://www.marchex.com) |
169 | |
170 | =head1 LICENSE |
171 | |
172 | You may distribute this code under the same terms as Perl itself. |
173 | |
174 | =cut |
175 | |