- make type_key 'ro' after controller has been created no real need to change it
[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 =cut
51
52 class_type 'Data::Serializer';
53 my $serializer_t = subtype 'Data::Serializer', where { 1 };
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 );
61
62 has type_key => (
63     is => 'ro', required =>1,
64     default => 'type',
65 );
66
67
68 sub begin : Private {
69     my ($self, $c) = @_;
70
71     # Deserialize the request message
72     my $message;
73     my $s = $self->serializer;
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     }
86 }
87
88 sub end : Private {
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
98     my $s = $self->serializer;
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}) {
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 };
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 );
123 }
124
125 sub default : Private {
126     my ($self, $c) = @_;
127
128     # Forward the request to the appropriate action, based on the
129     # message type.
130     my $action = $c->stash->{request}->{ $self->type_key };
131     if (defined $action) {
132         $c->forward($action, [$c->stash->{request}]);
133     }
134     else {
135          $c->error('no message type specified');
136     }
137 }
138
139 __PACKAGE__->meta->make_immutable;
140