Commit | Line | Data |
0a663589 |
1 | package Catalyst::Controller::MessageDriven; |
2 | use Moose; |
3 | |
4 | BEGIN { extends 'Catalyst::Controller' } |
5 | |
d78bb739 |
6 | =head1 NAME |
7 | |
8 | Catalyst::Controller::MessageDriven |
9 | |
10 | =head1 SYNOPSIS |
11 | |
12 | package MyApp::Controller::Queue; |
13 | use Moose; |
14 | BEGIN { extends 'Catalyst::Controller::MessageDriven' } |
15 | |
bd1954f9 |
16 | sub some_action : Local { |
d78bb739 |
17 | my ($self, $c) = @_; |
18 | # Reply with a minimal response message |
19 | my $response = { type => 'testaction_response' }; |
20 | $c->stash->{response} = $response; |
21 | } |
22 | |
23 | =head1 DESCRIPTION |
24 | |
25 | A Catalyst controller base class for use with Catalyst::Engine::Stomp, |
26 | which handles YAML-serialized messages. A top-level "type" key in the |
bd1954f9 |
27 | YAML determines the action dispatched to. |
d78bb739 |
28 | |
29 | =cut |
30 | |
0a663589 |
31 | __PACKAGE__->config( |
bd1954f9 |
32 | 'default' => 'text/x-yaml', |
33 | 'stash_key' => 'response', |
34 | 'map' => { 'text/x-yaml' => 'YAML' }, |
35 | ); |
0a663589 |
36 | |
37 | sub begin :ActionClass('Deserialize') { } |
38 | |
39 | sub end :ActionClass('Serialize') { |
bd1954f9 |
40 | my ($self, $c) = @_; |
41 | |
42 | # Engine will send our reply based on the value of this header. |
43 | $c->response->headers->header( 'X-Reply-Address' => $c->req->data->{reply_to} ); |
44 | |
45 | # Custom error handler - steal errors from catalyst and dump them into |
46 | # the stash, to get them serialized out as the reply. |
47 | if (scalar @{$c->error}) { |
48 | my $error = join "\n", @{$c->error}; |
49 | $c->stash->{response} = { status => 'ERROR', error => $error }; |
50 | $c->error(0); # clear errors, so our response isn't clobbered |
51 | } |
0a663589 |
52 | } |
53 | |
54 | sub default : Private { |
bd1954f9 |
55 | my ($self, $c) = @_; |
56 | |
57 | # Forward the request to the appropriate action, based on the |
58 | # message type. |
59 | my $action = $c->req->data->{type}; |
60 | $c->forward($action, [$c->req->data]); |
0a663589 |
61 | } |
62 | |
63 | __PACKAGE__->meta->make_immutable; |
64 | |
5cc6aac9 |
65 | =head1 METHODS |
66 | |
67 | =head2 default |
68 | |
69 | Forwards the request to the appropriate action based on the 'type' field |
70 | within the message data. |
71 | |
72 | =head2 begin |
73 | |
74 | Uses L<Catalyst::Action::Deserialize> to unserialize the message. |
75 | |
76 | =head2 end |
77 | |
78 | Serializes the data stashed by the dispatched action, and |
79 | arranges for the reply to be sent to the endpoint nominated in |
80 | the request's 'reply_to' field. |
81 | |
82 | Supplies custom exception handling which returns |
83 | throw exceptions as a serialized return message. |
84 | |
85 | =cut |
86 | |