[Catalyst-commits] r7344 - in Catalyst-Action-REST/0.60/trunk:
lib/Catalyst/Action lib/Catalyst/Action/Deserialize
lib/Catalyst/Action/Serialize lib/Catalyst/Action/Serialize/Data
lib/Catalyst/Action/Serialize/XML
lib/Catalyst/Action/Serialize/YAML lib/Catalyst/Controller
lib/Catalyst/Request t t/lib/Test
jshirley at dev.catalyst.perl.org
jshirley at dev.catalyst.perl.org
Thu Jan 3 17:54:25 GMT 2008
Author: jshirley
Date: 2008-01-03 17:54:23 +0000 (Thu, 03 Jan 2008)
New Revision: 7344
Modified:
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize/JSON.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize/YAML.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/REST.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/Data/Serializer.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/JSON.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/View.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/XML/Simple.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/YAML.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/YAML/HTML.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/SerializeBase.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Controller/REST.pm
Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Request/REST.pm
Catalyst-Action-REST/0.60/trunk/t/catalyst-action-serialize-accept.t
Catalyst-Action-REST/0.60/trunk/t/lib/Test/Serialize.pm
Log:
Configuration patches to handle component based configuration in a sane way, as well as the 'serialize' key available for global defaults.
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize/JSON.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize/JSON.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize/JSON.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -18,22 +18,24 @@
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;
}
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize/YAML.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize/YAML.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize/YAML.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -29,7 +29,8 @@
$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;
}
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Deserialize.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -52,14 +52,12 @@
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 @@
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<Catalyst::Action::Serialize> for the details.
-
The specifics of deserializing each content-type is implemented as
a plugin to L<Catalyst::Action::Deserialize>. You can see a list
of currently implemented plugins in L<Catalyst::Controller::REST>.
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/REST.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/REST.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/REST.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -16,7 +16,7 @@
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
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/Data/Serializer.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/Data/Serializer.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/Data/Serializer.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -16,7 +16,11 @@
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 @@
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 );
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/JSON.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/JSON.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/JSON.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -17,7 +17,11 @@
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});
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/View.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/View.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/View.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -7,9 +7,13 @@
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");
return 0;
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/XML/Simple.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/XML/Simple.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/XML/Simple.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -25,7 +25,11 @@
}
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} });
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/YAML/HTML.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/YAML/HTML.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/YAML/HTML.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -18,7 +18,11 @@
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 = "<html>";
$output .= "<title>" . $app . "</title>";
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/YAML.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/YAML.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize/YAML.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -17,7 +17,11 @@
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});
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/Serialize.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -29,15 +29,15 @@
$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 @@
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 @@
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<Catalyst::Action::Serialize>.
+The specifics of serializing each content-type is implemented as a plugin to
+L<Catalyst::Action::Serialize>.
Typically, you would use this ActionClass on your C<end> method. However,
nothing is stopping you from choosing specific methods to Serialize:
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/SerializeBase.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/SerializeBase.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Action/SerializeBase.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -43,13 +43,23 @@
# 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);
}
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Controller/REST.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Controller/REST.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Controller/REST.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -175,7 +175,7 @@
can ensure that something is always returned by setting the C<default>
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 @@
__PACKAGE__->mk_accessors(qw(serialize));
__PACKAGE__->config(
- serialize => {
'stash_key' => 'rest',
'map' => {
'text/html' => 'YAML::HTML',
@@ -228,7 +227,6 @@
'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ],
'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ],
},
- }
);
sub begin : ActionClass('Deserialize') {
@@ -351,7 +349,7 @@
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 @@
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 @@
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;
}
Modified: Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Request/REST.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Request/REST.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/lib/Catalyst/Request/REST.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -87,7 +87,6 @@
if $self->content_type;
if ($self->method eq "GET" && $self->param('content-type')) {
-
$types{ $self->param('content-type') } = 2;
}
Modified: Catalyst-Action-REST/0.60/trunk/t/catalyst-action-serialize-accept.t
===================================================================
--- Catalyst-Action-REST/0.60/trunk/t/catalyst-action-serialize-accept.t 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/t/catalyst-action-serialize-accept.t 2008-01-03 17:54:23 UTC (rev 7344)
@@ -14,7 +14,7 @@
__PACKAGE__->config(
name => 'Test::Catalyst::Action::Serialize',
serialize => {
- 'default' => 'text/x-yaml',
+ 'default' => 'text/x-yaml',
'stash_key' => 'rest',
'map' => {
'text/x-yaml' => 'YAML',
Modified: Catalyst-Action-REST/0.60/trunk/t/lib/Test/Serialize.pm
===================================================================
--- Catalyst-Action-REST/0.60/trunk/t/lib/Test/Serialize.pm 2008-01-03 17:49:05 UTC (rev 7343)
+++ Catalyst-Action-REST/0.60/trunk/t/lib/Test/Serialize.pm 2008-01-03 17:54:23 UTC (rev 7344)
@@ -13,47 +13,13 @@
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::Controller::REST");
__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!', };
-}
-
1;
More information about the Catalyst-commits
mailing list