From: jshirley Date: Thu, 3 Jan 2008 17:54:23 +0000 (+0000) Subject: Configuration patches to handle component based configuration in a sane way, as well... X-Git-Tag: 1.08~266 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=faf5c20bff09c91f18b46a3d3d8349379aa36192;p=catagits%2FCatalyst-Action-Serialize-Data-Serializer.git Configuration patches to handle component based configuration in a sane way, as well as the 'serialize' key available for global defaults. --- diff --git a/lib/Catalyst/Action/Deserialize.pm b/lib/Catalyst/Action/Deserialize.pm index f89447f..ed584c6 100644 --- a/lib/Catalyst/Action/Deserialize.pm +++ b/lib/Catalyst/Action/Deserialize.pm @@ -52,14 +52,12 @@ Catalyst::Action::Deserialize - Deserialize Data in a Request package Foo::Controller::Bar; __PACKAGE__->config( - serialize => { - 'default' => 'text/x-yaml', - 'stash_key' => 'rest', - 'map' => { - 'text/x-yaml' => 'YAML', - 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], - }, - } + 'default' => 'text/x-yaml', + 'stash_key' => 'rest', + 'map' => { + 'text/x-yaml' => 'YAML', + 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], + }, ); sub begin :ActionClass('Deserialize') {} @@ -71,9 +69,6 @@ It assumes that the body of the HTTP Request is a serialized object. The serializer is selected by introspecting the requests content-type header. -It requires that your Catalyst controller have a "serialize" entry -in it's configuration. See L for the details. - The specifics of deserializing each content-type is implemented as a plugin to L. You can see a list of currently implemented plugins in L. diff --git a/lib/Catalyst/Action/Deserialize/JSON.pm b/lib/Catalyst/Action/Deserialize/JSON.pm index 560bf24..ce36dd9 100644 --- a/lib/Catalyst/Action/Deserialize/JSON.pm +++ b/lib/Catalyst/Action/Deserialize/JSON.pm @@ -18,22 +18,24 @@ sub execute { my ( $controller, $c, $test ) = @_; my $body = $c->request->body; + my $rbody; + if ($body) { - my $rdata; - my $rbody; while (my $line = <$body>) { $rbody .= $line; } - eval { - $rdata = JSON::Syck::Load( $rbody ); - }; + } + + if ( $rbody ) { + my $rdata = eval { JSON::Syck::Load( $rbody ); }; if ($@) { return $@; } $c->request->data($rdata); } else { $c->log->debug( - 'I would have deserialized, but there was nothing in the body!'); + 'I would have deserialized, but there was nothing in the body!') + if $c->debug; } return 1; } diff --git a/lib/Catalyst/Action/Deserialize/YAML.pm b/lib/Catalyst/Action/Deserialize/YAML.pm index 84630f4..bae0fc7 100644 --- a/lib/Catalyst/Action/Deserialize/YAML.pm +++ b/lib/Catalyst/Action/Deserialize/YAML.pm @@ -29,7 +29,8 @@ sub execute { $c->request->data($rdata); } else { $c->log->debug( - 'I would have deserialized, but there was nothing in the body!'); + 'I would have deserialized, but there was nothing in the body!') + if $c->debug; } return 1; } diff --git a/lib/Catalyst/Action/REST.pm b/lib/Catalyst/Action/REST.pm index b07d1c1..94d6e70 100644 --- a/lib/Catalyst/Action/REST.pm +++ b/lib/Catalyst/Action/REST.pm @@ -16,7 +16,7 @@ use Catalyst::Request::REST; use 5.8.1; our - $VERSION = '0.50'; + $VERSION = '0.60'; # This is wrong in several ways. First, there's no guarantee that # Catalyst.pm has not been subclassed. Two, there's no guarantee that diff --git a/lib/Catalyst/Action/Serialize.pm b/lib/Catalyst/Action/Serialize.pm index a55d531..549e312 100644 --- a/lib/Catalyst/Action/Serialize.pm +++ b/lib/Catalyst/Action/Serialize.pm @@ -29,15 +29,15 @@ sub execute { $controller, $c ); unless ( defined($sclass) ) { if ( defined($content_type) ) { - $c->log->debug("Could not find a serializer for $content_type"); + $c->log->info("Could not find a serializer for $content_type"); } else { - $c->log->debug( + $c->log->info( "Could not find a serializer for an empty content type"); } return 1; } $c->log->debug( - "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) ); + "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) ) if $c->debug; my $rc; if ( defined($sarg) ) { @@ -65,14 +65,12 @@ Catalyst::Action::Serialize - Serialize Data in a Response package Foo::Controller::Bar; __PACKAGE__->config( - serialize => { - 'default' => 'text/x-yaml', - 'stash_key' => 'rest', - 'map' => { - 'text/html' => [ 'View', 'TT', ], - 'text/x-yaml' => 'YAML', - 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], - }, + 'default' => 'text/x-yaml', + 'stash_key' => 'rest', + 'map' => { + 'text/html' => [ 'View', 'TT', ], + 'text/x-yaml' => 'YAML', + 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], } ); @@ -83,12 +81,11 @@ Catalyst::Action::Serialize - Serialize Data in a Response This action will serialize the body of an HTTP Response. The serializer is selected by introspecting the HTTP Requests content-type header. -It requires that your Catalyst controller have a "serialize" entry -in it's configuration, which sets up the mapping between Content Type's -and Serialization classes. +It requires that your Catalyst controller is properly configured to set up the +mapping between Content Type's and Serialization classes. -The specifics of serializing each content-type is implemented as -a plugin to L. +The specifics of serializing each content-type is implemented as a plugin to +L. Typically, you would use this ActionClass on your C method. However, nothing is stopping you from choosing specific methods to Serialize: diff --git a/lib/Catalyst/Action/Serialize/Data/Serializer.pm b/lib/Catalyst/Action/Serialize/Data/Serializer.pm index 7297fcd..1aba2c7 100644 --- a/lib/Catalyst/Action/Serialize/Data/Serializer.pm +++ b/lib/Catalyst/Action/Serialize/Data/Serializer.pm @@ -16,7 +16,11 @@ sub execute { my $self = shift; my ( $controller, $c, $serializer ) = @_; - my $stash_key = $controller->config->{'serialize'}->{'stash_key'} || 'rest'; + my $stash_key = ( + $controller->config->{'serialize'} ? + $controller->config->{'serialize'}->{'stash_key'} : + $controller->config->{'stash_key'} + ) || 'rest'; my $sp = $serializer; $sp =~ s/::/\//g; $sp .= ".pm"; @@ -24,7 +28,7 @@ sub execute { require $sp }; if ($@) { - $c->log->debug("Could not load $serializer, refusing to serialize: $@"); + $c->log->info("Could not load $serializer, refusing to serialize: $@"); return 0; } my $dso = Data::Serializer->new( serializer => $serializer ); diff --git a/lib/Catalyst/Action/Serialize/JSON.pm b/lib/Catalyst/Action/Serialize/JSON.pm index 8a74a3e..a67853d 100644 --- a/lib/Catalyst/Action/Serialize/JSON.pm +++ b/lib/Catalyst/Action/Serialize/JSON.pm @@ -17,7 +17,11 @@ sub execute { my $self = shift; my ( $controller, $c ) = @_; - my $stash_key = $controller->config->{'serialize'}->{'stash_key'} || 'rest'; + my $stash_key = ( + $controller->config->{'serialize'} ? + $controller->config->{'serialize'}->{'stash_key'} : + $controller->config->{'stash_key'} + ) || 'rest'; my $output; eval { $output = JSON::Syck::Dump($c->stash->{$stash_key}); diff --git a/lib/Catalyst/Action/Serialize/View.pm b/lib/Catalyst/Action/Serialize/View.pm index 93be123..09ee45b 100644 --- a/lib/Catalyst/Action/Serialize/View.pm +++ b/lib/Catalyst/Action/Serialize/View.pm @@ -7,8 +7,12 @@ use base 'Catalyst::Action'; sub execute { my $self = shift; my ( $controller, $c, $view ) = @_; - my $stash_key = $controller->config->{'serialize'}->{'stash_key'} - || 'rest'; + + my $stash_key = ( + $controller->config->{'serialize'} ? + $controller->config->{'serialize'}->{'stash_key'} : + $controller->config->{'stash_key'} + ) || 'rest'; if ( !$c->view($view) ) { $c->log->error("Could not load $view, refusing to serialize"); diff --git a/lib/Catalyst/Action/Serialize/XML/Simple.pm b/lib/Catalyst/Action/Serialize/XML/Simple.pm index de297d2..668e3f5 100644 --- a/lib/Catalyst/Action/Serialize/XML/Simple.pm +++ b/lib/Catalyst/Action/Serialize/XML/Simple.pm @@ -25,7 +25,11 @@ sub execute { } my $xs = XML::Simple->new(ForceArray => 0,); - my $stash_key = $controller->config->{'serialize'}->{'stash_key'} || 'rest'; + my $stash_key = ( + $controller->config->{'serialize'} ? + $controller->config->{'serialize'}->{'stash_key'} : + $controller->config->{'stash_key'} + ) || 'rest'; my $output; eval { $output = $xs->XMLout({ data => $c->stash->{$stash_key} }); diff --git a/lib/Catalyst/Action/Serialize/YAML.pm b/lib/Catalyst/Action/Serialize/YAML.pm index 8a5939d..9ef8dc0 100644 --- a/lib/Catalyst/Action/Serialize/YAML.pm +++ b/lib/Catalyst/Action/Serialize/YAML.pm @@ -17,7 +17,11 @@ sub execute { my $self = shift; my ( $controller, $c ) = @_; - my $stash_key = $controller->config->{'serialize'}->{'stash_key'} || 'rest'; + my $stash_key = ( + $controller->config->{'serialize'} ? + $controller->config->{'serialize'}->{'stash_key'} : + $controller->config->{'stash_key'} + ) || 'rest'; my $output; eval { $output = Dump($c->stash->{$stash_key}); diff --git a/lib/Catalyst/Action/Serialize/YAML/HTML.pm b/lib/Catalyst/Action/Serialize/YAML/HTML.pm index 40a04f7..0fce953 100644 --- a/lib/Catalyst/Action/Serialize/YAML/HTML.pm +++ b/lib/Catalyst/Action/Serialize/YAML/HTML.pm @@ -18,7 +18,11 @@ sub execute { my $self = shift; my ( $controller, $c ) = @_; - my $stash_key = $controller->config->{'serialize'}->{'stash_key'} || 'rest'; + my $stash_key = ( + $controller->config->{'serialize'} ? + $controller->config->{'serialize'}->{'stash_key'} : + $controller->config->{'stash_key'} + ) || 'rest'; my $app = $c->config->{'name'} || ''; my $output = ""; $output .= "" . $app . ""; diff --git a/lib/Catalyst/Action/SerializeBase.pm b/lib/Catalyst/Action/SerializeBase.pm index 546f49a..db5d42e 100644 --- a/lib/Catalyst/Action/SerializeBase.pm +++ b/lib/Catalyst/Action/SerializeBase.pm @@ -43,13 +43,23 @@ sub _load_content_plugins { # we'll use it. my $sclass = $search_path . "::"; my $sarg; - my $map = $controller->config->{'serialize'}->{'map'}; - + my $map; + + my $config; + + if ( exists $controller->config->{'serialize'} ) { + $c->log->info("Using deprecated configuration for Catalyst::Action::REST!"); + $c->log->info("Please see perldoc Catalyst::Action::REST for the update guide"); + $config = $controller->config->{'serialize'}; + } else { + $config = $controller->config; + } + $map = $config->{'map'}; # If we don't have a handler for our preferred content type, try # the default if ( ! exists $map->{$content_type} ) { - if( exists $controller->config->{'serialize'}->{'default'} ) { - $content_type = $controller->config->{'serialize'}->{'default'} ; + if( exists $config->{'default'} ) { + $content_type = $config->{'default'} ; } else { return $self->_unsupported_media_type($c, $content_type); } diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm index 3761f07..6123010 100644 --- a/lib/Catalyst/Controller/REST.pm +++ b/lib/Catalyst/Controller/REST.pm @@ -175,7 +175,7 @@ response if an attempt to use an unsupported content-type is made. You can ensure that something is always returned by setting the C config option: - __PACKAGE__->config->{'serialize'}->{'default'} = 'text/x-yaml'; + __PACKAGE__->config->{'default'} = 'text/x-yaml'; Would make it always fall back to the serializer plugin defined for text/x-yaml. @@ -213,7 +213,6 @@ use Params::Validate qw(:all); __PACKAGE__->mk_accessors(qw(serialize)); __PACKAGE__->config( - serialize => { 'stash_key' => 'rest', 'map' => { 'text/html' => 'YAML::HTML', @@ -228,7 +227,6 @@ __PACKAGE__->config( 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ], 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ], }, - } ); sub begin : ActionClass('Deserialize') { @@ -351,7 +349,7 @@ sub status_bad_request { my %p = validate( @_, { message => { type => SCALAR }, }, ); $c->response->status(400); - $c->log->debug( "Status Bad Request: " . $p{'message'} ); + $c->log->debug( "Status Bad Request: " . $p{'message'} ) if $c->debug; $self->_set_entity( $c, { error => $p{'message'} } ); return 1; } @@ -377,7 +375,7 @@ sub status_not_found { my %p = validate( @_, { message => { type => SCALAR }, }, ); $c->response->status(404); - $c->log->debug( "Status Not Found: " . $p{'message'} ); + $c->log->debug( "Status Not Found: " . $p{'message'} ) if $c->debug; $self->_set_entity( $c, { error => $p{'message'} } ); return 1; } @@ -387,7 +385,7 @@ sub _set_entity { my $c = shift; my $entity = shift; if ( defined($entity) ) { - $c->stash->{ $self->config->{'serialize'}->{'stash_key'} } = $entity; + $c->stash->{ $self->{'stash_key'} } = $entity; } return 1; } diff --git a/lib/Catalyst/Request/REST.pm b/lib/Catalyst/Request/REST.pm index cd1b103..4caca6e 100644 --- a/lib/Catalyst/Request/REST.pm +++ b/lib/Catalyst/Request/REST.pm @@ -87,7 +87,6 @@ sub accepted_content_types { if $self->content_type; if ($self->method eq "GET" && $self->param('content-type')) { - $types{ $self->param('content-type') } = 2; } diff --git a/t/catalyst-action-serialize-accept.t b/t/catalyst-action-serialize-accept.t index e91c27e..873a379 100644 --- a/t/catalyst-action-serialize-accept.t +++ b/t/catalyst-action-serialize-accept.t @@ -14,7 +14,7 @@ use Catalyst; __PACKAGE__->config( name => 'Test::Catalyst::Action::Serialize', serialize => { - 'default' => 'text/x-yaml', + 'default' => 'text/x-yaml', 'stash_key' => 'rest', 'map' => { 'text/x-yaml' => 'YAML', diff --git a/t/lib/Test/Serialize.pm b/t/lib/Test/Serialize.pm index cb7c605..e19fda9 100644 --- a/t/lib/Test/Serialize.pm +++ b/t/lib/Test/Serialize.pm @@ -13,47 +13,13 @@ use Catalyst::Runtime '5.70'; use Catalyst; __PACKAGE__->config( - name => 'Test::Serialize', - serialize => { - 'stash_key' => 'rest', - 'map' => { - 'text/html' => 'YAML::HTML', - 'text/xml' => 'XML::Simple', - 'text/x-yaml' => 'YAML', - 'text/x-json' => 'JSON', - 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], - 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ], - 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ], - 'application/x-storable' => [ 'Data::Serializer', 'Storable' ], - 'application/x-freezethaw' => - [ 'Data::Serializer', 'FreezeThaw' ], - 'text/x-config-general' => - [ 'Data::Serializer', 'Config::General' ], - 'text/x-php-serialization' => - [ 'Data::Serializer', 'PHP::Serialization' ], - 'text/view' => [ 'View', 'Simple' ], - 'text/broken' => 'Broken', - }, - } + name => 'Test::Serialize', ); __PACKAGE__->setup; -__PACKAGE__->setup_component("Test::Serialize::View::Simple"); - -sub monkey_put : Local : ActionClass('Deserialize') { - my ( $self, $c ) = @_; - if ( ref($c->req->data) eq "HASH" ) { - $c->res->output( $c->req->data->{'sushi'} ); - } else { - $c->res->output(1) - } -} - -sub monkey_get : Local : ActionClass('Serialize') { - my ( $self, $c ) = @_; - $c->stash->{'rest'} = { monkey => 'likes chicken!', }; -} +__PACKAGE__->setup_component("Test::Serialize::Controller::REST"); +__PACKAGE__->setup_component("Test::Serialize::View::Simple"); 1;