Initial import of Catalyst::Engine::Stomp
[catagits/Catalyst-Engine-STOMP.git] / lib / Catalyst / Controller / MessageDriven.pm
1 package Catalyst::Controller::MessageDriven;
2 use Moose;
3
4 BEGIN { extends 'Catalyst::Controller' }
5
6 __PACKAGE__->config(
7                     'default'   => 'text/x-yaml',
8                     'stash_key' => 'response',
9                     'map'       => { 'text/x-yaml' => 'YAML' },
10                    );
11
12 sub begin :ActionClass('Deserialize') { }
13
14 sub end :ActionClass('Serialize') {
15         my ($self, $c) = @_;
16
17         # Engine will send our reply based on the value of this header.
18         $c->response->headers->header( 'X-Reply-Address' => $c->req->data->{reply_to} );
19
20         # Custom error handler - steal errors from catalyst and dump them into
21         # the stash, to get them serialized out as the reply.
22         if (scalar @{$c->error}) {
23                 my $error = join "\n", @{$c->error};
24                 $c->stash->{response} = { status => 'ERROR', error => $error };
25                 $c->error(0); # clear errors, so our response isn't clobbered
26         }
27 }
28
29 sub default : Private {
30         my ($self, $c) = @_;
31         
32         # Forward the request to the appropriate action, based on the
33         # message type.
34         my $action = $c->req->data->{type};
35         $c->forward($action, [$c->req->data]);
36 }
37
38 __PACKAGE__->meta->make_immutable;
39
40 1;