Add boilderplate POD
[catagits/Catalyst-Engine-STOMP.git] / lib / Catalyst / Controller / MessageDriven.pm
1 package Catalyst::Controller::MessageDriven;
2 use Moose;
3 use Data::Serializer;
4 use Moose::Util::TypeConstraints;
5 use MooseX::Types::Moose qw/Str/;
6 use namespace::autoclean;
7
8 BEGIN { extends 'Catalyst::Controller' }
9
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
20   sub some_action : Local {
21       my ($self, $c, $message) = @_;
22
23       # Handle message
24
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
34 YAML determines the action dispatched to.
35
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
50 =head1 CONFIGURATION
51
52 In 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
60 The hash key the module will try to pull out the received message to call
61 within the controller. This defaults to 'type'.
62
63 =head2 serializer
64
65 The serializer used to serialiser/deserialise. See Data::Serializer to see
66 what is available. Defaults to YAML. JSON is anotther that is available.
67
68
69 =cut
70
71 class_type 'Data::Serializer';
72 my $serializer_t = subtype 'Data::Serializer', where { 1 };
73 coerce $serializer_t, from 'Str',
74     via { Data::Serializer->new( serializer => $_ ) };
75
76 has serializer => (
77     isa => $serializer_t, is => 'ro', required => 1,
78     default => 'YAML', coerce => 1,
79 );
80
81 has type_key => (
82     is => 'ro', required =>1,
83     default => 'type',
84 );
85
86
87 sub begin : Private {
88     my ($self, $c) = @_;
89
90     # Deserialize the request message
91     my $message;
92     my $s = $self->serializer;
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     }
105 }
106
107 sub end : Private {
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
117     my $s = $self->serializer;
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}) {
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 };
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 );
142 }
143
144 sub default : Private {
145     my ($self, $c) = @_;
146
147     # Forward the request to the appropriate action, based on the
148     # message type.
149     my $action = $c->stash->{request}->{ $self->type_key };
150     if (defined $action) {
151         $c->forward($action, [$c->stash->{request}]);
152     }
153     else {
154          $c->error('no message type specified');
155     }
156 }
157
158 __PACKAGE__->meta->make_immutable;
159
160 =head1 AUTHOR AND CONTRIBUTORS
161
162 See information in L<Catalyst::Engine::Stomp>
163
164 =head1 LICENCE AND COPYRIGHT
165
166 Copyright (C) 2009 Venda Ltd
167
168 This library is free software; you can redistribute it and/or modify
169 it under the same terms as Perl itself, either Perl version 5.8.8 or,
170 at your option, any later version of Perl 5 you may have available.
171
172 =cut
173