Commit | Line | Data |
0a663589 |
1 | package Catalyst::Controller::MessageDriven; |
2 | use Moose; |
a5ae1e8c |
3 | use Data::Serializer; |
2b075013 |
4 | use Moose::Util::TypeConstraints; |
5 | use MooseX::Types::Moose qw/Str/; |
03c90167 |
6 | use namespace::autoclean; |
0a663589 |
7 | |
8 | BEGIN { extends 'Catalyst::Controller' } |
9 | |
d78bb739 |
10 | =head1 NAME |
11 | |
12 | Catalyst::Controller::MessageDriven |
13 | |
14 | =head1 SYNOPSIS |
15 | |
16 | package MyApp::Controller::Queue; |
17 | use Moose; |
18 | BEGIN { extends 'Catalyst::Controller::MessageDriven' } |
19 | |
5ec5e0b1 |
20 | sub some_action : Local { |
bf8937b7 |
21 | my ($self, $c, $message) = @_; |
22 | |
5ec5e0b1 |
23 | # Handle message |
bf8937b7 |
24 | |
d78bb739 |
25 | # Reply with a minimal response message |
26 | my $response = { type => 'testaction_response' }; |
27 | $c->stash->{response} = $response; |
28 | } |
29 | |
30 | =head1 DESCRIPTION |
31 | |
32 | A Catalyst controller base class for use with Catalyst::Engine::Stomp, |
33 | which handles YAML-serialized messages. A top-level "type" key in the |
5ec5e0b1 |
34 | YAML determines the action dispatched to. |
d78bb739 |
35 | |
7f592702 |
36 | =head1 METHODS |
37 | |
38 | =head2 begin |
39 | |
40 | Deserializes the request into C<< $c->stash->{request} >> |
41 | |
42 | =head2 default |
43 | |
44 | Dispatches to method named by the key C<< $c->stash->{request}->{type} >> |
45 | |
46 | =head2 end |
47 | |
48 | Serializes the response from C<< $c->stash->{response} >> |
49 | |
d78bb739 |
50 | =cut |
51 | |
2b075013 |
52 | class_type 'Data::Serializer'; |
6aff22e8 |
53 | my $serializer_t = subtype 'Data::Serializer', where { 1 }; |
2b075013 |
54 | coerce $serializer_t, from 'Str', |
55 | via { Data::Serializer->new( serializer => $_ ) }; |
56 | |
57 | has serializer => ( |
58 | isa => $serializer_t, is => 'ro', required => 1, |
59 | default => 'YAML', coerce => 1, |
60 | ); |
a5ae1e8c |
61 | |
520b6f81 |
62 | has type_key => ( |
63 | is => 'rw', required =>1, |
64 | default => 'type', |
65 | ); |
66 | |
67 | |
5ec5e0b1 |
68 | sub begin : Private { |
69 | my ($self, $c) = @_; |
70 | |
71 | # Deserialize the request message |
520b6f81 |
72 | my $message; |
2b075013 |
73 | my $s = $self->serializer; |
5ec5e0b1 |
74 | eval { |
75 | my $body = $c->request->body; |
76 | open my $IN, "$body" or die "can't open temp file $body"; |
77 | $message = $s->raw_deserialize(do { local $/; <$IN> }); |
78 | }; |
79 | if ($@) { |
80 | # can't reply - reply_to is embedded in the message |
81 | $c->error("exception in deserialize: $@"); |
82 | } |
83 | else { |
84 | $c->stash->{request} = $message; |
85 | } |
bf8937b7 |
86 | } |
0a663589 |
87 | |
bf8937b7 |
88 | sub end : Private { |
5ec5e0b1 |
89 | my ($self, $c) = @_; |
90 | |
91 | # Engine will send our reply based on the value of this header. |
92 | $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} ); |
93 | |
94 | # The wire response |
95 | my $output; |
96 | |
97 | # Load a serializer |
2b075013 |
98 | my $s = $self->serializer; |
5ec5e0b1 |
99 | |
100 | # Custom error handler - steal errors from catalyst and dump them into |
101 | # the stash, to get them serialized out as the reply. |
102 | if (scalar @{$c->error}) { |
8f0f19f9 |
103 | $c->log->error($_) for @{$c->error}; # Log errors in Catalyst |
104 | my $error = join "\n", @{$c->error}; |
105 | $c->stash->{response} = { status => 'ERROR', error => $error }; |
5ec5e0b1 |
106 | $output = $s->serialize( $c->stash->{response} ); |
107 | $c->clear_errors; |
108 | $c->response->status(400); |
109 | } |
110 | |
111 | # Serialize the response |
112 | eval { |
113 | $output = $s->raw_serialize( $c->stash->{response} ); |
114 | }; |
115 | if ($@) { |
116 | my $error = "exception in serialize: $@"; |
117 | $c->stash->{response} = { status => 'ERROR', error => $error }; |
118 | $output = $s->serialize( $c->stash->{response} ); |
119 | $c->response->status(400); |
120 | } |
121 | |
122 | $c->response->output( $output ); |
0a663589 |
123 | } |
124 | |
125 | sub default : Private { |
5ec5e0b1 |
126 | my ($self, $c) = @_; |
127 | |
128 | # Forward the request to the appropriate action, based on the |
129 | # message type. |
520b6f81 |
130 | my $action = $c->stash->{request}->{ $self->type_key }; |
5ec5e0b1 |
131 | if (defined $action) { |
132 | $c->forward($action, [$c->stash->{request}]); |
133 | } |
134 | else { |
135 | $c->error('no message type specified'); |
136 | } |
0a663589 |
137 | } |
138 | |
139 | __PACKAGE__->meta->make_immutable; |
140 | |