2 # Catlyst::Action::Deserialize
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
7 package Catalyst::Action::Deserialize;
12 use base 'Catalyst::Action';
13 use Module::Pluggable::Object;
14 use Catalyst::Request::REST;
16 __PACKAGE__->mk_accessors(qw(plugins));
20 my ( $controller, $c, $test ) = @_;
22 my $nreq = bless( $c->request, 'Catalyst::Request::REST' );
25 unless ( defined( $self->plugins ) ) {
26 my $mpo = Module::Pluggable::Object->new(
28 'search_path' => ['Catalyst::Action::Deserialize'],
30 my @plugins = $mpo->plugins;
31 $self->plugins( \@plugins );
33 my $content_type = $c->request->content_type;
34 my $sclass = 'Catalyst::Action::Deserialize::';
36 my $map = $controller->serialize->{'map'};
37 if ( exists( $map->{$content_type} ) ) {
39 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
40 $mc = $map->{$content_type}->[0];
41 $sarg = $map->{$content_type}->[1];
43 $mc = $map->{$content_type};
46 if ( !grep( /^$sclass$/, @{ $self->plugins } ) ) {
47 die "Cannot find plugin $sclass for $content_type!";
50 if ( exists( $controller->serialize->{'default'} ) ) {
51 $sclass .= $controller->serialize->{'default'};
53 die "I cannot find a default serializer!";
57 my @demethods = qw(POST PUT OPTIONS);
58 my $method = $c->request->method;
59 if ( grep /^$method$/, @demethods ) {
60 if ( defined($sarg) ) {
61 $sclass->execute( $controller, $c, $sarg );
63 $sclass->execute( $controller, $c );
65 $self->NEXT::execute( @_, );
67 $self->NEXT::execute(@_);
73 Catalyst::Action::Deserialize - Deserialize Data in a Request
77 package Foo::Controller::Bar;
82 'stash_key' => 'rest',
84 'text/x-yaml' => 'YAML',
85 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
90 sub begin : ActionClass('Deserialize') {}
94 This action will deserialize HTTP POST, PUT, and OPTIONS requests.
95 It assumes that the body of the HTTP Request is a serialized object.
96 The serializer is selected by introspecting the requests content-type
99 It requires that your Catalyst controller have a "serialize" entry
100 in it's configuration.
102 The specifics of deserializing each content-type is implemented as
103 a plugin to L<Catalyst::Action::Deserialize>. You can see a list
104 of currently implemented plugins in L<Catalyst::Controller::REST>.
106 The results of your Deserializing will wind up in $c->req->data.
107 This is done through the magic of L<Catalyst::Request::REST>.
115 The default Serialization format. See the next section for
120 Takes a hashref, mapping Content-Types to a given plugin.
126 You likely want to look at L<Catalyst::Controller::REST>, which implements
127 a sensible set of defaults for a controller doing REST.
129 L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
133 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
135 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
139 You may distribute this code under the same terms as Perl itself.