X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Engine-STOMP.git;a=blobdiff_plain;f=lib%2FCatalyst%2FController%2FMessageDriven.pm;h=56883c07cf905325a6a38f3a325ab1edc28f8fd0;hp=4e87cb2a79d29a3cf20713e8682e186b426086b8;hb=HEAD;hpb=bd1954f9fb32631d0c9d3130aec292c5577abe4b diff --git a/lib/Catalyst/Controller/MessageDriven.pm b/lib/Catalyst/Controller/MessageDriven.pm index 4e87cb2..56883c0 100644 --- a/lib/Catalyst/Controller/MessageDriven.pm +++ b/lib/Catalyst/Controller/MessageDriven.pm @@ -1,5 +1,9 @@ package Catalyst::Controller::MessageDriven; use Moose; +use Data::Serializer; +use Moose::Util::TypeConstraints; +use MooseX::Types::Moose qw/Str/; +use namespace::autoclean; BEGIN { extends 'Catalyst::Controller' } @@ -14,7 +18,10 @@ Catalyst::Controller::MessageDriven BEGIN { extends 'Catalyst::Controller::MessageDriven' } sub some_action : Local { - my ($self, $c) = @_; + my ($self, $c, $message) = @_; + + # Handle message + # Reply with a minimal response message my $response = { type => 'testaction_response' }; $c->stash->{response} = $response; @@ -26,29 +33,112 @@ A Catalyst controller base class for use with Catalyst::Engine::Stomp, which handles YAML-serialized messages. A top-level "type" key in the YAML determines the action dispatched to. +=head1 METHODS + +=head2 begin + +Deserializes the request into C<< $c->stash->{request} >> + +=head2 default + +Dispatches to method named by the key C<< $c->stash->{request}->{type} >> + +=head2 end + +Serializes the response from C<< $c->stash->{response} >> + +=head1 CONFIGURATION + +In the configuration file add the following to set the value for a parameter + + + type_key foo + + +=head2 type_key + +The hash key the module will try to pull out the received message to call +within the controller. This defaults to 'type'. + +=head2 serializer + +The serializer used to serialiser/deserialise. See Data::Serializer to see +what is available. Defaults to YAML. JSON is anotther that is available. + + =cut -__PACKAGE__->config( - 'default' => 'text/x-yaml', - 'stash_key' => 'response', - 'map' => { 'text/x-yaml' => 'YAML' }, - ); +class_type 'Data::Serializer'; +my $serializer_t = subtype 'Data::Serializer', where { 1 }; +coerce $serializer_t, from 'Str', + via { Data::Serializer->new( serializer => $_ ) }; + +has serializer => ( + isa => $serializer_t, is => 'ro', required => 1, + default => 'YAML', coerce => 1, +); -sub begin :ActionClass('Deserialize') { } +has type_key => ( + is => 'ro', required =>1, + default => 'type', +); -sub end :ActionClass('Serialize') { + +sub begin : Private { + my ($self, $c) = @_; + + # Deserialize the request message + my $message; + my $s = $self->serializer; + eval { + my $body = $c->request->body; + open my $IN, "$body" or die "can't open temp file $body"; + $message = $s->raw_deserialize(do { local $/; <$IN> }); + }; + if ($@) { + # can't reply - reply_to is embedded in the message + $c->error("exception in deserialize: $@"); + } + else { + $c->stash->{request} = $message; + } +} + +sub end : Private { my ($self, $c) = @_; # Engine will send our reply based on the value of this header. - $c->response->headers->header( 'X-Reply-Address' => $c->req->data->{reply_to} ); + $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} ); + + # The wire response + my $output; + + # Load a serializer + my $s = $self->serializer; # Custom error handler - steal errors from catalyst and dump them into # the stash, to get them serialized out as the reply. if (scalar @{$c->error}) { - my $error = join "\n", @{$c->error}; - $c->stash->{response} = { status => 'ERROR', error => $error }; - $c->error(0); # clear errors, so our response isn't clobbered + $c->log->error($_) for @{$c->error}; # Log errors in Catalyst + my $error = join "\n", @{$c->error}; + $c->stash->{response} = { status => 'ERROR', error => $error }; + $output = $s->serialize( $c->stash->{response} ); + $c->clear_errors; + $c->response->status(400); } + + # Serialize the response + eval { + $output = $s->raw_serialize( $c->stash->{response} ); + }; + if ($@) { + my $error = "exception in serialize: $@"; + $c->stash->{response} = { status => 'ERROR', error => $error }; + $output = $s->serialize( $c->stash->{response} ); + $c->response->status(400); + } + + $c->response->output( $output ); } sub default : Private { @@ -56,10 +146,28 @@ sub default : Private { # Forward the request to the appropriate action, based on the # message type. - my $action = $c->req->data->{type}; - $c->forward($action, [$c->req->data]); + my $action = $c->stash->{request}->{ $self->type_key }; + if (defined $action) { + $c->forward($action, [$c->stash->{request}]); + } + else { + $c->error('no message type specified'); + } } __PACKAGE__->meta->make_immutable; -1; +=head1 AUTHOR AND CONTRIBUTORS + +See information in L + +=head1 LICENCE AND COPYRIGHT + +Copyright (C) 2009 Venda Ltd + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.8.8 or, +at your option, any later version of Perl 5 you may have available. + +=cut +