Add boilderplate POD
[catagits/Catalyst-Engine-STOMP.git] / lib / Catalyst / Controller / MessageDriven.pm
CommitLineData
0a663589 1package Catalyst::Controller::MessageDriven;
2use Moose;
a5ae1e8c 3use Data::Serializer;
2b075013 4use Moose::Util::TypeConstraints;
5use MooseX::Types::Moose qw/Str/;
03c90167 6use namespace::autoclean;
0a663589 7
8BEGIN { extends 'Catalyst::Controller' }
9
d78bb739 10=head1 NAME
11
12Catalyst::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
32A Catalyst controller base class for use with Catalyst::Engine::Stomp,
33which handles YAML-serialized messages. A top-level "type" key in the
5ec5e0b1 34YAML determines the action dispatched to.
d78bb739 35
7f592702 36=head1 METHODS
37
38=head2 begin
39
40Deserializes the request into C<< $c->stash->{request} >>
41
42=head2 default
43
44Dispatches to method named by the key C<< $c->stash->{request}->{type} >>
45
46=head2 end
47
48Serializes the response from C<< $c->stash->{response} >>
49
d7c364aa 50=head1 CONFIGURATION
51
52In the configuration file add the following to set the value for a parameter
53
54 <MessageDriven>
55 type_key foo
56 </MessageDriven>
57
58=head2 type_key
59
60The hash key the module will try to pull out the received message to call
61within the controller. This defaults to 'type'.
62
63=head2 serializer
64
65The serializer used to serialiser/deserialise. See Data::Serializer to see
66what is available. Defaults to YAML. JSON is anotther that is available.
67
68
d78bb739 69=cut
70
2b075013 71class_type 'Data::Serializer';
6aff22e8 72my $serializer_t = subtype 'Data::Serializer', where { 1 };
2b075013 73coerce $serializer_t, from 'Str',
74 via { Data::Serializer->new( serializer => $_ ) };
75
76has serializer => (
77 isa => $serializer_t, is => 'ro', required => 1,
78 default => 'YAML', coerce => 1,
79);
a5ae1e8c 80
520b6f81 81has type_key => (
939aaa46 82 is => 'ro', required =>1,
520b6f81 83 default => 'type',
84);
85
86
5ec5e0b1 87sub begin : Private {
88 my ($self, $c) = @_;
89
90 # Deserialize the request message
520b6f81 91 my $message;
2b075013 92 my $s = $self->serializer;
5ec5e0b1 93 eval {
94 my $body = $c->request->body;
95 open my $IN, "$body" or die "can't open temp file $body";
96 $message = $s->raw_deserialize(do { local $/; <$IN> });
97 };
98 if ($@) {
99 # can't reply - reply_to is embedded in the message
100 $c->error("exception in deserialize: $@");
101 }
102 else {
103 $c->stash->{request} = $message;
104 }
bf8937b7 105}
0a663589 106
bf8937b7 107sub end : Private {
5ec5e0b1 108 my ($self, $c) = @_;
109
110 # Engine will send our reply based on the value of this header.
111 $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} );
112
113 # The wire response
114 my $output;
115
116 # Load a serializer
2b075013 117 my $s = $self->serializer;
5ec5e0b1 118
119 # Custom error handler - steal errors from catalyst and dump them into
120 # the stash, to get them serialized out as the reply.
121 if (scalar @{$c->error}) {
8f0f19f9 122 $c->log->error($_) for @{$c->error}; # Log errors in Catalyst
123 my $error = join "\n", @{$c->error};
124 $c->stash->{response} = { status => 'ERROR', error => $error };
5ec5e0b1 125 $output = $s->serialize( $c->stash->{response} );
126 $c->clear_errors;
127 $c->response->status(400);
128 }
129
130 # Serialize the response
131 eval {
132 $output = $s->raw_serialize( $c->stash->{response} );
133 };
134 if ($@) {
135 my $error = "exception in serialize: $@";
136 $c->stash->{response} = { status => 'ERROR', error => $error };
137 $output = $s->serialize( $c->stash->{response} );
138 $c->response->status(400);
139 }
140
141 $c->response->output( $output );
0a663589 142}
143
144sub default : Private {
5ec5e0b1 145 my ($self, $c) = @_;
146
147 # Forward the request to the appropriate action, based on the
148 # message type.
520b6f81 149 my $action = $c->stash->{request}->{ $self->type_key };
5ec5e0b1 150 if (defined $action) {
151 $c->forward($action, [$c->stash->{request}]);
152 }
153 else {
154 $c->error('no message type specified');
155 }
0a663589 156}
157
158__PACKAGE__->meta->make_immutable;
159
491ffbb3 160=head1 AUTHOR AND CONTRIBUTORS
161
162See information in L<Catalyst::Engine::Stomp>
163
164=head1 LICENCE AND COPYRIGHT
165
166Copyright (C) 2009 Venda Ltd
167
168This library is free software; you can redistribute it and/or modify
169it under the same terms as Perl itself, either Perl version 5.8.8 or,
170at your option, any later version of Perl 5 you may have available.
171
172=cut
173