From: Joel Bernstein Date: Mon, 25 Apr 2011 17:46:26 +0000 (+0100) Subject: separate request/response possible content-typees X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-REST.git;a=commitdiff_plain;h=ffbb0b919a336dea528e731202ca9b1bb98c1eec separate request/response possible content-typees --- diff --git a/lib/Catalyst/Action/SerializeBase.pm b/lib/Catalyst/Action/SerializeBase.pm index 1da09ad..3bb3427 100644 --- a/lib/Catalyst/Action/SerializeBase.pm +++ b/lib/Catalyst/Action/SerializeBase.pm @@ -69,7 +69,12 @@ sub _load_content_plugins { push @accepted_types, @$stashed; } # then content types requested by caller - push @accepted_types, @{ $c->request->accepted_content_types }; + push @accepted_types, + $search_path =~ m{Deserialize} + ? @{ $c->request->accepted_content_types } + : @{ $c->request->accepted_response_content_types } + ; + # then the default push @accepted_types, $config->{'default'} if $config->{'default'}; # pick the best match that we have a serializer mapping for diff --git a/lib/Catalyst/TraitFor/Request/REST.pm b/lib/Catalyst/TraitFor/Request/REST.pm index 4a2ca87..1d35710 100644 --- a/lib/Catalyst/TraitFor/Request/REST.pm +++ b/lib/Catalyst/TraitFor/Request/REST.pm @@ -24,14 +24,46 @@ has preferred_content_type => ( init_arg => undef, ); -sub _build_accepted_content_types { - my $self = shift; +has accepted_response_content_types => ( + is => 'ro', + isa => 'ArrayRef', + lazy => 1, + builder => '_build_accepted_response_content_types', + init_arg => undef, +); - my %types; +has preferred_response_content_type => ( + is => 'ro', + isa => 'Str', + lazy => 1, + builder => '_build_preferred_response_content_type', + init_arg => undef, +); + +sub _accepted_types_sort { + my ($self, %types) = @_; + [ sort { $types{$b} <=> $types{$a} } keys %types ]; +} +sub _build_accepted_content_types { + my $self = shift; + my %types = $self->_accepted_response_content_types_inner; # First, we use the content type in the HTTP Request. It wins all. $types{ $self->content_type } = 3 if $self->content_type; + $self->_accepted_types_sort(%types); +} + +sub _build_accepted_response_content_types { + my $self = shift; + my %types = $self->_accepted_response_content_types_inner; + $self->_accepted_types_sort(%types); +} + +sub _accepted_response_content_types_inner { + my $self = shift; + + my %types; if ($self->method eq "GET" && $self->param('content-type')) { $types{ $self->param('content-type') } = 2; @@ -66,11 +98,13 @@ sub _build_accepted_content_types { } } - [ sort { $types{$b} <=> $types{$a} } keys %types ]; + %types; } sub _build_preferred_content_type { $_[0]->accepted_content_types->[0] } +sub _build_preferred_response_content_type { $_[0]->accepted_response_content_types->[0] } + sub accepts { my $self = shift; my $type = shift;